Jump to content

Database Driven Drop Down Menu


mrphobos

Recommended Posts

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.

Link to comment
Share on other sites

<?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

Link to comment
Share on other sites

<?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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.