will35010 Posted January 18, 2010 Share Posted January 18, 2010 I have this function that creates a select menu. It was working fine until I changed the function to use return vs. echo. Now it only returns the very first thing in the database like the while loop isn't working. Any ideas? Thanks! //function to get chief complaint and create select menu options function getCC() { require('db.php'); $query = mysqli_query($conn, "SELECT complaint FROM complaints"); while($row = mysqli_fetch_array($query)) { return "<option value=\"" . $row['complaint'] . "\">".$row['complaint']."</option>"; } } Quote Link to comment https://forums.phpfreaks.com/topic/188944-function-to-create-select-menu-problem/ Share on other sites More sharing options...
will35010 Posted January 18, 2010 Author Share Posted January 18, 2010 Maybe it will will help if I show all the related code: Where the function is being used: echo " <label>Chief Complaint: <select name='cc' id='cc'> <option value=''></option> ".getCC()." </select> </label>"; The html source with only one option produced when there are four in the database: <label>Chief Complaint: <select name='cc' id='cc'> <option value=''></option> <option value="Chest Pain">Chest Pain</option> </select> </label> Quote Link to comment https://forums.phpfreaks.com/topic/188944-function-to-create-select-menu-problem/#findComment-997663 Share on other sites More sharing options...
laffin Posted January 18, 2010 Share Posted January 18, 2010 Problem is you only get a string of the first result from sql. You should start with a empty string, append results, and than return the string function getCC() { require('db.php'); $selects=null; $query = mysqli_query($conn, "SELECT complaint FROM complaints"); while($row = mysqli_fetch_array($query)) { $selects .= "<option value=\"" . $row['complaint'] . "\">".$row['complaint']."</option>"; } return $selects; } Quote Link to comment https://forums.phpfreaks.com/topic/188944-function-to-create-select-menu-problem/#findComment-997666 Share on other sites More sharing options...
will35010 Posted January 18, 2010 Author Share Posted January 18, 2010 Problem is you only get a string of the first result from sql. You should start with a empty string, append results, and than return the string function getCC() { require('db.php'); $selects=null; $query = mysqli_query($conn, "SELECT complaint FROM complaints"); while($row = mysqli_fetch_array($query)) { $selects .= "<option value=\"" . $row['complaint'] . "\">".$row['complaint']."</option>"; } return $selects; } Thank you! That works. Quote Link to comment https://forums.phpfreaks.com/topic/188944-function-to-create-select-menu-problem/#findComment-997672 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.