Connecting Tech Pros Worldwide Forums | Help | Site Map

how to assign number values on select

ddtpmyra's Avatar
Familiar Sight
 
Join Date: Jun 2008
Location: CA
Posts: 222
#1: Sep 29 '09
How can I assgined value to each row of my query result?
Example Book=1; Fruits=2; Animals=3
OR is there other way to do it?

thanks for your help!

Expand|Select|Wrap|Line Numbers
  1. echo "<tr><td><b>Categories:</b></td>"; 
  2.             $res=mysql_query("select cat_name  from categories order by cat_name"); 
  3.             if(mysql_num_rows($res)==0){ 
  4.             echo "there is no data in table.."; 
  5.             } else { 
  6.             echo "
  7.  
  8.             <td width='100%'><select name=\"catname\" id=\"catname\" value=\"$catname\" >"; 
  9.                         for($i=0;$i<mysql_num_rows($res);$i++) { 
  10.                         $row=mysql_fetch_assoc($res); 
  11.                         echo"<option value=\"$row[cat_name]\""; 
  12.                         if($catname==$row[cat_name]) 
  13.                         echo "selected"; 
  14.                         echo ">$row[cat_name]</option>"; 
  15.                         } 
  16.                         echo "</select><br></td></tr>"; 
  17.             } 

TheServant's Avatar
Expert
 
Join Date: Feb 2008
Location: Australia
Posts: 919
#2: Sep 29 '09

re: how to assign number values on select


Where does the number come from? Is it in your database? Does it change (dynamic)?

If it's in your database, you could select it with your cat_name and include that in your loop.
If it's dynamic then you need a function in your loop to work out which one it is.

I couldn't really make out from your code what you are referring to. Do you want your value for each option to be a number instead of a name?
Atli's Avatar
Moderator
 
Join Date: Nov 2006
Location: Iceland
Posts: 3,758
#3: Sep 29 '09

re: how to assign number values on select


Hey.

What column in your table serves as a Primary Key?

Ideally, the Primary Key would be an integer with Auto_Increment.
That creates a "row number" column which you can use if you need a number to go along with a row.
Reply