Note that there are some explanatory texts on larger screens.

plurals
  1. POCheck through an array and identify matching numbers
    primarykey
    data
    text
    <p>I am working on a scratch card game where by a user purchases a scratch card from me/my store and can then redeem said card by scratching off numbers and matching 3 pictures to win a prize</p> <p>Like physical scratch cards I want the prizes to be pre configured so that I know when a prize will be won, but obviously the users dont, this is so I can limit the prizes, so it is not true random, but will be random in terms of who wins the prize..</p> <p>I know how I will setup the prizes but I need to check each data value in an array to ensure that there is not 3 matching numbers, unless they have actually won..</p> <p>So far I have this (I will neaten it up and shorten the code down soon)</p> <pre><code>$numbers = array( 0 =&gt; rand(0,9), 1 =&gt; rand(0,9), 2 =&gt; rand(0,9), 3 =&gt; rand(0,9), 4 =&gt; rand(0,9), 5 =&gt; rand(0,9), 6 =&gt; rand(0,9), 7 =&gt; rand(0,9), 8 =&gt; rand(0,9) ); </code></pre> <p>Which just randomly generates 9 numbers and </p> <pre><code>echo "&lt;table&gt;"; $i=0; for($x=0;$x&lt;3;$x++){ echo "&lt;tr&gt;"; for($y=0;$y&lt;3;$y++){ echo "&lt;td&gt;" . $numbers[$i] . "&lt;/td&gt;"; $i++; } echo "&lt;/tr&gt;"; } </code></pre> <p>Sorts them into a 3x3 table</p> <p>I know there is the command in_array() which will sort through the array looking for certain values but I dont want to run an endless loop checking each value to see if I get a match, because it's labor intensive and would be messy as well.</p> <p>I'm looking for a suggestion anyone might have so I can sort through the array and see if any 3 numbers exist, if they do, and they're not suppost to (ie the user hasn't actually won a prize) then one should be changed to a new number.</p> <p>So thank you in advance for your help guys I look forward to your suggestions</p> <p>Luke</p> <p><em><strong></em>**<em>*</em>*</strong> EDIT <strong><em>*</em>*</strong></p> <p>+More info</p> <p>To pick the winners I will use a database query such as this</p> <pre><code>$stock = $conn -&gt; query("SELECT DISTINCT * FROM codes WHERE Available = 1 AND 0 = FLOOR(RAND()*Chance) ORDER BY RAND()"); </code></pre> <p>Each code will have a set chance to win and if it matches up it will be shown, depending on the code won a certain prize will show </p> <pre><code>$stock = $conn -&gt; query("SELECT DISTINCT * FROM codes WHERE Available = 1 AND Win = 1); </code></pre> <p>'Win' will be a table in the database which each time someone plays the game it goes down by 1</p> <p>So each code will have the table Win which when it hits 1 it will pop out the in the next game</p> <p>These are two ways I can think of doing it which I think will work, both ways roughly allow me to control when and if someone wins but the second solution is obviously a more accurate way of doing it </p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload