Closed Thread Icon

Topic awaiting preservation: How do I set priority on the form tag SELECT? (Page 1 of 1) Pages that link to <a href="https://ozoneasylum.com/backlink?for=27199" title="Pages that link to Topic awaiting preservation: How do I set priority on the form tag SELECT? (Page 1 of 1)" rel="nofollow" >Topic awaiting preservation: How do I set priority on the form tag SELECT? <span class="small">(Page 1 of 1)</span>\

 
dade
Nervous Wreck (II) Inmate

From: Kansas, just this side of the rainbow
Insane since: Sep 2004

posted posted 12-22-2005 20:25

My problem is that I have a form with a SELECT tag that calls its options from a MySQL database. This works fine, but when I set the edit page to allow people to edit their entries, I want the select tag to list whatever the original entry had.

code:
<select name="zone" size="1">
<?php
include( 'includes/loginstuff.php');
					
$order_name = "SELECT * FROM zone_table ORDER BY zone_name ASC";
$get_name = mysql_query($order_name, $login) or die (mysql_error());
	
while ($result = mysql_fetch_array($get_name)) {
	$id = $result['id'];
	$zone = $result['zone_name'];
						
	$display_zone .= "<option value=\"$id \">$zone</option>";
						
}
?>
<?php echo $display_zone; ?>	
</select>



This code works fine, but it does not allow me to choose what option gets listed first. I have tried using a

code:
"SELECT * FROM  zone_table WHERE id = $_GET[id]";

which works fine to get the previous option to list, but it only lists that option. I cannot seem to figure out a way to include both into this select tag and make it work. How do I write this so that I have the previous option listed first and still have all the other options listed below?

Hope I have written this properly to convey my problem.

Thank you for your ideas,

D

This patient is ready for his medication!

DL-44
Maniac (V) Inmate

From: under the bed
Insane since: Feb 2000

posted posted 12-22-2005 21:31

If I understand correctly, you have two main options -

1) pass the information for the 'previous' option from the original page, via a session variable, or throught a hidden input field.

2) make 2 separate database queries. once for the 'previous' and once for the rest.

then you simply spit the 'previous' variable out into an option tag first, then loop through the rest.

Make sense?

Tyberius Prime
Paranoid (IV) Mad Scientist with Finglongers

From: Germany
Insane since: Sep 2001

posted posted 12-22-2005 21:43

Hm... you either want to have the current value at the top of the list - you'll need to built an array and for each fetched row, either append it, or unshift() it if it's the old entry for that.

Or you just want it selected in the box?
Then you'll need to specify selected="true" on that particular <option> tag.

like this

code:
<select name="zone" size="1">
<?php
include( 'includes/loginstuff.php');

$order_name = "SELECT * FROM zone_table ORDER BY zone_name ASC";
$get_name = mysql_query($order_name, $login) or die (mysql_error());

while ($result = mysql_fetch_array($get_name)) 
{
    $id = $result['id'];
    $zone = $result['zone_name'];
    if ($id == $_GET['id'])
           $display_zone .= "<option selected="true" value=\"$id \">$zone</option>";
    else
           $display_zone .= "<option value=\"$id \">$zone</option>";

}
?>
<?php echo $display_zone; ?>
</select>


so long,
->Tyberius Prime

Edit:

code:
"SELECT * FROM  zone_table WHERE id = $_GET[id]";
that's a security risk.
at least make it
"SELECT * FROM  zone_table WHERE id = " . intval ( $_GET[id] ) ;

dade
Nervous Wreck (II) Inmate

From: Kansas, just this side of the rainbow
Insane since: Sep 2004

posted posted 12-22-2005 22:10

Thank you both of you.

DL-44: I was on the path of the second option before I posted. When you mentioned it in your post I went back and found a typo that help caused my problem. Thank you. I was too sure of my code to think perhaps I had made a mistake...that and the server I am on, the IT department won't turn on error reporting or logging for me.

Tyberius Prime: Your code is much cleaner than mine, so I am going to try it now. As for security, I am at a loss about making things secure (really, I am a newbie about PHP). Where can I go to learn about security and how is the function "intval" more secure?

Thank you,

D

This patient is ready for his medication!

« BackwardsOnwards »

Show Forum Drop Down Menu