Author Topic: combings arrays of numbers  (Read 1125 times)

boxoroxs

  • New Member
  • *
  • Posts: 20
  • Karma: 0
    • View Profile
Re: combings arrays of numbers
« Reply #15 on: July 14, 2012, 02:48:08 PM »
The end result is 6 different numbers... which may or may not repeat.

I need to turn it into one 6 digit number and then compare that number to an array of employee id numbers to see if there is a match.

ErnieAlex

  • Expert PHP Helper
  • Senior Member
  • *****
  • Posts: 1847
  • Karma: 32
    • View Profile
Re: combings arrays of numbers
« Reply #16 on: July 14, 2012, 02:56:51 PM »
Okay, perhaps I am not clear on this...

So, you have a list of employee numbers?  Correct?
Where are they stored?

You want to pick 6 random numbers and then see if they match the employee list of numbers?  Correct?

Where is your list of employee numbers stored.  Most likely you are way over-coding this...

Also, if you pick a 6 digit number, what happens if it is not in the employee list of numbers?
Do you just pick another 6 digit number?   

boxoroxs

  • New Member
  • *
  • Posts: 20
  • Karma: 0
    • View Profile
Re: combings arrays of numbers
« Reply #17 on: July 14, 2012, 03:06:49 PM »
The employee numbers are in their own array. I am not skilled enough to use php with a database yet.

Yes. Pick 6 numbers, check for a match, if no match, then no match... for now. I double check every random result.

I'm running it as a lottery type game. You may or may not win.

I am also using a weighted rand. Depending on the result of that, the script will pick 6 random numbers, or a random employee number.

I don't know how to make it rerun to pick new numbers aside from refreshing the page.  :-X

ErnieAlex

  • Expert PHP Helper
  • Senior Member
  • *****
  • Posts: 1847
  • Karma: 32
    • View Profile
Re: combings arrays of numbers
« Reply #18 on: July 14, 2012, 10:31:22 PM »
You said:
Quote
I don't know how to make it rerun to pick new numbers aside from refreshing the page.

So, your code picks the numbers, 6 of them and places them into an array.  Then you do some odd compares and check other numbers and finally do some implode to create a 6 digit number and compare it to employees array....   Still silly from what you have told me.  Maybe I am still mixed up on what you want to do.

If you want to pick a random 6 digit number, just do $num=rand(111112,99998);  (You said there was not any 1111111 or 999999...  So, that would create a random number between 111112 and 999998.  Then, you just check the employee array for an entry...   If(isset(emp_array[$num]))...etc...   If the employee array for that random number ($num) is valid and contains info, then, you can continue.  If that entry is empty, then there is no employee with that number...   Is that more of what you are looking for?

boxoroxs

  • New Member
  • *
  • Posts: 20
  • Karma: 0
    • View Profile
Re: combings arrays of numbers
« Reply #19 on: July 15, 2012, 03:40:48 PM »
Thanks for all your help.

I'm sure I over complicated this script, but it helps me to understand the functions. As I said before, when I figure things out, I rewrite the sections.

So, originally I used RAND with a range between 108832 and 642096. This is the lowest id number to the highest. Not every number in this range is an active employee however.

This makes it fun, because as a lottery game, there is a smaller range to select from, but also a lot of additional inactive numbers that can pop up.

As a raffle game, I can change some of the code so it just picks a random number from the employee id array instead. That way there is a guaranteed winner.

Why did I complicate the code with switches and implodes and such?

Well... I found an old php lottery game script that is automated with a timer and displays the winning numbers as balls.... which is just a gif image obviously.

The 6 digit id numbers are a single number, versus 6 individual numbers, so they won't display properly in the ball image.

As 6 separate numbers, they can be displayed in the ball images, and as a single 6 digit number, I can them check the id array for a match.

 Again, I'm sure I've over complicated this since I'm just learning php, and there is probably a WAY simpler way to do it, but I'm enjoying figuring out how it all works.

ErnieAlex

  • Expert PHP Helper
  • Senior Member
  • *****
  • Posts: 1847
  • Karma: 32
    • View Profile
Re: combings arrays of numbers
« Reply #20 on: July 16, 2012, 01:46:30 PM »
Well, the programmer is always right!   LOL...  But, yes, you may have over-complicated it a bit.

Just FYI, you can take a 6-digit number and strip out each position with a small amount of code, which would allow you to dump all the arrays and the switches.  A simple 6 to 12 line code would pull out the values and display the six "balls". 

Also, since you are learning, the best way to create a program is to first lay out the idea and look at all the possible ways you could create the project.  Usually, if you look at the layout of the project, you will find a lot of ways to do the same thing.  Then, think about the code involved and you will come up with a good way to do it.  Of course, next you will see where you create too much code for a section (such as your 200 lines you dumped) and you can then look for possible better ways. 

As you acquire experience, you will just "know" more and more ways to handle your programming idea.  Then, pick the fastest and smallest code that works for you...     So, just my thoughts on it all...  Good luck!

boxoroxs

  • New Member
  • *
  • Posts: 20
  • Karma: 0
    • View Profile
Re: combings arrays of numbers
« Reply #21 on: July 16, 2012, 02:10:50 PM »
Thanks.

I have yet to figure out how to pull out the individual numbers so I am taking the long road. lol.

From there, I'd have to figure out the timer part of code and make it work with my script.
It also accesses a database, which is way above me at the moment.

Not to mention the 'easy pick' feature.

whew!

ErnieAlex

  • Expert PHP Helper
  • Senior Member
  • *****
  • Posts: 1847
  • Karma: 32
    • View Profile
Re: combings arrays of numbers
« Reply #22 on: July 16, 2012, 02:36:13 PM »
Well, for "easy pick"  you just use your rand() function again and limit to the span of your values...  ( Easy! )

And, to pull out a digit, first you just "cast" it to a string.  This means that the number is turned into a string.
That is done like this:  (assuming $number is the random number)   $num_string = (string)$number;
This gives you a string value for the number.   Sooooo, from any "string" you can pull one letter which would
actually be your single number digit.  In this case, if you wanted the 3rd digit, you would use somehting like:
   $third_digit = substr((string)$number, 2, 1);   Note, this simple line "cast's" the number into a string and
then plucks out the 3 digit.  Since a number such as 123456 would cast into a string like "123456", you can
pull out any single digit using substr...   One problem with this is that the number would have to be 6 full digits
because 6 digit numbers starting with a zero would cause problems...  (Lots of ways around that in code!)
So, if your numbers all verified to all be 6 digits, no problems!

Hope all that makes sense...  One simple line to pull out any digit.  You can also use a variable instead of a
locked in starting number and loop thru it to pull a digit for displaying the numbered "ball" image...

Well, ALL THINGS can be programmed!  LOL...  Just some changes and it can be programmed smaller...  Ha!

boxoroxs

  • New Member
  • *
  • Posts: 20
  • Karma: 0
    • View Profile
Re: combings arrays of numbers
« Reply #23 on: July 16, 2012, 04:25:42 PM »
Thanks for response. Something I'll definitely have to look into!

Lots to understand and work with.

The parentheses are hard enough to keep straight!

I'll have to play around with that.

ErnieAlex

  • Expert PHP Helper
  • Senior Member
  • *****
  • Posts: 1847
  • Karma: 32
    • View Profile
Re: combings arrays of numbers
« Reply #24 on: July 16, 2012, 04:37:55 PM »
Well, paren's are easy...  They are mainly just used for two things.  To send arguments to functions like this:

   Some_Function(arg1, arg2, arg3)

And, to separate calculations in IF's, etc...  Like this:  (note that an IF is actually a function of a sort)

   If( $somevarible=="somevalue" and ($x!=$y) or ($totalvariable>100)) { do some code }

And, same with brackets:  { }   They are mainly used to group code for functions or IF's...

Anyway, use  php.net/manual  to look up any of this type of thing...    And, use w3schools.com for tutorials as they have really great ones...     Good luck!

boxoroxs

  • New Member
  • *
  • Posts: 20
  • Karma: 0
    • View Profile
Re: combings arrays of numbers
« Reply #25 on: July 18, 2012, 02:41:53 PM »
So what would cause this?

Allowed memory size of 134217728 bytes exhausted...

ErnieAlex

  • Expert PHP Helper
  • Senior Member
  • *****
  • Posts: 1847
  • Karma: 32
    • View Profile
Re: combings arrays of numbers
« Reply #26 on: July 18, 2012, 04:08:12 PM »
LOL, I got that error awhile back on a project.  The number was a bit different...

Well, depending on the real error which can be shown if needed by altering the server's error display settings.
Most likely it is that you overflowed the browser's allowed memory stack.  This is often because you set up a
loop and it counted too far or because you looped and filled an array with too much data.

The limit for PHP by default is 128megs.  (1024x1024x1024, hence the 134meg number)  If you feel this limit needs to be increased, you can use this command near the top of the PHP code:
   ini_set('memory_limit', '-1');   (Removes the limit...)

But, that is a huge amount of memory to be using.  You can reach this limit by filling a variable with database data and not limiting the data pulled from the database.  Not sure why your code is causing this.  Could be several things.  But, you can override it, but, most likely you should look at your code and look for loops that have no endings.   (Or post it all here...)  A loop that tests for some variable being over a value and the variable never changes, the loop would run forever...

Well, something for you to think about...  Good luck, let us know..

boxoroxs

  • New Member
  • *
  • Posts: 20
  • Karma: 0
    • View Profile
Re: combings arrays of numbers
« Reply #27 on: July 18, 2012, 04:34:52 PM »
Hmmm... I was testing using range with the minimum and maximum numbers I posted earlier, instead of using the 6 arrays limited to 0 through 9.

What's weird is that it worked before when I was first playing around with php.

ErnieAlex

  • Expert PHP Helper
  • Senior Member
  • *****
  • Posts: 1847
  • Karma: 32
    • View Profile
Re: combings arrays of numbers
« Reply #28 on: July 19, 2012, 08:57:25 PM »
Well, there are a lot of setting in PHP for options such as memory usage.  But, most likely it is just some small error in your logic of the code.  Can you post the area that is causing the error and we can take a peek at it?


boxoroxs

  • New Member
  • *
  • Posts: 20
  • Karma: 0
    • View Profile
Re: combings arrays of numbers
« Reply #29 on: July 21, 2012, 10:15:13 PM »
I found my error. I had typed range (x,y)  instead of rand(x,y).

And the subsr function shaved another 100 lines of code off. Thanks for that!

Plugging along nicely....