php - Show multiple values from a row when dropdown box is used from mysql database -
i'm trying show specific item in row when item in dropdown list selected. clarify, lets have item1
chosen in dropdown menu, , when item1
chosen, want price item1
shown in field on page.
ps. i'm trying make inventory , ordering form connected mysql database.
thanks in advance.
here php code.
<?php function dropdown( $alcohol, array $options, $selected=null ) { /*** begin select ***/ $dropdown = '<select name="'.$alcohol.'" id="'.$alcohol.'">'."\n"; $selected = $selected; /*** loop on options ***/ foreach( $options $key=>$option ) { /*** assign selected value ***/ $select = $selected==$key ? ' selected' : null; /*** add each option dropdown ***/ $dropdown .= '<option `value="'.$key.'"'.$select.'>'.$option.'</option>'."\n"; } /*** close select ***/ $dropdown .= '</select>'."\n"; /*** , return completed dropdown ***/ return $dropdown; } ?> <form> <?php mysql_connect('localhost', 'root', ''); mysql_select_db(''); $sql = "select alcohol alcohol "; $result = mysql_query($sql); echo "<select name='alcohol'>"; while ($row = mysql_fetch_array($result)) { echo "<option value='" . $row['alcohol'] . "'>" . $row['alcohol'] . "</option>"; } echo "</select>"; ?> </form>
//new stuff below this//
<form id="data" class="form_alcohol" role="form" method="post" action="connect.php"> <input type = "submit" name = "submit" value = "submit"> <select size="1" name="alcohol"> <option value="">--- select alcohol ---</option> <?php mysql_connect('localhost', 'root', ''); mysql_select_db(''); $sql = "select alcohol alcohol"; $result1 = mysql_query($sql); while ($row = mysql_fetch_array($result1)) { echo "<option value='" . $row['alcohol'] . "'>" . $row['alcohol'] . "</option>"; } $dropdown1 = empty($_post['alcohol'])? die ("error: select dropdown") : mysql_escape_string($_post['alcohol']); echo "</select>"; ?> <?php if(isset($_post['submit'])) { mysql_connect('localhost', 'root', ''); mysql_select_db(''); $sql = "select * alcohol '$dropdown1' = alcohol"; $result = mysql_query($sql) or die(mysql_error()); ?> <table> <td> alcohol </td> <td> price </td> <td> amount in stock </td> <?php while ($row = mysql_fetch_array($result)) { echo "<tr><td>".$row['alcohol']."</td><td>".$row['price']."</td><td>".$row['quantity_in_stock']."</td>"; } } ?> </table>
first of all, use mysqli. mysql_* deprecated , removed in future version.
now have out of way, add "marker" character in original dropdown select values in order value. pick unicode character wouldn't in database, , echo in value looks this:
<option value="$name@$price">$name</option>
then in code above, split value @ @ character , pick out after in order secondary value. should save database operation in long run.
good luck!
Comments
Post a Comment