madspof Posted January 7, 2007 Share Posted January 7, 2007 can you list a row from a mysql in a form list if so can anyone give me an insight i have tried namy things but none seem to work Quote Link to comment https://forums.phpfreaks.com/topic/33158-can-you-list-a-row-from-a-mysql-in-a-form-list/ Share on other sites More sharing options...
Jessica Posted January 7, 2007 Share Posted January 7, 2007 Post the code.What is a form list? Do you mean a drop down?Explain more (and read How To Ask Questions in my signature.) Quote Link to comment https://forums.phpfreaks.com/topic/33158-can-you-list-a-row-from-a-mysql-in-a-form-list/#findComment-154580 Share on other sites More sharing options...
madspof Posted January 7, 2007 Author Share Posted January 7, 2007 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> </p></body></html> Quote Link to comment https://forums.phpfreaks.com/topic/33158-can-you-list-a-row-from-a-mysql-in-a-form-list/#findComment-154590 Share on other sites More sharing options...
Jessica Posted January 7, 2007 Share Posted January 7, 2007 What is the problem? Quote Link to comment https://forums.phpfreaks.com/topic/33158-can-you-list-a-row-from-a-mysql-in-a-form-list/#findComment-154600 Share on other sites More sharing options...
madspof Posted January 7, 2007 Author Share Posted January 7, 2007 If you run the code you get some thing like this in the list box test5 <br>madspof <br>>test5 <br>madspof <br> when i realy want the drop down list to show something like this:madspoftest Quote Link to comment https://forums.phpfreaks.com/topic/33158-can-you-list-a-row-from-a-mysql-in-a-form-list/#findComment-154606 Share on other sites More sharing options...
Jessica Posted January 7, 2007 Share Posted January 7, 2007 I don't quite understand. Do you have this live somewhere, that you can post a link to the page so I can see the resulting html? Quote Link to comment https://forums.phpfreaks.com/topic/33158-can-you-list-a-row-from-a-mysql-in-a-form-list/#findComment-154611 Share on other sites More sharing options...
madspof Posted January 7, 2007 Author Share Posted January 7, 2007 yes i have it live at www.deskfun.co.nr/test.php i want the drop downlist to just show the userid colums i.e:madspofdaveboblulu Quote Link to comment https://forums.phpfreaks.com/topic/33158-can-you-list-a-row-from-a-mysql-in-a-form-list/#findComment-154614 Share on other sites More sharing options...
Jessica Posted January 7, 2007 Share Posted January 7, 2007 Try just this for now, and then work on adding the selected part. <option><?=$tmp?></option> Quote Link to comment https://forums.phpfreaks.com/topic/33158-can-you-list-a-row-from-a-mysql-in-a-form-list/#findComment-154617 Share on other sites More sharing options...
madspof Posted January 7, 2007 Author Share Posted January 7, 2007 okay that works kinda but now i need them to seperate i.e maybye using a loop have a look again see what i mean www.deskfun.co.nr/test.php Quote Link to comment https://forums.phpfreaks.com/topic/33158-can-you-list-a-row-from-a-mysql-in-a-form-list/#findComment-154621 Share on other sites More sharing options...
Jessica Posted January 7, 2007 Share Posted January 7, 2007 Yes, so make a loop. Instead of making a string of the results added together, make an array.$users[] = $row->userid;Then use foreach.Try that and if you still need help and you really tried...post again ;) Quote Link to comment https://forums.phpfreaks.com/topic/33158-can-you-list-a-row-from-a-mysql-in-a-form-list/#findComment-154634 Share on other sites More sharing options...
madspof Posted January 7, 2007 Author Share Posted January 7, 2007 hi i looked up some information on arrays but i dont know much on them could u please give me an idea of what kind of code i need Quote Link to comment https://forums.phpfreaks.com/topic/33158-can-you-list-a-row-from-a-mysql-in-a-form-list/#findComment-154650 Share on other sites More sharing options...
Jessica Posted January 7, 2007 Share Posted January 7, 2007 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] Quote Link to comment https://forums.phpfreaks.com/topic/33158-can-you-list-a-row-from-a-mysql-in-a-form-list/#findComment-154654 Share on other sites More sharing options...
emehrkay Posted January 7, 2007 Share Posted January 7, 2007 look at this example$count = count($arr); //where $arr is your result set$sel = "<select>\n"for($i = 0; $i < $count; $i++){$sel .= "<option value=\"". $arr[$i] ."\">". $arr[$i] ."</option>\n";}$sel .= "</select>\n"; Quote Link to comment https://forums.phpfreaks.com/topic/33158-can-you-list-a-row-from-a-mysql-in-a-form-list/#findComment-154656 Share on other sites More sharing options...
emehrkay Posted January 7, 2007 Share Posted January 7, 2007 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 Quote Link to comment https://forums.phpfreaks.com/topic/33158-can-you-list-a-row-from-a-mysql-in-a-form-list/#findComment-154661 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.