Jump to content

function to create select menu problem


will35010

Recommended Posts

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>";
}
}

Link to comment
https://forums.phpfreaks.com/topic/188944-function-to-create-select-menu-problem/
Share on other sites

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>

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;
}

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.  8)

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.