Drop down list help

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
therat
Forum Commoner
Posts: 62
Joined: Wed Oct 01, 2003 2:44 pm
Location: London

Drop down list help

Post by therat »

I want to have a drop down list that has 2 options, Active or Inactive. The values entered to MySQL should be 1 or 0 respectivly.
This is what I have so far.

Code: Select all

<?php
<select name="CAT_STATUS" id="CAT_STATUS">
                  <?php
do &#123;  
?>
                  <option value="<?php echo $row_newcat&#1111;'status']?>"><?php echo $row_newcat&#1111;'status']?></option>
                  <?php
&#125; while ($row_newcat = mysql_fetch_assoc($newcat));
  $rows = mysql_num_rows($newcat);
  if($rows > 0) &#123;
      mysql_data_seek($newcat, 0);
	  $row_newcat = mysql_fetch_assoc($newcat);
  &#125;
?>
                </select>
?>
All this does is select values from the database. HOw do I change the above to do what I need. I think I need to use an array but, I am not sure on how to go about this.
tsg
Forum Contributor
Posts: 142
Joined: Sun Jan 12, 2003 9:22 pm
Location: SE, Alabama
Contact:

Post by tsg »

This is the way I do it ...

Code: Select all

<?
$result = @mysql_query("SELECT * FROM table_name WHERE whatever");
	if (!$result) { echo( "Error perforing query" . mysql_error() . "that error"); exit(); }

	$cat_status = $row["cat_status"];

print "<select name="cat_status">";
print "<option value="">Please Choose";
if ($cat_status == '1') {
	print "<option value="1" selected>Active";
} else {
	print "<option value="1">Active";
}

if ($cat_status == '0') {
	print "<option value="0" selected>Inactive";
} else {
	print "<option value="0">Inactive";
}
print "</select>
?>
Something like that .. may need to adjust to what you are doing.
User avatar
mrvanjohnson
Forum Contributor
Posts: 137
Joined: Wed May 28, 2003 11:38 am
Location: San Diego, CA

Post by mrvanjohnson »

tsg way is one way of doing it.

If you wanted to use an array like you mentioned and knew what the values were and that they wouldn't change, you could do something like this
(assuming $newcat = your database call)

Code: Select all

<?php
$status = array("Inactive", "Active");
while ($row_newcat = mysql_fetch_assoc($newcat)){
$statusid = $row_newcat ['status'];
echo <option value= " $statusid ">" . $status['statusid']. "</option> \n";
}
?>
:? for some reason the first \" (escape) isn't showing up but it should be there. Should be value= \"$statusid \"

That should create your drop down box so the user sees the words Active or InActive and still write the correct number to the database. Obviously you can tweak to get a default to select etc.
zenabi
Forum Commoner
Posts: 84
Joined: Mon Sep 08, 2003 5:26 am
Location: UK

Post by zenabi »

I have a similar problem. I have a dropdown box like this:

Code: Select all

&lt;select name='month'&gt;				
   &lt;option value='01'&gt;January&lt;/option&gt;
   &lt;option value='02'&gt;February&lt;/option&gt;
   &lt;option value='03'&gt;March&lt;/option&gt;
   &lt;option value='04'&gt;April&lt;/option&gt;
   &lt;option value='05'&gt;May&lt;/option&gt;
   &lt;option value='06'&gt;June&lt;/option&gt;
   &lt;option value='07'&gt;July&lt;/option&gt;
   &lt;option value='08'&gt;August&lt;/option&gt;
   &lt;option value='09'&gt;September&lt;/option&gt;
   &lt;option value='10'&gt;October&lt;/option&gt;
   &lt;option value='11'&gt;November&lt;/option&gt;
   &lt;option value='12'&gt;December&lt;/option&gt;
&lt;/select&gt;
If I pulled out a value from my database and assigned it to a variable, for example:

Code: Select all

$month = 10;
how would I make PHP echo out SELECTED in the correct place?

Code: Select all

&lt;select name='month'&gt;
   ...
   ...
   ...
   &lt;option value='10' SELECTED&gt;October&lt;/option&gt;
   ...
&lt;/select&gt;
Nay
Forum Regular
Posts: 951
Joined: Fri Jun 20, 2003 11:03 am
Location: Brisbane, Australia

Post by Nay »

Here's something I wrote:

Code: Select all

<?php

$months = array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December");

$month = "October";

$max = count($months);

echo "<select name="month">";

for($i=0;$i<$max;$i++) {

if($months[$i]==$month) {

echo "<option value="$i" selected="selected">$months[$i]</option>";

} else {

echo "<option value="$i">$months[$i]</option>";

}

}

echo "</select>";

?>
Hope it helps,

-Nay
therat
Forum Commoner
Posts: 62
Joined: Wed Oct 01, 2003 2:44 pm
Location: London

Post by therat »

tsg wrote:This is the way I do it ...

Code: Select all

<?
$result = @mysql_query("SELECT * FROM table_name WHERE whatever");
	if (!$result) { echo( "Error perforing query" . mysql_error() . "that error"); exit(); }

	$cat_status = $row["cat_status"];

print "<select name="cat_status">";
print "<option value="">Please Choose";
if ($cat_status == '1') {
	print "<option value="1" selected>Active";
} else {
	print "<option value="1">Active";
}

if ($cat_status == '0') {
	print "<option value="0" selected>Inactive";
} else {
	print "<option value="0">Inactive";
}
print "</select>
?>
Something like that .. may need to adjust to what you are doing.
Thanks, I'll give that a go
Post Reply