Page 1 of 1

how to check for multiple occurences of a word??

Posted: Fri Apr 19, 2002 9:05 am
by Craig
Hi, I'm at the beginning of try to write a little program to allow people to choose a team - it takes into account - name, position, value and country.

I've just started playing with sessions and stuff, the choices come from a MySql database, and I'm using a different page for each selection and carrying each variable forward with session_register() ideally once I have the selection I'll be putting into another table in the MySql database. First I have to check things out - eg the value of the selection - thats not a problem I'm simply using

Code: Select all

if ($total > "150"){
	die ("woah your selection cost to much");
}
BUT and this is my big problem, I'd like to limit people to selecting on a maximum of 3 from a particular country so by the time I'm done I'll have 11 variables for each county selection like so

$firstcountry
$secondcountry
$thirdcountry
$fourthcountry
$fifthcountry

and so on.

What is the best way to check out the values and number of occurences, like if china appears more than 3 times it would throw up an error message telling you that you have too many from the one country?

I haven't got a clue where to begin :?

Posted: Fri Apr 19, 2002 9:38 am
by jason
Hrmm...not quite sure I understand what you are looking to do, but I can tell you that this:

Code: Select all

$firstcountry 
$secondcountry 
$thirdcountry 
$fourthcountry 
$fifthcountry
Would be better written as an array, like this:

Code: Select all

$countries = array("USA", "Brazil", "Canada", "New Jersey");
Then you could use the PHP function array_count_values() to determine how many of each a person has chosen.

Hope this helps.

Posted: Fri Apr 19, 2002 9:51 am
by Craig
ok, lets assume the info is going into an array then:

taking the example from the manual -

Code: Select all

$countries = array("USA", "Brazil", "USA", "Canada", "USA", "USA", "New Jersey");
print_r(array_count_values ($countries));
would produce a count of

Code: Select all

Array
(
    їUSA] => 4
    їBrazil] => 1
    їCanada] => 1
    їNew Jersey] => 1
)
USA appears 4 times. I'm looking to limit that to a maximum of three times for each country, so if it appears 4 times that's not allowed and you would have to make another choice.

Posted: Fri Apr 19, 2002 10:00 am
by jason
Yup, that is how it would work, pretty much. Questions?

Posted: Fri Apr 19, 2002 10:13 am
by Craig
no doubt I will, just not yet ;)

stay sharp, I'll likely be back in a few hours :)

stuck?!?

Posted: Sun Apr 21, 2002 3:57 pm
by Craig
How do I query this to see if ANY value appear more than 3 times? I can't find an example of how to do it?

It's all well and good that I can print the total, but if a country appears 4 or more times I want to throw up an error.

8O

Posted: Sun Apr 21, 2002 7:00 pm
by jason
Just loop through the array, and test to see if each value is 3 or below. If not, you print an error. Something like this:

Code: Select all

foreach ( $array as $key => $value )
{
 if ( $value > 3 )
 {
  $error = true;
 }
}

if ( $error )
{
 die("There is an error.");
}