Jump to content

New probelm with dropdown list


aminkorea

Recommended Posts

If I can get this fixed, I will have completed all but the admin login for this project - my first php/mysql project.

 

Here is what I need.  I have a list_records.php that list all the records in the table 'links' and the category each entry is in from the table 'categories'.

 

Here are my table structures.

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

 

On the update.php file, I have a form that lets me make changes to the record. 

 

Here is the codes for update.php

 

<? include "menu.php" ?>

<? include "db.php" ?>
<?php
$id=$_GET['id'];

$sql = "select * from links where id =$id";
$query = mysql_query($sql);

while ($row = mysql_fetch_array($query)){

    $id = $row['id'];
    $catid = $row['catid'];
    $name = $row['name'];
    $url = $row['url'];
    $content = $row['content'];

    //we will echo these into the proper fields

}
mysql_free_result($query);
?>

<table width="65%" align="center">
<tr><td align="left">

<form action="updated.php" method="post">

<input type="hidden" value="<?php echo $id; ?>" name="id"/>

<br>


<b>Website Name:</B><br> Change the name of the website listing.<br>
<input type="text" value="<?php echo $name; ?>" name="name"/>

<br>
<br>
<b>URL:</b><br> Change the URL of the website listing.<br>
<input type="text" value="<?php echo $url; ?>" name="url"/>

<br>
<br>

<b>Description:</b><br> Change the description of the website listing.<br> Limit 255 characters.<br/>
<textarea name="content" cols="45" rows="4" wrap="soft"><?php echo($content);?></textarea>

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


?>


<SELECT NAME=catid>
<OPTION VALUE=selected><? echo $catid; ?><? echo $options; ?></OPTION>
</SELECT>

<?php
mysql_close();
?>

<div align="center">
<input type="submit" value="submit changes"/>
</div>
</form>

<br>
</td></tr></table>

 

The part of he code I need help with is

 

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


?>


<SELECT NAME=catid>
<OPTION VALUE=selected><? echo $catid; ?><? echo $options; ?></OPTION>
</SELECT>

 

I want it to default to the category that the entry is in.  If you look, you will see in the select portion that I I have

<$ echo $catid; ?>

which echos the proper category ID, but if I use

<? echo $categories; ?>

it echos Writing, which is the last category in the list.  Yet, the $options echo the catid and it corresponding category.  How can I get the default option to echo BOTH the catid and category name while also listing all the other categories so that the records can be moved to a new category is needed?

 

Any help will be appreciated. Thank you in advance.

 

Link to comment
Share on other sites

Not tested.

<?php 
include("menu.php");
include("db.php");
if (isset($_GET['id']) && ctype_digit($_GET['id']))
$id=$_GET['id'];
}
$sql = "select * from links where id =$id";
$query = mysql_query($sql); 
while ($row = mysql_fetch_array($query)){ 
    $id = $row['id'];
    $catid = $row['catid'];
    $name = $row['name'];
    $url = $row['url'];
    $content = $row['content']; 
}
if (isset($_POST['submit_changes'])){
//process update

}//if (isset($_POST['submit_changes']))
?>
<html>
<head>
<title>Update</title>
</head>
<body>
<form action="updated.php" method="post"> 
<table width="65%" align="center">
<tr>
	<td align="left"><input type="hidden" value="<?php if (isset($id){echo "$id";} ?>" name="id" /><b>Website Name:</b>
	<br />Change the name of the website listing.<br />
	<input type="text" value="<?php if (isset($name){echo "$name";} ?>" name="name" />
	<br>
	<br><b>URL:</b>
	<br>Change the URL of the website listing.
	<br><input type="text" value="<?php if (isset($url){echo "$url";} ?>" name="url"/> 
	<br>
	<br><b>Description:</b>
	<br>Change the description of the website listing.
	<br>Limit 255 characters.
	<br/><textarea name="content" cols="45" rows="4" wrap="soft"><?php if (isset($content){echo($content);} ?></textarea>
	<br>
	<?php
	$result = mysql_query("SELECT id, categories FROM categories") or die(mysql_error());
	echo "<select name=\"catid\">\r"; 
	while ($row = mysql_fetch_array($result)) {
	echo "<option value=\"{$row['id']}\"" . (isset($id) && $id=="{$row['id']}" ? ' selected="selected"' : '') . ">{$row['id']}-{$row['categories']}</option>\r";
	}
	echo "</select>\r";
	?>
	<div align="center">
	<input type="submit" name="submit_changes" value="submit changes" />
	</div></td>
</tr>
</table>
</form>	 
</body>
</html>

Link to comment
Share on other sites

I would wrap the first IF statement the receives the GET id so that it encloses the query so it's not run without an id.  Sorry I didn't spot that before.

 

<?php 
include("menu.php");
include("db.php");
if (isset($_GET['id']) && ctype_digit($_GET['id'])){
$id=$_GET['id'];
$sql = "select * from links where id =$id";
$query = mysql_query($sql); 
while ($row = mysql_fetch_array($query)){ 
    $id = $row['id'];
    $catid = $row['catid'];
    $name = $row['name'];
    $url = $row['url'];
    $content = $row['content']; 
}
}//if (isset($_GET['id']) && ctype_digit($_GET['id']))
if (isset($_POST['submit_changes'])){
//process update

}//if (isset($_POST['submit_changes']))
?>
<html>
<head>
<title>Update</title>
</head>
<body>
<form action="updated.php" method="post"> 
<table width="65%" align="center">
<tr>
	<td align="left"><input type="hidden" value="<?php if (isset($id){echo "$id";} ?>" name="id" /><b>Website Name:</b>
	<br />Change the name of the website listing.<br />
	<input type="text" value="<?php if (isset($name){echo "$name";} ?>" name="name" />
	<br>
	<br><b>URL:</b>
	<br>Change the URL of the website listing.
	<br><input type="text" value="<?php if (isset($url){echo "$url";} ?>" name="url"/> 
	<br>
	<br><b>Description:</b>
	<br>Change the description of the website listing.
	<br>Limit 255 characters.
	<br/><textarea name="content" cols="45" rows="4" wrap="soft"><?php if (isset($content){echo($content);} ?></textarea>
	<br>
	<?php
	$result = mysql_query("SELECT id, categories FROM categories") or die(mysql_error());
	echo "<select name=\"catid\">\r"; 
	while ($row = mysql_fetch_array($result)) {
	echo "<option value=\"{$row['id']}\"" . (isset($id) && $id=="{$row['id']}" ? ' selected="selected"' : '') . ">{$row['id']}-{$row['categories']}</option>\r";
	}
	echo "</select>\r";
	?>
	<div align="center">
	<input type="submit" name="submit_changes" value="submit changes" />
	</div></td>
</tr>
</table>
</form>	 
</body>
</html>

Link to comment
Share on other sites

Hey Drummin. I just tried the code you wrote and I got a syntax error.

 

syntax error, unexpected '{' in /home3/johnsesl/public_html/links/linksadmin/update.php on line 30

 

So, I changed the code a bit so that in the form, where the syntax error was, I changed to

 

<?php echo $id; ?>

 

in all the form fields. Here is the complete code now,

 

<?php 
include("menu.php");
include("db.php");
if (isset($_GET['id']) && ctype_digit($_GET['id'])){
$id=$_GET['id'];
$sql = "select * from links where id =$id";
$query = mysql_query($sql); 
while ($row = mysql_fetch_array($query)){ 
    $id = $row['id'];
    $catid = $row['catid'];
    $name = $row['name'];
    $url = $row['url'];
    $content = $row['content']; 
}
}//if (isset($_GET['id']) && ctype_digit($_GET['id']))
if (isset($_POST['submit_changes'])){
//process update

}//if (isset($_POST['submit_changes']))
?>
<html>
<head>
<title>Update</title>
</head>
<body>
<form action="updated.php" method="post"> 
<table width="65%" align="center">
<tr>
	<td align="left"><input type="hidden" value="<?php echo $id; ?>" name="id" /><b>Website Name:</b>
	<br />Change the name of the website listing.<br />
	<input type="text" value="<?php echo $name; ?>" name="name" />
	<br>
	<br><b>URL:</b>
	<br>Change the URL of the website listing.
	<br><input type="text" value="<?php echo $url; ?>" name="url"/> 
	<br>
	<br><b>Description:</b>
	<br>Change the description of the website listing.
	<br>Limit 255 characters.
	<br/><textarea name="content" cols="45" rows="4" wrap="soft"><?php echo $content; ?></textarea>
	<br>
	<?php
	$result = mysql_query("SELECT id, categories FROM categories") or die(mysql_error());
	echo "<select name=\"catid\">\r"; 
	while ($row = mysql_fetch_array($result)) {
	echo "<option value=\"{$row['id']}\"" . (isset($id) && $id=="{$row['id']}" ? ' selected="selected"' : '') . ">{$row['id']}-{$row['categories']}</option>\r";
	}
	echo "</select>\r";
	?>
	<div align="center">
	<input type="submit" name="submit_changes" value="submit changes" />
	</div></td>
</tr>
</table>
</form>	 
</body>
</html>

 

The code now displays all the correct information in the form fields, and it defaults to a single category within the list. The problem now is that the default category that the code chooses is equal to the data ID and not the catid. So, if a record has an id of 8 the default category chosen by t he code is also 8, but the id in the links is not associated with the categories, so it is the wrong default category.

 

Example: links.id = 8 links.catid = 3  links.name = http://somesite.com || categories.id = 3 categories.categories = Bilingual Education

 

Output:  URL = http://somesite.com  Name = Somesite  Content = Description  Default Category = 8-CALL

 

I hope all that made sense..

Link to comment
Share on other sites

Are you saying the option should be catid like this?

echo "<option value=\"{$row['id']}\"" . (isset($catid) && $catid=="{$row['id']}" ? ' selected="selected"' : '') . ">{$row['id']}-{$row['categories']}</option>\r";

 

Your are a god send.  I tried something similar but I had a few things wrong so it didn't work.  Where you have

 

(isset($catid) && $catid=="{$row['id']}" 

 

I wrote

(isset($catid) && $id=="{$row['id']}" 

 

Which gave me an out put where there were eight of every option in the list.  It never occurred to me to write the code as you have it. But now that I see it, it makes perfect sense.

 

But since I am new to all this my mind hasn't yet learned what syntax can and cannot be used.  Please let me take a shot at explaining this and correct me is I am wrong.

 

In simple English: if the catid is set and it exactly matches (==) the value in the row categories id then echo what has been selected by the user. The echo will be in the format of categories id and category name and will exactly match the links category id. 

 

Am I close? 

 

Thank you for your help. The script now does exactly what I need.  I just hope someday I will know enough that I, too, can be helpful.

 

Cheers

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.