SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Zealot
    Join Date
    Aug 2009
    Posts
    125
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Array Problem (dropdown)

    Hi All,

    I'm trying to get a value from a drop down; change it to a db column name and use that later down the script for a search query.

    The problem I'm having is how to accomplish this. I've tried different codes but I'm not successful with this perhaps simple array issue.

    Can you help me fill in the gaps with my code? I post both the php and html below. Thanks in advance!

    php code:
    PHP Code:
    $dropdown mysqli_real_escape_string($link$_GET['dropdown']); 
        {
             
    $dropdown[] = array('1' => 'car''2' => 'bike''3' => 'truck');
        }
    ...

    $result mysqli_query($link"SELECT * FROM vehicles WHERE $dropdown LIKE 'green'"); 
    html:
    HTML Code:
    <select name="dropdown">
    		<option value="1">1</option>
    		<option value="2">2</option>
    		<option value="3">3</option>
    		</select>

  2. #2
    Non-Member
    Join Date
    Oct 2009
    Posts
    1,852
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You've almost did it
    Only need to learn to give variables different names

    PHP Code:
    $dropdown $_GET['dropdown']; 
    $drop_var = array('1' => 'car''2' => 'bike''3' => 'truck');

    if isset(
    $drop_var[$dropdown]) $field=$drop_var[$dropdown]
    else die (
    "wrong choice");

    $result mysqli_query($link"SELECT * FROM vehicles WHERE $field LIKE 'green'"); 

  3. #3
    SitePoint Zealot
    Join Date
    Aug 2009
    Posts
    125
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks, Shrapnel_N5,

    It worked! - though I had to make a couple of adjustments:

    PHP Code:
    if (isset($drop_var[$dropdown])) 
            {
                
    $field $drop_var[$dropdown];
            } 
    Thanks!


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •