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
therat
Forum Commoner
Posts: 62 Joined: Wed Oct 01, 2003 2:44 pm
Location: London
Post
by therat » Wed Oct 08, 2003 2:13 pm
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 {
?>
<option value="<?php echo $row_newcatї'status']?>"><?php echo $row_newcatї'status']?></option>
<?php
} while ($row_newcat = mysql_fetch_assoc($newcat));
$rows = mysql_num_rows($newcat);
if($rows > 0) {
mysql_data_seek($newcat, 0);
$row_newcat = mysql_fetch_assoc($newcat);
}
?>
</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 » Wed Oct 08, 2003 3:20 pm
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.
mrvanjohnson
Forum Contributor
Posts: 137 Joined: Wed May 28, 2003 11:38 am
Location: San Diego, CA
Post
by mrvanjohnson » Wed Oct 08, 2003 4:02 pm
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 » Thu Oct 09, 2003 7:02 am
I have a similar problem. I have a dropdown box like this:
Code: Select all
<select name='month'>
<option value='01'>January</option>
<option value='02'>February</option>
<option value='03'>March</option>
<option value='04'>April</option>
<option value='05'>May</option>
<option value='06'>June</option>
<option value='07'>July</option>
<option value='08'>August</option>
<option value='09'>September</option>
<option value='10'>October</option>
<option value='11'>November</option>
<option value='12'>December</option>
</select>
If I pulled out a value from my database and assigned it to a variable, for example:
how would I make PHP echo out SELECTED in the correct place?
Code: Select all
<select name='month'>
...
...
...
<option value='10' SELECTED>October</option>
...
</select>
Nay
Forum Regular
Posts: 951 Joined: Fri Jun 20, 2003 11:03 am
Location: Brisbane, Australia
Post
by Nay » Thu Oct 09, 2003 7:38 am
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 » Thu Oct 09, 2003 1:29 pm
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