Monkuar Posted January 25, 2012 Share Posted January 25, 2012 I have ticket's for my lottery system. Somone chooses 3 random numbers out of 36, it will show 1|20|30 but now I want to beable to have my Members BUY multiple Tickets! So then I added a comma between the 3 arrays So the code will look like this when they select the balls they want 1|20|30,20|10|5 /etc /etc But now I have 1 problem. How do I go about validating the arrays for each COMMA inside the | ARRAY? Cause let's saY I don't want anyone to Submit a ball number higher then "36" how would I go about making it so it checks through each array and if it's higher then 32 I can give them a Error? Also, I each Ticket costs (5 Forum Gold) so the above code would be a total of 10 Forum Gold, because they're "2" Tickets being bought, how would I go about making a $counter++ in the arrays to count only the "," so I can tell how much Gold the member needs to have before he purchases Thanks Quote Link to comment https://forums.phpfreaks.com/topic/255716-ok-need-some-help-with-2-arrays-in-1/ Share on other sites More sharing options...
nine7ySix Posted January 25, 2012 Share Posted January 25, 2012 In PHP, the explode function is able to break a string into multiple array, it looks for a delimiter, in your case, the comma, and creates an array for each string inside the comma (<-- Badly worded :S) http://php.net/manual/en/function.explode.php So essentially, <?php $numbers = "1|20|30,20|10|5"; $numArray = explode("," $numbers); //$numArray[0] = "1|20|30"; //$numArray[1] = "20|10|5"; //To further break the array, you would do foreach($numArray as $numList) { $nums = explode("|", $numList); foreach($nums as $num) { //Validate each $num } } Quote Link to comment https://forums.phpfreaks.com/topic/255716-ok-need-some-help-with-2-arrays-in-1/#findComment-1310883 Share on other sites More sharing options...
Pikachu2000 Posted January 25, 2012 Share Posted January 25, 2012 A multidimensional array would be a much better option for this. Quote Link to comment https://forums.phpfreaks.com/topic/255716-ok-need-some-help-with-2-arrays-in-1/#findComment-1310895 Share on other sites More sharing options...
Monkuar Posted January 25, 2012 Author Share Posted January 25, 2012 $numbers = "{$ibforums->input['balls']}"; $numArray = explode(",", $numbers); //$numArray[0] = "1|20|30"; //$numArray[1] = "20|10|5"; //To further break the array, you would do foreach($numArray as $numList) { $nums = explode("|", $numList); foreach($nums as $num) { $count++; //Validate each $num if ($num > 36){ $std->Error2("Your Lottery Balls cannot be higher then 36, stop trying to be nawty."); } } echo $count; exit; } Ok so the input for this one is: 1|7|13,14|8|2,9|3|10,4|5|11,16|15|14 Let's count that 1 2 3 4 5 the $count outputs "3" any idea? Quote Link to comment https://forums.phpfreaks.com/topic/255716-ok-need-some-help-with-2-arrays-in-1/#findComment-1310914 Share on other sites More sharing options...
Monkuar Posted January 25, 2012 Author Share Posted January 25, 2012 Man I feel bad i added $count = count($numArray); now it works. Shows 7. =] Now I almost finished! But now how do I only accept X|X|X ? Because I dont want users to submit cheating/hacking material through Tamper data? Quote Link to comment https://forums.phpfreaks.com/topic/255716-ok-need-some-help-with-2-arrays-in-1/#findComment-1310915 Share on other sites More sharing options...
codebyren Posted January 25, 2012 Share Posted January 25, 2012 There are a lot of approaches you could take to validating the data. Something like this should do: <?php $entry = "20|10|5"; $errors = 0; $numbers = explode("|", $entry); foreach($numbers as $number) { $number = (int) $number; # cast the value as an integer if ($number < 1 OR $number > 36) $errors++; } if ($errors > 0 OR count($numbers) < 3) { echo 'No. Bad.'; } else echo 'Looking good'; ?> Quote Link to comment https://forums.phpfreaks.com/topic/255716-ok-need-some-help-with-2-arrays-in-1/#findComment-1310924 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.