Page 1 of 1
list/menu + selected
Posted: Thu Sep 29, 2005 7:48 pm
by Jim_Bo
Hi,
I cant get the following code to show the database record as the selected option I thought it was correct:
Code: Select all
<select name="category_name">
<?php
$sql = "SELECT * FROM gallery_category";
$result = mysql_query($sql);
while ($row = mysql_fetch_array($result)) {
?>
<option <?php echo $category_name==$row['category_name'] ? 'selected' : ''?> value="<?php echo $row['category_name']; ?>"><?php echo $row['category_name']; ?></option>
<?php
}
?>
</select>
Cheers
Posted: Thu Sep 29, 2005 7:53 pm
by John Cartwright
try
Code: Select all
$sql = "SELECT * FROM gallery_category";
$result = mysql_query($sql) or die(mysql_error());
echo '<select name="category_name">';
while ($row = mysql_fetch_array($result)) {
echo '<option '.($category_name == $row['category_name'] ? 'selected = "selected"' : '').' value="'.$row['category_name'].'">'.$row['category_name'].'</option>';
}
echo '</select>';
And see if that produces an error. Other than that syntax looks ok.
By the way, where does $category_name come from?
Posted: Thu Sep 29, 2005 8:10 pm
by Jim_Bo
Hi,
$category_name comes from the "select name"
Posted: Thu Sep 29, 2005 8:23 pm
by John Cartwright
That won't work unless register globals is ON, which it is OFF by default in any half-new release of php.
Change this line
Code: Select all
echo '<option '.($category_name == $row['category_name'] ? 'selected = "selected"' : '').'
to
Code: Select all
echo '<option '.(isset($_POST['category_name']) && $_POST['category_name'] == $row['category_name'] ? 'selected = "selected"' : '').'
Posted: Thu Sep 29, 2005 8:29 pm
by Jim_Bo
Hi,
I have sent $category_name across the url and used $_GET .. is that a pluasable way to do it?
Cheers
Posted: Thu Sep 29, 2005 9:01 pm
by John Cartwright
so your form method is set to GET instead of POST? that is fine.. just change $_POST to $_GET in that line I told you to change
Posted: Fri Sep 30, 2005 1:13 am
by Jim_Bo
Hi
What is the correct syntax for this:
Code: Select all
echo '<option '.$_GET['category_name'] == $row['category_name'] ? 'selected' : ''.' value="'.$row['category_id'].'">'.$row['category_name'].'</option>';
the following works but when I try to echo the result the list menu is empty
Code: Select all
<option <?php echo $_GET['category_name'] == $row['category_name'] ? 'selected' : ''?> value="<?php echo $row['category_id']; ?>"><?php echo $row['category_name']; ?></option>
Posted: Fri Sep 30, 2005 1:32 am
by ruchit
corrected syntax
Code: Select all
echo '<option '.($_GET['category_name'] == $row['category_name'] ? 'selected' : '').' value="'.$row['category_id'].'">'.$row['category_name'].'</option>';
Posted: Fri Sep 30, 2005 1:34 am
by Jim_Bo
Hi,
works great
Cheers