See if variable from a list box is IN a list?

I have a listbox in the follwing format (HTML):

<select class="select" name="passport" onchange="">
                        <option value="" selected>Select a country</option>
                        <option>-----------------------</option>
                        <option>Afghanistan</option>
                        <option>Albania</option>
                        <option>Algeria</option>
                        <option>American Samoa</option>
                                etc.

Now I have 5 categories of countries, example of category one below:

| Aruba | Australia | Austria | Belgium | Bermuda | Canada | Cayman Islands Channel Islands | Denmark | Finland | France | French Polynesia | Germany | Gibraltar Guam | Hong Kong | Iceland | Ireland | Italy | Japan | Kuwait | Liechtenstein | Luxembourg Monaco | Netherlands | Norway | Qatar | San Marino | Singapore | Sweden | Switzerland UAE | United Kingdom | United States | Vatican

What i need to do is see if what the user selected in the drop down box is in one of the categories? Is there an easier way to do it other than 100's of If statements, if so how?
AndriesKeunAsked:
Who is Participating?
 
Richard QuadlingConnect With a Mentor Senior Software DeveloperCommented:
Try this as the form handler.
<?php
error_reporting(E_ALL);

$categorya=array("Andorra","Aruba","Australia","Austria","Belgium","Bermuda","Canada","Cayman Islands Channel Islands","Denmark","Finland","France","French Polynesia","Germany","Gibraltar Guam","Hong Kong","Iceland","Ireland","Italy","Japan","Kuwait","Liechtenstein","Luxembourg Monaco","Netherlands","Norway","Qatar","San Marino","Singapore","Sweden","Switzerland UAE","United Kingdom","United States","Vatican");

echo '<pre>' . var_export($_GET, True) . var_export($_POST, True) . var_export($_COOKIE, True) . '</pre>';

$points = 50;
if (isset($_GET['passport']) && in_array($_GET['passport'], $categorya))
 {
 $points + 1000;
 }
if (isset($_POST['passport']) && in_array($_POST['passport'], $categorya))
 {
 $points + 1000;
 }

echo $points;
?>

What EXACTLY comes out?
0
 
dr_dedoCommented:
first, you sould add value to ur options e.g.
<option value="Afghanistan">Afghanistan</option>
this way, $_REQUEST['passport'] would return country name

then u can use a simple regex
if(preg_match ('/\b'.$_REQUEST['passport'].'\b/',$country_list_1)

you can loop through country lists with a for loop
you can have county lists as arrays and check using in_array() function
0
 
TeRReFCommented:
A regex for such a simple task is a bit overkill. Use in_array(), it's faster.
if(in_array($_REQUEST['passport'], $country_list_1))
0
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
TeRReFCommented:
Sorry dr_dedo, I hadn't seen your reference to in_array!
0
 
TeRReFCommented:
If it's a string, you should use strpos()...
if(strpos($country_list_1, $_REQUEST['passport']))
0
 
Richard QuadlingSenior Software DeveloperCommented:
I would use $_GET or $_POST for finer control over the data coming in.

From the PHP Manual ...

Note:
Introduced in 4.1.0. There is no equivalent array in earlier versions.

Note:
Prior to PHP 4.3.0, $_FILES information was also included in $_REQUEST.

An associative array consisting of the contents of $_GET, $_POST, and $_COOKIE.

This is a 'superglobal', or automatic global, variable. This simply means that it is available in all scopes throughout a script. You don't need to do a global $_REQUEST; to access it within functions or methods.




So, if you save a value in the cookie and have it on the form? Which one will you get? You can only have 1!!!!

By using the right superglobal, you are in control and not hoping the user (or a hacker) doesn't mess things up for you!

0
 
AndriesKeunAuthor Commented:
Its not working.

My Array:

$categorya=array("Andorra","Aruba","Australia","Austria","Belgium","Bermuda","Canada","Cayman Islands Channel Islands","Denmark","Finland","France","French Polynesia","Germany","Gibraltar Guam","Hong Kong","Iceland","Ireland","Italy","Japan","Kuwait","Liechtenstein","Luxembourg Monaco","Netherlands","Norway","Qatar","San Marino","Singapore","Sweden","Switzerland UAE","United Kingdom","United States","Vatican");

php:

$points = 50;
if(in_array($_GET['passport'], $categorya)) $points += 1000;

if(in_array($_POST['passport'], $categorya)) $points += 1000;

if(in_array($_REQUEST['passport'], $categorya)) $points += 1000;

if(strpos($categorya, $_GET['passport'])) $points += 1000;

if(strpos($categorya, $_POST['passport'])) $points += 1000;

if(strpos($categorya, $_REQUEST['passport'])) $points += 1000;

I email the result to me and only get 50 points.


0
 
AndriesKeunAuthor Commented:
Ah, think i spotted it This comes out:

array (
)array (
  'qualification' => 'None',
  'work' => 'None',
  'salary' => '',
  'passport' => '“United',
  'spouse' => 'No Degree / No Spouse Apply',
)array (
)

50

Passport should be "United Kingdom", its cut the last part off. But i set the value properly

                    <select class="select" name="passport" onchange="">
                              <option value="" selected>Select a country</option>
                              <option value="">-----------------------</option>
                              <option value=“United Kingdom“>United Kingdom</option>
                              <option value=“United States“>United States</option>
                              <option value=“South Africa“>South Africa</option>
                              <option value=“Australia“>Australia</option>
                              <option value=“New Zealand”>New Zealand</option>

know how to sort it?
0
 
Richard QuadlingSenior Software DeveloperCommented:
It looks like the " you are using is different sometimes.

If you do a view source on this page you will get ...

      Ah, think i spotted it This comes out:<br /><br />array (<br />)array (<br />&nbsp; 'qualification' =&gt; 'None',<br />&nbsp; 'work' =&gt; 'None',<br />&nbsp; 'salary' =&gt; '',<br />&nbsp; 'passport' =&gt; '“United',<br />&nbsp; 'spouse' =&gt; 'No Degree / No Spouse Apply',<br />)array (<br />)<br /><br />50<br /><br />Passport should be &quot;United Kingdom&quot;, its cut the last part off. But i set the value properly<br /><br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;select class=&quot;select&quot; name=&quot;passport&quot; onchange=&quot;&quot;&gt;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;option value=&quot;&quot; selected&gt;Select a country&lt;/option&gt;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;option value=&quot;&quot;&gt;-----------------------&lt;/option<wbr/>&gt;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;option value=“United Kingdom“&gt;United Kingdom&lt;/option&gt;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;option value=“United States“&gt;United States&lt;/option&gt;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;option value=“South Africa“&gt;South Africa&lt;/option&gt;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;option value=“Australia“&gt;Australia&lt;/option&gt;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;option value=“New Zealand”&gt;New Zealand&lt;/option&gt;<br /><br />know how to sort it?</div>


You will see that the " are shown as &quot; (which is correct) but you also have “ which is a different symbol.

Are you using something like Word or Frontpage?
0
 
AndriesKeunAuthor Commented:
using editplus, but copied it from word. will change them all and see if it works. I notice now that they are looking different.
0
 
Richard QuadlingSenior Software DeveloperCommented:
Word uses an auto formatter to make quotes look nice (opening and closing quotes). Word has no place in writing PHP code. Completely the wrong tool.
0
 
AndriesKeunAuthor Commented:
Yes I know, the the list of 200+ countries and areas were already in a word document, thought it would be easier to just do the necesaries there and then just copy over, but prob should just have done it the other way, just me being lazy :)
0
 
Richard QuadlingSenior Software DeveloperCommented:
Ha!

Did that all work then?
0
 
AndriesKeunAuthor Commented:
nice one bro, cheers
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.