Determine selected element number

Posted on 2011-10-19
Last Modified: 2012-05-12
Hi All,

I have a dropdown box on a form that lists countries:

$lines =file('./Countries.csv');
foreach($lines as $data) {
   list($countriesAr[],$Continent[],$Notify[]) = explode(',',$data);

for($i=0;$i<count($countriesAr);$i++) {
     echo "<option value='".$countriesAr[$i]."'";
     if($_POST['country'] ==  $countriesAr[$i]) 
          echo "SELECTED";
     echo ">".$countriesAr[$i]."</option>\n";

Open in new window

My question is, when the form get submitted, I want to know which country was chosen, i.e. the first on the list, or 12th, etc. I need to know the element number. How do I get that value?

Question by:ragnarok89
    LVL 8

    Accepted Solution

    Hi ragnarok89,

    You first need to know the selected item, then get the item index from the array. It will be something like this:

    $selected_item = $_POST['select'];
    $index_of_value = array_search($selected_item, $countriesAr);

    Open in new window

    NOTE that the indexing is start form 0 the means the first element will be 0, the second element will be 1 ...etc.
    LVL 107

    Expert Comment

    by:Ray Paseur
    Just curious - why does the number of the selection matter?  I cannot think of any design pattern that would work that way.  It's potentially very brittle - what if the number or order of the countries changed?  If you have code that depends on a numeric position in an array from an external source (like $_POST) you may be accepting too much risk.  Just a thought.
    LVL 8

    Author Closing Comment


       Worked like a charm; thank you.

    LVL 8

    Expert Comment

    by:Ahmed Merghani
    You are welcome

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Threat Intelligence Starter Resources

    Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

    Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
    Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
    Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
    Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

    760 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    10 Experts available now in Live!

    Get 1:1 Help Now