March 12, 201313 yr 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.
March 13, 201313 yr 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>
March 13, 201313 yr 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?
March 13, 201313 yr <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 March 13, 201313 yr by Tex0gen
March 13, 201313 yr <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
March 13, 201313 yr 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)
March 13, 201313 yr 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.
March 13, 201313 yr 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
March 13, 201313 yr Lol, where's the embarrassed smiley when I need it? Thank you. lol i know right and your welcome
Create an account or sign in to comment