Jump to content

can you list a row from a mysql in a form list


madspof

Recommended Posts

okay i think this was one on the code i got close to getting to work:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Untitled Document</title>
</head>

<body>
<?php

// Set the various database connect information.  Host is usually
// localhost.  If no password, just leave blank.  This information is 
// given by your web host OR if your own server, you setup yourself.
// See php manual for setup instructions.
$db_host  = 'localhost';
$db_user  = 'cms';
$db_pass  = '';
$db_name  = 'phpsms';

// More then one table can be used but in ths example we use just
// one. :-)  And in many scripts just one is used.  And it's easiest
// to comment on.
$db_table = 'userdata';

// ---- END CONFIGURATIONS ------------------------------------------//

// Make a connect to mysql.  Through this connect many databases
// and tables can be selected.  $conn will be what we call this
// connection.
$conn = mysql_connect($db_host,$db_user,$db_pass);

if ($conn == true) {


// Select the database.
  mysql_select_db($db_name,$conn); 

// We are selecting all fields (*) from database and using the $conn
// database connection.  By default $conn would be used but we're
// learning so may as well not be lazy :-)  Usually you wouldn't 
// select * but for the lazy and unsure, it works nicely.
//
// This is a very powerful function, one can query *any* SQL statement
// and do many things, like update/delete/insert/select data.  A good 
// basic SQL tutorial is : sqlcourse.com .  The result of this 
// query is named $result
  $result = mysql_query("SELECT * from $db_table",$conn);

// While a row exists to equal the fetched object (row) of data
// continue to loop until end of fetch (at the end)
// The way data is called may be new.  $row->dbfieldname . See 
// the manual on mysql_fetch_object() for more info.  Other functions
// can just as easily have been used, like mysql_fetch_array.  That method 
// one would use format as : $row['id']; , $row['name'] , etc.  See manual 
// for all the possible (and beautiful) tools that are available.
    while($row = mysql_fetch_object($result)) {

// These will loop through entire database.  $tmp keeps getting
// appended to so it's getting very large. This is what's printed
// and where one makes look pretty :-)  Below the database fields
// id, name and url are being printed from the $db_table table.  And, 
// the .= means it appends (concatenates) to $tmp so $tmp grows larger
// and later.  See : http://www.zend.com/zend/tut/using-strings.php
// Again, id,name and url are just examples ... replace according to 
// your database.

     
        $tmp .= "$row->userid <br>\n";   
   

// The while loop ends here at this brace so the above cycles through until 
// all data is gathered.
    }

// Else we are not connected ($conn == false) so print error.
} else {
    echo 'could not connect to database : '. mysql_error();
}

// Print the pulled data.
print $tmp;

?>
<form name="form1" method="post" action="">
  <select name="select" size="1">
    <option value="" <?php if (!(strcmp("", print $tmp))) {echo "SELECTED";} ?>><?php print $tmp; ?></option>
  </select>
</form>
<p>&nbsp;</p>
</body>
</html>
I posted some the code you need.
Replace
[code]$tmp .= "$row->userid
\n";    [/code]

with
[code]$users[] = $row->userid;[/code]

Then replace
[code]
print $tmp;

?>
<form name="form1" method="post" action="">
  <select name="select" size="1">
    <option value="" <?php if (!(strcmp("", print $tmp))) {echo "SELECTED";} ?>><?php print $tmp; ?></option>
  </select>
[/code]

With:
[code]?>
<form name="form1" method="post" action="">
  <select name="select" size="1">
<?
foreach($users AS $user){
    ?><option><?=$user?></option><?
}
?>
  </select>
[/code]
oh i see you're using a while statement ill revise what i wrote

$sel = "<select>\n"
while($row = mysql_fetch_object($result)) {
$sel .= "<option value=\"". $row->val ."\">". $row->val ."</option>\n";
}
$sel .= "</select>\n";

then where you want the dropdown displayed do

<?php echo $sel; ?>
or just echo $sel if you're already within the php tags

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.