Page 1 of 1

php list box from database

Posted: Mon Oct 10, 2005 8:03 am
by mhouldridge
Hi,

I have an update page in which I have echo'd database results like below;

Code: Select all

<? echo $formVars["ThisField"]; ?>
This works for text boxes, however with a list box it does not.


Any ideas?

Posted: Mon Oct 10, 2005 8:04 am
by Nathaniel
What do you mean by list box? Radio buttons? Could you show us your HTML?

Posted: Mon Oct 10, 2005 8:13 am
by mhouldridge
Sorry,

I mean selection box with say yes or no values...

I am trying to populate it from my database query as follows;

Code: Select all

<?php
session_start(); 
foreach($HTTP_POST_VARS as $varname => $value)
        $formVars[$varname]=$value;
$db = mysql_connect("localhost","",""); 
mysql_select_db ('audit'); 
$query="SELECT * FROM dedicated WHERE asset = \"".$_GET['varl']."\"";
$result=mysql_query($query);
$row=mysql_fetch_array($result);
$formVars = array();
$formVars["title"]=$row["title"];
$formVars["customer"]=$row["customer"];
$formVars["type"]=$row["type"];
$formVars["serial"]=$row["serial"];
$formVars["os"]=$row["os"];
$formVars["oemfull"]=$row["oemfull"];
$formVars["oslicense"]=$row["oslicense"];
$formVars["IP"]=$row["IP"];
$formVars["location"]=$row["location"];
$formVars["processor"]=$row["processor"];
$formVars["memory"]=$row["memory"];
$formVars["motherboard"]=$row["motherboard"];
$formVars["disksize"]=$row["disksize"];
$formVars["value"]=$row["value"];
$formVars["value2"]=$row["value2"];
$formVars["networkcard"]=$row["networkcard"];
$formVars["graphics"]=$row["graphics"];
$formVars["asset"]=$row["asset"];
$formVars["stopped"]=$row["stopped"];
$formVars["recon"]=$row["recon"];
mysql_close($db);
?>

taking the ["recon"] value for the selection box.


thanks

Posted: Mon Oct 10, 2005 8:16 am
by Nathaniel

Code: Select all

<select name="recon">
<option value="yes"<?PHP if ( $formVars['recon'] == 'yes' ) { ?> selected="selected"<?PHP } ?>>Yes</option>
<option value="no"<?PHP if ( $formVars['recon'] == 'no' ) { ?> selected="selected"<?PHP } ?>No</option>
</select>
I think that may be what you want.

Posted: Mon Oct 10, 2005 8:35 am
by mhouldridge
yep,

that's sorted it - Thanks for you help, much appreciated.

Posted: Mon Oct 10, 2005 9:19 am
by mhouldridge
Er,

Just tested this - I thought it was working but it does not..

It does not display the value, just Yes all the time.

Posted: Mon Oct 10, 2005 9:28 am
by Jenk
That's because the value is set to yes or no.

Posted: Mon Oct 10, 2005 9:32 am
by mhouldridge
Uhh?

Sorry, I dont follow...

I need this to display the formVar['recon'] value in the selection box.

Posted: Mon Oct 10, 2005 9:40 am
by mhouldridge
Hi,


That's definately working now.

One more question. How could I display "no" in the selection box if there is no value entered into that field.

Could I use another if statement to take "" ?

Posted: Mon Oct 10, 2005 9:44 am
by mhouldridge
Ignore that last one, I answered it myself.

Cheers peeps!