Jump to content

default in drop down list chosen from mysql table


aminkorea

Recommended Posts

I am working on a project that uses a drop down list to chose the category when inserting new data into the database. What I want to do now is make the drop down list default to the chosen category on the list records page and the update page. I have read several tutorials, but they all say that I have to list the options and then select the default. But since it is possible to add and remove categories, this approch won't work. I need the code to chose the correct category on the fly.

 

There are two tables, one that has the category ID and category name.  The second table has the data and the catid which is referenced to the category id in the first table.

 

--
-- Table structure for table `categories`
--

DROP TABLE IF EXISTS `categories`;
CREATE TABLE IF NOT EXISTS `categories` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `categories` varchar(37) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=40 ;

-- --------------------------------------------------------

--
-- Table structure for table `links`
--

DROP TABLE IF EXISTS `links`;
CREATE TABLE IF NOT EXISTS `links` (
  `id` int(4) NOT NULL AUTO_INCREMENT,
  `catid` int(11) DEFAULT NULL,
  `name` varchar(255) NOT NULL DEFAULT '',
  `url` varchar(255) NOT NULL DEFAULT '',
  `content` varchar(255) NOT NULL DEFAULT '',
  PRIMARY KEY (`id`),
  KEY `catid` (`catid`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=35 ;

 

Then is the list records file, I have

 

<?php
include ("db.php");
include ("menu.php");


$result = mysql_query("SELECT categories FROM categories") or die(mysql_error()); 
while ($row = mysql_fetch_array($result)) { 
$categories=$row["categories"];
$options.= '<option value="'.$row['categories'].'">'.$row['categories'].'</option>';
};



$id = $_GET['id'];
$query="SELECT * FROM links ORDER BY catid ASC";
$result=mysql_query($query);

?>
<table width="65%" align="center" border="0" cellspacing="1" cellpadding="0">
<tr>
<td>
<table width="100%" border="1" cellspacing="0" cellpadding="3">
<tr>
<td colspan="7"><strong>List data from mysql </strong> </td>
</tr>

<tr>
<td align="center"><strong>Category ID</strong></td>
<td align="center"><strong>Category ID</strong></td>
<td align="center"><strong>Name</strong></td>
<td align="center"><strong>URL</strong></td>
<td align="center"><strong>Content</strong></td>
<td align="center"><strong>Update</strong></td>
<td align="center"><strong>Delete</strong></td>
</tr>
<?php
while($rows=mysql_fetch_array($result)){
?>
<tr>
<td>
<SELECT NAME=catid>
<OPTION>Categories</OPTION>
<?php echo $options; ?>
</SELECT>
</td>
<td><? echo $rows['catid']; ?></td>
<td><? echo $rows['name']; ?></td>
<td><a href="<? echo $rows['url']; ?>"><? echo $rows['url']; ?></a></td>
<td><? echo $rows['content']; ?></td>
<td align="center"><a href="update.php?id=<? echo $rows['id']; ?>">update</a></td>
<td align="center"><a href="delete.php?id=<? echo $rows['id']; ?>">delete</a></td>

</tr>
<?php
}
?>
</table>
</td>
</tr>
</table>
<?php
mysql_close();
?>

 

So, how do I get this code

$result = mysql_query("SELECT categories FROM categories") or die(mysql_error()); 
while ($row = mysql_fetch_array($result)) { 
$categories=$row["categories"];
$options.= '<option value="'.$row['categories'].'">'.$row['categories'].'</option>';
};

<SELECT NAME=catid>
<OPTION>Categories</OPTION>
<?php echo $options; ?>
</SELECT>

 

to give me an output that will be

 

something like if catid exactly matches categories.id echo categories.categorie  ???

 

so far everything I have done produces either a default category of the last category,  the catid (which is a number), all of the categories (logical since catid will always be = id, or nothing.  How do I get just the category name? 

 

I will keep reading and try to figure this out, but any help would be greatly appreciated.

 

Thanks in advance

Link to comment
Share on other sites

I decided to change the code to echo only the category without the dropdown list, so this has been solved. But I still need to know how to getthe drop down on the update.php to default, I will post the code for that soon. Thanks and have a nice day.

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.