...

View Full Version : Retrieving data from DB based on Dropdown items



havish
10-20-2012, 01:26 PM
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?

bjarneo
10-20-2012, 03:57 PM
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.

havish
10-20-2012, 05:31 PM
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

davidjones1990
10-20-2012, 07:35 PM
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:



'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

rgEffects
10-20-2012, 07:59 PM
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 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:


<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.

havish
10-21-2012, 04:14 AM
Thank you everyone for the replies.. Now i get an idea how to implement it..:)



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum