Page 1 of 1

Drop down list help

Posted: Wed Oct 08, 2003 2:13 pm
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.

Posted: Wed Oct 08, 2003 3:20 pm
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.

Posted: Wed Oct 08, 2003 4:02 pm
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.

Posted: Thu Oct 09, 2003 7:02 am
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;

Posted: Thu Oct 09, 2003 7:38 am
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

Posted: Thu Oct 09, 2003 1:29 pm
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