chris57828 Posted May 22, 2011 Share Posted May 22, 2011 Hi, I am new to programming hence why I am unfamiliar with built in PHP functions and my code is rather unsophisticated, however, I am attempting to write a small ‘Lucky Lotto’ program which involves me predetermining the winning numbers manually via the array $draw. Via display() a user can decide how many ‘attempts’ they want. I want to be able to ascertain how many winning numbers each draw has (if any) so I have constructed a rather long IF function whereby the results are ‘exploded’ into an array namely $temp or $temp1 then via count() or sizeof() I should be able to determine the results. The problem I am having is that the arrays I am ‘exploding’ to namely $temp and $temp1 only ever appear to hold one element at position zero, which is obviously contrary to what arrays are for. By looking at the code below can someone show me in laymen’s terms how I can get $temp and $temp1 to hold and output more than one element, thanks! <?php class Game { public $temp; public $temp1; public $result; public $result1; public $draw = array(15,11,27,9,5,25); public $numbers = array(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30, 31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49); public $age; public $count; function __construct($sub1, $sub2) { $this->name = $sub1; $this->age = $sub2; return $this->name;age; } function checkAge() { if ($this->age <18) { return "Sorry $this->name, you are too young to gamble"; } else { return "Hi $this->name welcome to Lucky Lotto Dip, how many draws would you like?"; } } function display($sub3) { shuffle ($this->numbers); $this->count = $sub3; echo "<pre>"; echo "*********************"; echo "<br />"; echo "* Lucky Lotto *"; echo "<br />"; echo "*********************"; while ($this->count >0) { echo "<br />"; echo "* "; for ($j =0; $j <6; $j++) // For loop if ($this->numbers[$j] <10) { echo " " . $this->numbers[$j] . " "; if ($this->numbers[$j] == $this->draw[0] or $this->numbers[$j] == $this->draw[1] or $this->numbers[$j] == $this->draw[2] or $this->numbers[$j] == $this->draw[3] or $this->numbers[$j] == $this->draw[4] or $this->numbers[$j] == $this->draw[5] ) { $temp = explode(' ', $this->numbers[$j]); for ($a =0; $a <$temp.length; $j++); } } else { echo $this->numbers[$j] . " "; if ($this->numbers[$j] == $this->draw[0] or $this->numbers[$j] == $this->draw[1] or $this->numbers[$j] == $this->draw[2] or $this->numbers[$j] == $this->draw[3] or $this->numbers[$j] == $this->draw[4] or $this->numbers[$j] == $this->draw[5] ) { $temp1 = explode(' ', $this->numbers[$j]); for ($a =0; $a <$temp1.length; $j++); } } echo "* "; echo "<br />"; echo "<br />"; echo $temp[$a]; echo "<br />"; echo $temp1[$a]; echo "<br />"; echo "*********************"; echo "<br />"; echo " "; echo "<br />"; echo "*********************"; $this->count --; shuffle ($this->numbers); $temp1= 0; $temp = 0; } } } $object2 = new Game("Chris", 36); echo $object2->checkAge(); echo $object2->display(3); ?> Quote Link to comment https://forums.phpfreaks.com/topic/237109-exploding-arrays/ Share on other sites More sharing options...
wildteen88 Posted May 22, 2011 Share Posted May 22, 2011 Did you not try my code from your previous post http://www.phpfreaks.com/forums/index.php?topic=333081.msg1568706#msg1568706 Quote Link to comment https://forums.phpfreaks.com/topic/237109-exploding-arrays/#findComment-1218665 Share on other sites More sharing options...
chris57828 Posted May 22, 2011 Author Share Posted May 22, 2011 Hi, yeah I did see your reply last time, sorry for not thanking you, only problem is your code is quite different from mine, and at present because I am learning I don’t want to simply cut and paste someone else’s code. Since I started this thread I have learnt that the explode() is for strings only and not numbers but there is a workaround. Do you know how I could explode whole numbers into an array, but for instance if the numbers are 1, 15, 25, 28, I want them placed in an array in that format, not splitting them up into single integers? Quote Link to comment https://forums.phpfreaks.com/topic/237109-exploding-arrays/#findComment-1218734 Share on other sites More sharing options...
wildteen88 Posted May 22, 2011 Share Posted May 22, 2011 only problem is your code is quite different from mine, and at present because I am learning I don’t want to simply cut and paste someone else’s code That fine. I just through you may have forgotten about your previous post as you didn't reply to it last time. I want them placed in an array in that format, not splitting them up into single integers? If you want to add a string of numbers, eg $numbers = '1, 3, 18, 27'; And you want to add them into an array you can simply do $array[] = $numbers; That will add the string of numbers to the array. Quote Link to comment https://forums.phpfreaks.com/topic/237109-exploding-arrays/#findComment-1218739 Share on other sites More sharing options...
Pikachu2000 Posted May 22, 2011 Share Posted May 22, 2011 What do you mean explode() is not for numbers? If the numbers are in a variable like this: $var = '1, 2, 3, 4, 5, 6, 7, 8, 9, 0'; then you have a string of numeric characters that can be explode()d into an array. Quote Link to comment https://forums.phpfreaks.com/topic/237109-exploding-arrays/#findComment-1218742 Share on other sites More sharing options...
chris57828 Posted May 22, 2011 Author Share Posted May 22, 2011 If that’s the case why does the code in my initial post not work, only one element is ever displayed? Quote Link to comment https://forums.phpfreaks.com/topic/237109-exploding-arrays/#findComment-1218745 Share on other sites More sharing options...
wildteen88 Posted May 22, 2011 Share Posted May 22, 2011 The only places you use explode on are lines 58 and 69 58. $temp = explode(' ', $this->numbers[$j]); 59. for ($a =0; $a <$temp.length; $j++); ... 69. $temp1 = explode(' ', $this->numbers[$j]); 70. for ($a =0; $a <$temp1.length; $j++); On lines 58 and 69 you use explode to get the individual numbers from $this->numbers[$j]. But you are not doing anything with them afterwards (lines 59 and 70). The for loop is not constructed incorrectly. Variables do not have properties such as .length. Quote Link to comment https://forums.phpfreaks.com/topic/237109-exploding-arrays/#findComment-1218751 Share on other sites More sharing options...
chris57828 Posted May 22, 2011 Author Share Posted May 22, 2011 Hold on, I'll have a look! :'( Quote Link to comment https://forums.phpfreaks.com/topic/237109-exploding-arrays/#findComment-1218765 Share on other sites More sharing options...
chris57828 Posted May 23, 2011 Author Share Posted May 23, 2011 Thanks wildteen88, I have replaced the 'for' loop with a 'for..each' loop, and it seems to be working, when I have finished I'll post the code Quote Link to comment https://forums.phpfreaks.com/topic/237109-exploding-arrays/#findComment-1218981 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.