Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    New Coder
    Join Date
    Jul 2012
    Posts
    26
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Retrieving data from DB based on Dropdown items

    I need a general idea of how to implement this.. I have a table in MySQL database named xyz which has field named name, class.. Now what I want to do is when i select the class in the dropdown list, the particular class details should populate.
    Eg..
    <select name="users">
    <option value="">Select a class:</option>
    <option value="1">5th Standard</option>
    <option value="2">6th Standard </option>
    <option value="3">7th standard</option>
    </select>
    So when i select 5th standard, all details pertaining to 5th standard should be fetched. and similarly for 6th and 7th. I have gone through w3schools tut but could not understand. Do I need to write separate query for each drop down item?

  • #2
    New Coder
    Join Date
    May 2012
    Posts
    50
    Thanks
    6
    Thanked 4 Times in 4 Posts
    Check out PHP PDO: http://workbench.no/blog/php-pdo/

    And you don't need to write separate query for each drop down item.

    Read about queries.
    Last edited by bjarneo; 10-20-2012 at 03:59 PM.

  • #3
    New Coder
    Join Date
    Jul 2012
    Posts
    26
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by bjarneo View Post
    Check out PHP PDO: http://workbench.no/blog/php-pdo/

    And you don't need to write separate query for each drop down item.

    Read about queries.
    thanks will check that out

  • #4
    New Coder
    Join Date
    Sep 2011
    Posts
    66
    Thanks
    0
    Thanked 7 Times in 7 Posts
    At the moment '1' will be submitted if the user chooses 5th standard and so on, so im assuming this is the value of the ID column in your database.

    Your query should look something like this:

    PHP Code:
    'SELECT * FROM xyz WHERE id = ' $_POST['users'
    Yeh I agree with bjarneo, using PDOs is a good habit to get in to, although knowledge if OOP is required.

    Hope that helped

  • #5
    Regular Coder
    Join Date
    Aug 2012
    Posts
    142
    Thanks
    39
    Thanked 3 Times in 3 Posts
    If the values already exist in the database then you could create a recordset then use repeat region like this:

    The Query for classRS recordset:
    PHP Code:
    <?php mysql_select_db($database_xyzDB$xyzDB);
    $query_classRS "SELECT xyzTable.* FROM xyzTable ";
    $classRS mysql_query($query_classRS$xyzDB) or die(mysql_error());
    $row_classRS mysql_fetch_assoc($classRS);
    $totalRows_classRS mysql_num_rows($classRS); ?>
    The list and repeat region:
    PHP Code:
     <select name="classID" id="class">
              <?php do { ?>
        <option value="<?php echo $row_class['id']; ?>
             <?php if (!(strcmp($row_classRS['id'], $row_classRS['folderLogID']))) 
                {echo 
    "selected=\"selected\"";} ?>>
                    <?php echo $row_classRS['name'], " - "$row_classRS['class']; ?>
           <?php }  while ($row_classRS mysql_fetch_assoc($classRS)); ?>
        </option>
     </select>
    This should give you an HTML list with every class in your database that returns the name and class in the pull down menu then sends the ID of the class to the form on submit.

    I didn't check for typo's but I use this basic form to generate pull down menu's for multiple database fields so that I don't end up having to generate the list of options by hand. Some of my lists contain thousands of possibilities that I can limit by if statements using another cell in the table. For example, if you had 20 chemistry classes and 50 math classes you could add a column to your table for classType then use an IF ($row_classRS['classType'] == chemistry) method in the PHP or a WHERE classType == 'chemistry' in the SQL to limit the List Menu to chemistry classes.

    I hope this helps and that I understood what you were trying to do.

  • #6
    New Coder
    Join Date
    Jul 2012
    Posts
    26
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Thank you everyone for the replies.. Now i get an idea how to implement it..


  •  

    Posting Permissions

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