Ell20 Posted August 6, 2008 Share Posted August 6, 2008 Hi, I am generating 20 random, unique numbers and storing them into an array: $nums = array(); $rn = rand(1,80); for ($cnt = 1; $cnt < 21;$cnt++) { while (in_array($rn,$nums)) $rn = rand(1,80); $nums[] = $rn; } I also have 10 numbers chosen by the user stored in variables: $value1, $value2, $value3 etc I cant work out how to run through these numbers looking for matches. Can anyone help me do this? Thanks Quote Link to comment Share on other sites More sharing options...
Ell20 Posted August 6, 2008 Author Share Posted August 6, 2008 Think I have managed to do this now, but not sure its the most efficient way! Quote Link to comment Share on other sites More sharing options...
DarkWater Posted August 6, 2008 Share Posted August 6, 2008 Well, show us what you did. Quote Link to comment Share on other sites More sharing options...
Ell20 Posted August 6, 2008 Author Share Posted August 6, 2008 $nums = array(); $rn = rand(1,80); for ($cnt = 1; $cnt < 21;$cnt++) { while (in_array($rn,$nums)) $rn = rand(1,80); $nums[$cnt] = $rn; echo $nums[$cnt].'<br>'; if ($nums[$cnt] == $value1) { echo 'Match '.$value1.'<br>'; $background = 'purple'; } if ($nums[$cnt] == $value2) { echo 'Match '.$value2.'<br>'; $background = 'purple'; } //etc Quote Link to comment Share on other sites More sharing options...
Ell20 Posted August 6, 2008 Author Share Posted August 6, 2008 What I am aiming to do eventually is: - If a number matches turn the background of the td purple - Numbers which the user selected but didnt match turn the background of the td red - Numbers which came up but the user didnt select turn the background of the td blue I cant work out how I am going to link this to the TD either, I will obviously need to add some PHP but I cant work out a decent, sensible solution at the moment. Any help is appreciated. Quote Link to comment Share on other sites More sharing options...
DarkWater Posted August 6, 2008 Share Posted August 6, 2008 And these are random numbers that you want to match user input against? What's the name of the user input array? I'll see if I can write something up. Quote Link to comment Share on other sites More sharing options...
Ell20 Posted August 6, 2008 Author Share Posted August 6, 2008 Yeah basically the user selects 10 values which I stored in $value1, $value2......$value10. Then I have used the code below to create 20 random, unique numbers between 1 and 80: $nums = array(); $rn = rand(1,80); for ($cnt = 1; $cnt < 21;$cnt++) { while (in_array($rn,$nums)) $rn = rand(1,80); $nums[$cnt] = $rn; } Then I have 80 td's which store the numbers which I will need to turn a different colour depending on the result. Quote Link to comment Share on other sites More sharing options...
DarkWater Posted August 6, 2008 Share Posted August 6, 2008 Can you store them in an array instead? Then it'll be a lot easier. You can just do: <?php //array $values is user input and array $nums is your random numbers: foreach ($values as $key=>$value) { if (in_array($value, $nums)) { unset($values[$key]); $keyOfNum = array_search($value, $nums); unset($nums[$keyOfNum]); $matched[] = $value; } } ?> That way you can just loop through the $nums, $matched, and $values arrays to simply have which values didn't show up in which because it'll be NULL where it showed up so it won't be in the loop. Quote Link to comment Share on other sites More sharing options...
Ell20 Posted August 6, 2008 Author Share Posted August 6, 2008 It can be however you wish, but I dont quiet get what you have done in your code? How do I use this to get my desired result? Quote Link to comment Share on other sites More sharing options...
DarkWater Posted August 6, 2008 Share Posted August 6, 2008 After using my code: <?php //after that loop and your random number loop: foreach ($matched as $match) { printf('<tr class="match"><td>Match %d</td></tr>', $match); } foreach ($values as $value) { printf('<tr class="nomatchred"><td>Unmatched User Value %d</td></tr>', $value); } foreach ($nums as $num) { printf("<tr class="nomatchblue"><td>Unmatched Random Value %d</td></tr>', $value); } ?> You'll just need to create CSS classes named match, nomatchred, and nomatchblue. Based on your post, this is what you wanted. If not, just tell me and I'll try to fix it a bit. Quote Link to comment Share on other sites More sharing options...
Ell20 Posted August 6, 2008 Author Share Posted August 6, 2008 Hmm I dont understand. Ill try and explain it a bit better. I have a grid of numbers in a table: <td width="50" height="25" align="center" onMouseDown="changeto(event, '1')" id="num1" onMouseOver="this.style.cursor='pointer'">1</td> <td width="50" height="25" align="center" onMouseDown="changeto(event, '2')" id="num2" onMouseOver="this.style.cursor='pointer'">2</td> <td width="50" height="25" align="center" onMouseDown="changeto(event, '3')" id="num3" onMouseOver="this.style.cursor='pointer'">3</td> This goes all the way to 80. The user selects any 10 numbers of the 80 and presses submit - this is what is stored in $value1, $value2 etc. On submit 20 numbers need to be randomly generated from between 1 and 80, all 20 numbers must be different. Any numbers which match will turn bgcolor="purple" on the td. Any numbers which the user select which didnt match turn bgcolor="red" on the td. Any numbers which came up from the list of 20 but the user didnt guess turn bgcolor="blue" on the td. Hope this makes it a bit clearer. Quote Link to comment Share on other sites More sharing options...
DarkWater Posted August 6, 2008 Share Posted August 6, 2008 Okay, so just adapt what I did. All the numbers that matched are in $matched, all the numbers that the user picked but didn't match are in $values and all the random numbers picked but not matched are in $nums. It shouldn't be too hard to figure out. (I don't want to do the whole thing for you, I just got you started. ) Quote Link to comment Share on other sites More sharing options...
Ell20 Posted August 6, 2008 Author Share Posted August 6, 2008 Ok, im just confused as to what code I need and dont need now, do I still need the following code: $nums = array(); $rn = rand(1,80); for ($cnt = 1; $cnt < 21;$cnt++) { while (in_array($rn,$nums)) $rn = rand(1,80); $nums[$cnt] = $rn; } Quote Link to comment Share on other sites More sharing options...
DarkWater Posted August 6, 2008 Share Posted August 6, 2008 Yes. You can probably change that up to just be: $nums = array(); $rn = rand(1,80); for ($cnt = 1; $cnt < 21;$cnt++) { while (in_array($rn,$nums)) $rn = rand(1,80); $nums[] = $rn; } Quote Link to comment Share on other sites More sharing options...
Ell20 Posted August 6, 2008 Author Share Posted August 6, 2008 Ok, I dont really have a clue what im doing now but this is the error im getting when I submit: Warning: Invalid argument supplied for foreach() in /home/streety/public_html/keno.php on line 43 if (isset($_POST['submit'])) { $value1 = $_POST['value1']; $value2 = $_POST['value2']; $value3 = $_POST['value3']; $value4 = $_POST['value4']; $value5 = $_POST['value5']; $value6 = $_POST['value6']; $value7 = $_POST['value7']; $value8 = $_POST['value8']; $value9 = $_POST['value9']; $value10 = $_POST['value10']; $nums = array(); $rn = rand(1,80); for ($cnt = 1; $cnt < 21;$cnt++) { while (in_array($rn,$nums)) $rn = rand(1,80); $nums[] = $rn; } foreach ($values as $key=>$value) { //LINE 43 if (in_array($value, $nums)) { unset($values[$key]); $keyOfNum = array_search($value, $nums); unset($nums[$keyOfNum]); $matched[] = $value; } } } Quote Link to comment Share on other sites More sharing options...
DarkWater Posted August 6, 2008 Share Posted August 6, 2008 Make $values an array of user inputs instead of $value1, $value2, etc. Put them all into the $values[] array. EDIT: Btw, I had a slight syntax error on the post before with the 3 foreach loops and the printf() statements. The last one has " instead of '. Woops. Quote Link to comment Share on other sites More sharing options...
Ell20 Posted August 6, 2008 Author Share Posted August 6, 2008 Ok I think I am getting there: if (isset($_POST['submit'])) { $values = array($_POST['value1'],$_POST['value2'],$_POST['value3'],$_POST['value4'],$_POST['value5'],$_POST['value6'],$_POST['value7'],$_POST['value8'],$_POST['value9'],$_POST['value0']); $nums = array(); $rn = rand(1,80); for ($cnt = 1; $cnt < 21;$cnt++) { while (in_array($rn,$nums)) $rn = rand(1,80); $nums[] = $rn; } foreach ($values as $key=>$value) { if (in_array($value, $nums)) { unset($values[$key]); $keyOfNum = array_search($value, $nums); unset($nums[$keyOfNum]); $matched[] = $value; } } } However I dont want to add a new row, I simply want to change the colour of the existing TD, how would I go about doing that? Cheers Quote Link to comment Share on other sites More sharing options...
DarkWater Posted August 6, 2008 Share Posted August 6, 2008 Show the code where you echo out the cells please? Quote Link to comment Share on other sites More sharing options...
Ell20 Posted August 6, 2008 Author Share Posted August 6, 2008 Here is an example of 1 - 10: <tr> <td width="50" height="25" align="center" onMouseDown="changeto(event, '1')" id="num1" onMouseOver="this.style.cursor='pointer'">1</td> <td width="50" height="25" align="center" onMouseDown="changeto(event, '2')" id="num2" onMouseOver="this.style.cursor='pointer'">2</td> <td width="50" height="25" align="center" onMouseDown="changeto(event, '3')" id="num3" onMouseOver="this.style.cursor='pointer'">3</td> <td width="50" height="25" align="center" onMouseDown="changeto(event, '4')" id="num4" onMouseOver="this.style.cursor='pointer'">4</td> <td width="50" height="25" align="center" onMouseDown="changeto(event, '5')" id="num5" onMouseOver="this.style.cursor='pointer'">5</td> <td width="50" height="25" align="center" onMouseDown="changeto(event, '6')" id="num6" onMouseOver="this.style.cursor='pointer'">6</td> <td width="50" height="25" align="center" onMouseDown="changeto(event, '7')" id="num7" onMouseOver="this.style.cursor='pointer'">7</td> <td width="50" height="25" align="center" onMouseDown="changeto(event, '8')" id="num8" onMouseOver="this.style.cursor='pointer'">8</td> </tr> <tr> <td width="50" height="25" align="center" onMouseDown="changeto(event, '9')" id="num9" onMouseOver="this.style.cursor='pointer'">9</td> <td width="50" height="25" align="center" onMouseDown="changeto(event, '10')" id="num10" onMouseOver="this.style.cursor='pointer'">10</td> Quote Link to comment Share on other sites More sharing options...
Ell20 Posted August 6, 2008 Author Share Posted August 6, 2008 DarkWater is there anyway I can incorporate what you did into the current TD's but just change their background colour? Cheers Quote Link to comment Share on other sites More sharing options...
DarkWater Posted August 6, 2008 Share Posted August 6, 2008 Wait, did you hard code all of those? P.S: Sorry for not responding, I missed your post I guess. Quote Link to comment Share on other sites More sharing options...
Ell20 Posted August 6, 2008 Author Share Posted August 6, 2008 Yeah these are hardcoded? Quote Link to comment Share on other sites More sharing options...
DarkWater Posted August 6, 2008 Share Posted August 6, 2008 Why not just create a loop for making those? <?php echo '<tr>'; for ($row=1;$row<=80;$row++) { printf('<td width="50" height="25" align="center" onMouseDown="changeto(event, \'%1$d\')" id="num%1$d" onMouseOver="this.style.cursor=\'pointer\'">%1$d</td>', $row); if ($row % 8 == 0) { echo '</tr><tr>'; } } echo '</tr>'; ?> The color part should be relatively easy to incorporate. Quote Link to comment Share on other sites More sharing options...
Ell20 Posted August 6, 2008 Author Share Posted August 6, 2008 Thanks for the loop, tidies up the page somewhat. I dont get where you put the foreach loop as I want the colours to change on the already exsisting TD's. Getting late....early here now! 3:30am!!! Quote Link to comment Share on other sites More sharing options...
DarkWater Posted August 6, 2008 Share Posted August 6, 2008 Well you wouldn't use foreach in this case. I'll give you a bit of a hint because it's late. =P You'll need to do the other 2 colors: <?php echo '<tr>'; for ($row=1;$row<=80;$row++) { if (isset($_POST['submit'])) { //You don't want colors unless they actually submitted! if (in_array($row, $matched)) { $bgcolor = "purple"; } } else { $bgcolor = "white"; } printf('<td width="50" height="25" align="center" onMouseDown="changeto(event, \'%1$d\')" id="num%1$d" onMouseOver="this.style.cursor=\'pointer\'" bgcolor="%2$s">%1$d</td>\n', $row, $bgcolor); if ($row % 8 == 0) { echo '</tr><tr>'; } } echo '</tr>'; ?> The other two should just be 2 more if statements. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.