Jump to content

List box problem


newbie_07

Recommended Posts

I have the following code for displaying all the service names in a list box but only the last record is displaying and the rest are not. When i print the service names they all are printing.

$sql1 ="SELECT service_name from services";
$res1 = mysql_query($sql1, $conn);
while($row1 = mysql_fetch_assoc($res1)){
$s_names = $row1['service_name'];
print $s_names;
$s_names .= "<option value='$s_names'>". $s_names . "</option>";
}

Plz help

Link to comment
Share on other sites

You are using the same variable for two different things

 

<?php

while($row1 = mysql_fetch_assoc($res1)){

    //Set the variable to the current value
    $s_names = $row1['service_name'];
    //Prints the variable
    print $s_names;

    //Resets the variable to this new value - but it will get overwritten
    //on the next pass in the loop on the first line above
    $s_names .= "<option value='$s_names'>". $s_names . "</option>";
}

?>

 

Try this:

<?php

while($row1 = mysql_fetch_assoc($res1)){
    $s_names .= "<option value=\"" . $row1['service_name'] . "\">". $row1['service_name'] . "</option>";
}
print $s_names;
?>

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.