doucie Posted April 11, 2007 Share Posted April 11, 2007 Hello, I'm new to all this php business, so I hope you can help? I'm trying to write a function which gets all the client names from a table and displays them in a drop down list. My function is displaying a form with nothing in! Help! function get_clients() { $db=mysqli_connect('localhost','root','','debtnet'); if(mysqli_connect_errno()) { echo 'Could not connect to the database'; exit; } $query="SELECT client_name FROM groups"; $result=mysqli_query($db,$query); ?> <form action="group.php" method="POST" /> <select name="Client" id="Client" /> <?php while($list=mysqli_fetch_array($result)){ echo "<option value='$list'</option> </form>"; } } Link to comment https://forums.phpfreaks.com/topic/46644-solved-help-with-function-to-display-form-from-database/ Share on other sites More sharing options...
per1os Posted April 11, 2007 Share Posted April 11, 2007 <?php function get_clients() { $db=mysqli_connect('localhost','root','','debtnet'); if(mysqli_connect_errno()) { echo 'Could not connect to the database'; exit; } $query="SELECT client_name FROM groups"; $result=mysqli_query($db,$query); ?> <form action="group.php" method="POST" /> <select name="Client" id="Client" /> <?php while($list=mysqli_fetch_array($result)){ echo "<option value='".$list['client_name']."'>".$list['client_name']."</option>"; } echo "</form>"; } ?> try that. EDIT: fixed some syntax I noticed was wrong. Link to comment https://forums.phpfreaks.com/topic/46644-solved-help-with-function-to-display-form-from-database/#findComment-227162 Share on other sites More sharing options...
only one Posted April 11, 2007 Share Posted April 11, 2007 try closing your option tag... echo "<option value='$list'></option></form>"; Link to comment https://forums.phpfreaks.com/topic/46644-solved-help-with-function-to-display-form-from-database/#findComment-227163 Share on other sites More sharing options...
craygo Posted April 11, 2007 Share Posted April 11, 2007 try this <?php function get_clients() { $db=mysqli_connect('localhost','root','','debtnet'); if(mysqli_connect_errno()) { echo 'Could not connect to the database'; exit; } $query="SELECT client_name FROM groups"; $result=mysqli_query($db,$query); $results = '<form action="group.php" method="POST" />'; $results .= '<select name="Client" id="Client" />'; while($list=mysqli_fetch_array($result)){ $results .= "<option value='".$list['client_name']."'>".$list['client_name']."</option>"; } $results .= "</form>"; return $results; } echo get_clients(); ?> leaving it the way you have it you are create a form for every row of data in the table Ray Link to comment https://forums.phpfreaks.com/topic/46644-solved-help-with-function-to-display-form-from-database/#findComment-227166 Share on other sites More sharing options...
doucie Posted April 11, 2007 Author Share Posted April 11, 2007 Thanks frost110 and every one else. The frost110 way works just fine. Just one question why the $list[client_name] bit twice??? Link to comment https://forums.phpfreaks.com/topic/46644-solved-help-with-function-to-display-form-from-database/#findComment-227173 Share on other sites More sharing options...
per1os Posted April 11, 2007 Share Posted April 11, 2007 The value needs something assigned to it, the second time displays it as an item in the select box. Link to comment https://forums.phpfreaks.com/topic/46644-solved-help-with-function-to-display-form-from-database/#findComment-227175 Share on other sites More sharing options...
doucie Posted April 11, 2007 Author Share Posted April 11, 2007 Of course, just like a regular old form. Cheers. Link to comment https://forums.phpfreaks.com/topic/46644-solved-help-with-function-to-display-form-from-database/#findComment-227178 Share on other sites More sharing options...
craygo Posted April 11, 2007 Share Posted April 11, 2007 yes you can have the value you want "value="blah blah blah"" but show friendly text in the dropdown menu <option value="1">ON</option> will display ON in the dropdown but will pass "1" as the value. If you do not use a value it will pass what is used in between the option tags <option>BlahBlahBlah</option> Usually don't need a value parameter if the actual value is the same as what is being displayed Ray Link to comment https://forums.phpfreaks.com/topic/46644-solved-help-with-function-to-display-form-from-database/#findComment-227183 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.