Paul-D Posted July 16 Share Posted July 16 (edited) Hi I have a lottery viewer which works. What I want to do is check if a number is a match and change the colour of the ball. Right now it just returns 0 or 1. I thought black 000000 normal and blue 0000ff. Is there a way of deleting the code section. Spelling mistake $Colour? Tried to delete. ## Page <?php $found = GetColur($MyBalls,$rsLottoList['N1']); ?> <td style="text-align:right"><?=$rsLottoList['N1']?></td> ## Code function GetColur($Balls,$Ball) { $found = 0; for($a = 0;$a <5; $a++) { if($Balls[$a] == $Ball) $found = 1; } return $found; } Edited July 16 by Paul-D Quote Link to comment https://forums.phpfreaks.com/topic/322402-passing-a-colour-value-back-and-using-it/ Share on other sites More sharing options...
Barand Posted July 16 Share Posted July 16 Alternative approach... <?php $winners = [ 'norm' => [1,23,27,36,48], 'stars' => [8,12] ]; $myNums = [ 'norm' => [1,3,27,38,48], 'stars' => [7,12] ]; $normCorrect = count(array_intersect($winners['norm'], $myNums['norm'])); $starsCorrect = count(array_intersect($winners['stars'], $myNums['stars'])); $results = sprintf ("\n%-15s%3d\n%-15s%3d", 'Normal', $normCorrect, 'Lucky Stars', $starsCorrect); $balls = ''; foreach ($myNums['norm'] as $b) { $bno = str_pad($b, 2, '0', STR_PAD_LEFT); $cls = (in_array($b, $winners['norm'])) ? 'match' : 'nomatch'; $balls .= "<div class='ball $cls'>$bno</div>"; } $balls .= '<br>'; foreach ($myNums['stars'] as $b) { $bno = str_pad($b, 2, '0', STR_PAD_LEFT); $cls = (in_array($b, $winners['stars'])) ? 'matchstar' : 'nomatchstar'; $balls .= "<div class='ball $cls'>$bno</div>"; } ?> <!DOCTYPE html> <html lang="en"> <head> <title>Example</title> <meta charset="utf-8"> <style type='text/css'> body { font-family: verdana, arial, sans-serif; font-size: 12pt; } .ball { width: 40px; height: 40px; clip-path: circle(40%); text-align: center; display: inline-block; padding-top: 18px; margin: 5px; } .nomatch { background-color: #000000; color: #FFFFFF; } .nomatchstar { background-color: #808080; color: #FFFFFF; } .match { background-color: #006EFC; color: #FFFFFF; } .matchstar { background-color: #EEEE18; color: #000000; } </style> </head> <body> <div> <h3>Results</h3> <pre> <?= $results ?> </pre> <?= $balls ?> </div> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/322402-passing-a-colour-value-back-and-using-it/#findComment-1630215 Share on other sites More sharing options...
Paul-D Posted July 16 Author Share Posted July 16 (edited) I realise that there are other ways of doing this but I can't rip it up now and start again. I have a system which has worked for years. I need to work out why I am not getting a match in... I have tested the values in the function and they are correct. I am getting back #c53ba1 in all the returns. function GetColour($Balls,$Ball) { $found = "#c53ba1"; for($a = 0;$a <5; $a++) { if($Balls[$a] == $Ball) $found == "#0000ff"; } return $found; } Edited July 16 by Paul-D Quote Link to comment https://forums.phpfreaks.com/topic/322402-passing-a-colour-value-back-and-using-it/#findComment-1630218 Share on other sites More sharing options...
Barand Posted July 16 Share Posted July 16 The assignment operator is "=", not "==". Instead of the for() loop, why not just use in_array() as I did? function GetColour($Balls,$Ball) { return in_array($Ball, $Balls) ? "#0000ff" : "#c53ba1"; } 1 Quote Link to comment https://forums.phpfreaks.com/topic/322402-passing-a-colour-value-back-and-using-it/#findComment-1630220 Share on other sites More sharing options...
Paul-D Posted July 16 Author Share Posted July 16 (edited) Thanks. Stupid mistake to make. Edited July 16 by Paul-D Quote Link to comment https://forums.phpfreaks.com/topic/322402-passing-a-colour-value-back-and-using-it/#findComment-1630221 Share on other sites More sharing options...
jodunno Posted July 20 Share Posted July 20 On 7/16/2024 at 6:01 PM, Paul-D said: Thanks. Stupid mistake to make. common mistake, actually. Happens to everyone somewhere along the line. However, ignoring Barand's advice is probably more of a stupid mistake because it is more efficient and logical. function GetColour($Balls, $Ball) { return in_array($Ball, $Balls) ? '#0000ff': '#c53ba1'; } echo GetColour([01, 03, 27, 38, 48, 07, 12], 27) . ' = 27<br>'; echo GetColour([01, 03, 27, 38, 48, 07, 12], 73) . ' = 73'; sorry, folks: As an American born person, I prefer the stars and stripes spelling of color[-s] over the French spelling. It drives me nuts sometimes. I can excuse the British Barand but knock it off already. 🙂 LOL Honestly. Barand is a pro and that is some good pro advice for you to follow. Quote Link to comment https://forums.phpfreaks.com/topic/322402-passing-a-colour-value-back-and-using-it/#findComment-1630495 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.