zelig Posted April 12, 2012 Share Posted April 12, 2012 I'm trying to make it so that someone can add an item to the shop based upon the current items within the database. How do I go about doing that? I know i have to change <input> to <select>, but beyond that, how do I code it so I can run an array to get all the current items from the table `items` and list them by their `name` field? Thanks in advance! Here is what I have so far: function addshopinv($id) { error_reporting(E_ALL); ini_set('display_errors', 1); if (isset($_POST["submit"])) { extract($_POST); $errors = 0; $errorlist = ""; if ($name == "") { $errors++; $errorlist .= "Name is required.<br />"; } else if ($errors == 0) { $dbh=dbconnect() or die ("Userlist read error: " . mysql_error()."<br>"); mysql_select_db("XXX"); $query = mysql_query("SELECT id FROM items WHERE name='$name'"); while ($result = mysql_fetch_array($query)){ $item_id = $result['id']; $query1 = mysql_query("INSERT INTO sale SET shop_id='$id', item_id='$item_id'"); } admindisplay("Inventory Item Added.","Add New Inventory Item"); } else { admindisplay("<b>Errors:</b><br /><div style=\"color:red;\">$errorlist</div><br />Please go back and try again.", "Add New Item to Shop"); } } $page = <<<END <b><u>Add New Inventory Item</u></b><br /><br /> <form action="admin_panel.php?do=addshopinv:$id" method="post"> <table width="90%"> <tr><td width="20%">Name:</td><td><input type="text" name="name" size="30" maxlength="255" value="" />*255 character max</td></tr> </table> <input type="submit" name="submit" value="Submit" /> <input type="reset" name="reset" value="Reset" /> </form> END; $page = parsetemplate($page, $row); admindisplay($page, "Add New Inventory Item"); } Quote Link to comment https://forums.phpfreaks.com/topic/260811-trying-to-do-a-drop-down-menu-of-items-in-the-database/ Share on other sites More sharing options...
MMDE Posted April 12, 2012 Share Posted April 12, 2012 Pre-text selected: echo '<form method="post" action=""> <select name="theNameOfTheDropDownList"> <option selected="selected">select an alternative</option>'; while($row = mysql_fetch_assoc($result)){ echo ' <option value="'.$row['someId'].'">'.$row['someData'].'</option>'; } echo ' </select> </form>'; Alternatively something is pre-selected: echo '<form method="post" action=""> <select name="theNameOfTheDropDownList"> while($row = mysql_fetch_assoc($result)){ if($row['someId'] == $selectedId){ echo ' <option value="'.$row['someId'].'" selected="selected">'.$row['someData'].'</option>'; }else{ echo ' <option value="'.$row['someId'].'">'.$row['someData'].'</option>'; } } echo ' </select> </form>'; Quote Link to comment https://forums.phpfreaks.com/topic/260811-trying-to-do-a-drop-down-menu-of-items-in-the-database/#findComment-1336729 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.