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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.