mrphobos Posted July 10, 2007 Share Posted July 10, 2007 I'm trying to create a database driven drop down menu. I feel I'm on to the right path but my code doesn't seem to work. I've looked around and haven't seen where I'm making the mistake. Any one care to take a look? //Header font drop down include "connect.php"; $query="SELECT font FROM headerfont"; $result=mysql_query($query); $num = mysql_numrows($result); $row=mysql_fetch_array($result); echo "<select name=\"font\">"; echo "<option value=\"None shosen\">Choose:</option>\n"; for($i=0; $i<$num;$i++){ echo "<option value=\"".trim($row[$i])."</option>\n"; } echo "</select>"; As you can see I'm trying to get this loop to cycle through all the items in table and print out the different fonts the user can pick from. Right now the drop down menu displays blank spaces instead of the names in the table. I'd like to be able to take the font the user picks and put it into a variable to be stored on the database. Is this a workable method? I'm really shooting at he dark on this one. Any help would be greatly appreciated. Quote Link to comment https://forums.phpfreaks.com/topic/59216-database-driven-drop-down-menu/ Share on other sites More sharing options...
metrostars Posted July 10, 2007 Share Posted July 10, 2007 <?php //Header font drop down include "connect.php"; $query="SELECT font FROM headerfont"; $result=mysql_query($query); echo "<select name=\"font\">"; echo "<option value=\"None shosen\">Choose:</option>\n"; while($row = mysql_fetch_assoc($result)){ echo "<option value=\"".trim($row[$i])."</option>\n"; } echo "</select>"; ?> try that Quote Link to comment https://forums.phpfreaks.com/topic/59216-database-driven-drop-down-menu/#findComment-294155 Share on other sites More sharing options...
mrphobos Posted July 10, 2007 Author Share Posted July 10, 2007 No change in results. ??? Quote Link to comment https://forums.phpfreaks.com/topic/59216-database-driven-drop-down-menu/#findComment-294157 Share on other sites More sharing options...
metrostars Posted July 10, 2007 Share Posted July 10, 2007 <?php //Header font drop down include "connect.php"; $query="SELECT font FROM headerfont"; $result=mysql_query($query); echo "<select name=\"font\">"; echo "<option value=\"None shosen\">Choose:</option>\n"; while($row = mysql_fetch_assoc($result)){ echo "<option value=\"".trim($row["font"])."</option>\n"; } echo "</select>"; ?> sorry forgot to change something Quote Link to comment https://forums.phpfreaks.com/topic/59216-database-driven-drop-down-menu/#findComment-294159 Share on other sites More sharing options...
wildteen88 Posted July 10, 2007 Share Posted July 10, 2007 The code metrostars provided will work correctly however all you'll get is a blank list. That's because the generated code for the list is incorrect. echo "<option value=\"".trim($row["font"])."</option>\n"; That line will produce this: <option value="Arial</option> As you can see that is correct atall. It should be like this: $font = trim($row["font"]); echo "<option value=\"". $font ."\">" . $font . "</option>\n"; with now produces this: <option value="Arial">Arial</option> Ad that change and the code should work Quote Link to comment https://forums.phpfreaks.com/topic/59216-database-driven-drop-down-menu/#findComment-294547 Share on other sites More sharing options...
mrphobos Posted July 13, 2007 Author Share Posted July 13, 2007 Your right, it works now. I can't believe I over looked that. Thank you both for your help. I currently have a admin page that can update a few tables in a database, however, one of the inputs to update the database is a html textarea. When I update the tables without entering information into the textarea it erases the data already in the database feild for that textarea but when I do the same for a input textfeild it doesn't. It's also doing this for the drop down. Just not the textfeilds. The if statements I've written to take care of this issue don't seem to be working. Does any know if text area's aren't supported by php? Here's the code, Any additional help you guys can provide would be greatly appreciated. <html> <head> <title></title> <link rel="stylesheet" type="text/css" href=""/> </head> <body> <form action="admin.php" method="post"> <table border="1"> <tr> <th>Header:</th> <td> <input type="textfeild" name="header" /><br/></td> </tr> <tr> <th>Header Font</th> <td> <?php //Header font drop down include "connect.php"; $query="SELECT font FROM headerfont"; $result=mysql_query($query); echo "<select name=\"font\">"; echo "<option value=\"None chosen\">Choose header font:</option>\n"; while($row = mysql_fetch_assoc($result)){ $font = trim($row["font"]); echo "<option value=\"". $font ."\">" . $font . "</option>\n"; } echo "</select>"; $updatefont ="$_POST[font]"; echo $updatefont; if($updatefont == ""){ echo "Header font not updated"; }else{ $query="UPDATE currentfont set headerfont = '$updatefont'"; mysql_query($query); } ?> </td> </tr> <tr> <th>Content:</th> <td> <textarea rows="10" cols="40" name="content"> </textarea><br/></td> </tr> <tr> <th>Footer:</th> <td> <input type="textfeild" name="footer" /><br/></td> </tr> <tr> <td><input type="submit" value="update the site"/></td> </tr> </table> </form> <br/> <?php include "connect.php"; $updateheader = "$_POST[header]"; $updatecontent = "$_POST[content]"; $updatefooter = "$_POST[footer]"; //Update header if ($updateheader == "") { echo 'Header feild empty'; } else { $query = "UPDATE feilds SET header = '$updateheader'"; mysql_query($query); } ?> <br/> <? //Update content if ($updatecontent == "") { echo "Content feild empty"; } else { $query= "UPDATE feilds SET content ='$updatecontent'"; mysql_query($query); } ?> <br/> <? //Update footer if ($updatefooter == "") { echo "Footer feild empty"; } else { $query= "UPDATE feilds SET footer ='$updatefooter'"; mysql_query($query); } mysql_close($con); ?> View the site by clicking<a href="index.php"><u> here.</u></a> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/59216-database-driven-drop-down-menu/#findComment-297672 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.