Jump to content

[SOLVED] Cant get my head round this!


Ell20

Recommended Posts

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

Link to comment
Share on other sites

$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

Link to comment
Share on other sites

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.

 

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

 

Link to comment
Share on other sites

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. ;) )

Link to comment
Share on other sites

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;
}

Link to comment
Share on other sites

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;
         }
}
}

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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>

Link to comment
Share on other sites

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. ;)

Link to comment
Share on other sites

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!!!

Link to comment
Share on other sites

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. ;)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.