Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 07-23-2008, 06:05 PM   PM User | #1
blueuniverse
New Coder

 
Join Date: Jun 2006
Posts: 21
Thanks: 2
Thanked 0 Times in 0 Posts
blueuniverse is an unknown quantity at this point
Dynamically filled select form

I have a piece of code

PHP Code:
    $result mysql_query("SELECT * FROM category"
    or die(
mysql_error());  
    
    echo 
"<select name='catid' id='catid'>";
    
// keeps getting the next row until there are no more to get
    
while($row mysql_fetch_array$result )) {
    
// Print out the contents of each row into a table
    
echo "<option value='".$row['id']."'>"
    echo 
$row['category'];
    echo 
"</option>"
    echo 
"\n"
This is fine for an upload form (where I've got it from) but not for an edit form. I need it to haev selected=selected by the option that matches my category id.

Just for reference the outputted html of the above is

Code:
<select name='catid' id='catid'><option value='1'>Cat1</option>
<option value='2'>Cat2</option>
<option value='3'>Cat3</option>
<option value='4'>Cat4</option>
</select>
My script is getting the recordid and calling it $id

It's then doing a sql select

PHP Code:
$sql "SELECT * FROM library WHERE id=$id"

I want it to grab the catid from this (catid being a field in the table) and then add selected=selected in the initial code I posted.

Thanks in advance,
Ed
blueuniverse is offline   Reply With Quote
Old 07-23-2008, 06:35 PM   PM User | #2
Fumigator
UE Antagonizer


 
Fumigator's Avatar
 
Join Date: Dec 2005
Location: Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
Posts: 7,686
Thanks: 42
Thanked 637 Times in 625 Posts
Fumigator is a glorious beacon of lightFumigator is a glorious beacon of lightFumigator is a glorious beacon of lightFumigator is a glorious beacon of lightFumigator is a glorious beacon of light
Do you have the correct value in $id at the time you build your <select> tag? If so, simply add "if" statements to each iteration of your loop to check $id against what's just been selected from your category table and if it matches, echo the selected="selected" text.

A popular way to do this is to use the ternary form of an "if" statement:

PHP Code:
 echo "<option value='".$row['id']."' " .($row['id'] == $id "selected='selected'" "") . ">"
http://us.php.net/ternary (page down a bit)
__________________
Fumigator is offline   Reply With Quote
Old 07-23-2008, 09:08 PM   PM User | #3
scoop_987
New Coder

 
Join Date: Jul 2008
Posts: 91
Thanks: 4
Thanked 9 Times in 9 Posts
scoop_987 is an unknown quantity at this point
erm... do you have the cat id in the url as the query string?

otherwise it would be as simple as:

PHP Code:
    $result mysql_query("SELECT * FROM category")  
    or die(
mysql_error());   
     
    echo 
"<select name='catid' id='catid'>"
    
// keeps getting the next row until there are no more to get 
    
while($row mysql_fetch_array$result )) { 
    
// Print out the contents of each row into a table 
if($_GET['cat_id'] == $row['id']){
echo 
"<option value=\"".$row['id']."\" selected=\"selected\">";  
}else{
echo 
"<option value=\"".$row['id']."\">";  
}
    echo 
"<option value='".$row['id']."'>";  
    echo 
$row['category']; 
    echo 
"</option>";  
    echo 
"\n"


Last edited by scoop_987; 07-23-2008 at 09:10 PM.. Reason: forgot the php tags
scoop_987 is offline   Reply With Quote
Old 07-23-2008, 09:22 PM   PM User | #4
oesxyl
Master Coder


 
Join Date: Dec 2007
Posts: 6,682
Thanks: 436
Thanked 890 Times in 879 Posts
oesxyl is a jewel in the roughoesxyl is a jewel in the roughoesxyl is a jewel in the rough
if you want the result as $row['xxxxxx'] you must use mysql_fetch_assoc instead of mysql_fetch_array

regards
oesxyl is offline   Reply With Quote
Old 07-23-2008, 09:29 PM   PM User | #5
Iszak
Regular Coder

 
Iszak's Avatar
 
Join Date: Jun 2007
Location: Perth, Western Australia
Posts: 332
Thanks: 2
Thanked 58 Times in 57 Posts
Iszak is an unknown quantity at this point
oesxyl no you don't have to use mysql_fetch_assoc compared to mysql_fetch_array... you can add a parameter to use number or association or both and by default array is both If I recall. So he doesn't need to use mysql_fetch_assoc compared to mysql_fetch_array.

http://www.php.net/mysql_fetch_array - Should check it out oesxyl.

Edit: I might add that it is faster but by no means MUST he use it.

Last edited by Iszak; 07-23-2008 at 09:32 PM..
Iszak is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 07:03 AM.


Advertisement
Log in to turn off these ads.