newbie_07 Posted May 16, 2008 Share Posted May 16, 2008 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 Quote Link to comment Share on other sites More sharing options...
Psycho Posted May 16, 2008 Share Posted May 16, 2008 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; ?> Quote Link to comment 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.