Skip to content
View in the app

A better way to browse. Learn more.

Web Designer Forum

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Display data dynamically based on dropdown menu selection

Featured Replies

Hey, need help from some php Ninja's.


I have some members details set-up in a database where a plugin has done the easy part and created a table and id for members. I now need to insert a drop down menu on each members page which will dynamically display their price based on the selected location. So the member id as well as location field/values will need to be taken into consideration.


Thanks in advance.
  • Author

Thanks

 

Okay, see if I can make it more clear. My database has workers who charge a different price based on locations. When a user goes to the workers profile page, i want them to be able to select a location from the drop-down menu and then below the drop-down list display a predefined price stored for that worker.

 

So far, using the below I've manage to create a drop down list with all the column names from the table, but I only want the columns with names of locations. How do i choose the columns to display in the drop down? And then how do I output the stored value, for that column, for a specific worker when its selected from the drop down list?

 

<?php
$dbhost = 'localhost';
$dbname ='database';
$dbuser = 'root';
$dbpass = 'pass';


mysql_connect($dbhost, $dbuser, $dbpass) or die(mysql_error());
mysql_select_db($dbname) or die(mysql_error());

?>


<select name="selectname">

<?php
$result = mysql_query("SHOW COLUMNS FROM my_database")
or die(mysql_error());

while($nt=mysql_fetch_row($result)){
echo "<option value=" . $nt[0] . ">" . $nt[0] . "</option>";
}

?>

</select>

You'll need to use a SELECT statement to get the stored values. There are a number of ways you can filter which columns are selected, for example you may want to set up a view, but it would help to see the table structure. Do you have more than 1 table e.g. one for members and one for locations? Also is a member the same as a worker?

<select name="selectname">

<?php
$sql = "SELECT * FROM my_database";
$result = mysql_query($sql)or die(mysql_error()); 

while($row=mysql_fetch_assoc($result)){ 
echo "<option value=" . $row['fieldname'] . ">" . $row['otherfield'] . "</option>"; 
} 
?>
</select>

 

That should work. (Not tested but will do the trick).

 

 

 

 

Edited by Tex0gen

 

<select name="selectname">

<?php
$sql = "SELECT * FROM my_database";
$result = mysql_query($sql)or die(mysql_error()); 

while($row=mysql_fetch_assoc($result)){ 
echo "<option value=" . $row['fieldname'] . ">" . $row['otherfield'] . "</option>"; 
} 
?>
</select>

 

That should work. (Not tested but will do the trick).

 

 

 

 

 

there is a few issues with ur code for one

 

where it says my_database that is where a table name goes not a database name and 2 i would only select the 2 fields he needs not select from the entire table heres a revised version

 

<?php
$sql = "SELECT `fieldname`,`otherfield` FROM `my_table`";
$result = mysql_query($sql)or die(mysql_error()); 

while($row=mysql_fetch_assoc($result)){ 
echo "<option value=" . $row['fieldname'] . ">" . $row['otherfield'] . "</option>"; 
} 
?>

id also suggest not to use mysql but instead use mysqli or PDO

there is a few issues with ur code for one

 

where it says my_database that is where a table name goes not a database name and 2 i would only select the 2 fields he needs not select from the entire table heres a revised version

 

<?php
$sql = "SELECT `fieldname`,`otherfield` FROM `my_table`";
$result = mysql_query($sql)or die(mysql_error()); 

while($row=mysql_fetch_assoc($result)){ 
echo "<option value=" . $row['fieldname'] . ">" . $row['otherfield'] . "</option>"; 
} 
?>

id also suggest not to use mysql but instead use mysqli or PDO

Missed the db in there rather than table.

Why mysqli or PDO rather than mysql? (Sorry to thread hi-jack for 2 seconds)

The mysql_ set of functions have been deprecated in newer versions of PHP, so you're better off using newer methods such as mysqli or PDO. Have a look at this: http://net.tutsplus.com/tutorials/php/pdo-vs-mysqli-which-should-you-use/. It may be useful.

Sorta off subject but ur looking very stunning Lyndsey in ur pink and white dress lol just noticed ur new profile pic haha

Create an account or sign in to comment

Account

Navigation

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.