Jump to content

ok need some help with 2 arrays in 1


Monkuar

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/255716-ok-need-some-help-with-2-arrays-in-1/
Share on other sites

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

$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?

 

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?

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';
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.