Jump to content

Recommended Posts

I have the following code:

 

<tr>
<td>School 1</td>
<td>:</td>
<td>
<select name="school_1">
<?php
//school_query
$school_query = "SELECT * FROM schools";

//get the results
$school_results = mysql_query($school_query);

while($school_row = mysql_fetch_array($school_results))
{
?>
<option selected="<?php echo $row['school']; ?>" value="<?php echo $school_row['school']; ?>"><?php echo $school_row['school']; ?></option>
<?php
}
?>
</select>
</td>
</tr>

 

the $row['school'] comes from another query. The problem with this is that it doesn't show the selected option that is in the database. It is just displaying the last value. $row['school'] has a value because I echo'd it. Any help is appreciated

Link to comment
https://forums.phpfreaks.com/topic/136214-solved-while-help/
Share on other sites

I have the following code:

 

<tr>
<td>School 1</td>
<td>:</td>
<td>
<select name="school_1">
<?php
//school_query
$school_query = "SELECT * FROM schools";

//get the results
$school_results = mysql_query($school_query);

while($school_row = mysql_fetch_array($school_results))
{
?>
<option selected="<?php echo $row['school']; ?>" value="<?php echo $school_row['school']; ?>"><?php echo $school_row['school']; ?></option>
<?php
}
?>
</select>
</td>
</tr>

 

the $row['school'] comes from another query. The problem with this is that it doesn't show the selected option that is in the database. It is just displaying the last value. $row['school'] has a value because I echo'd it. Any help is appreciated

try this:

 

<option value="<?php echo $row['school']; ?>"><?php echo $row['school']; ?></option>

<?

while($school_row = mysql_fetch_array($school_results))

{

if($row['school'] != $school_row['school']) {

?>

<option value="<?php echo $school_row['school']; ?>"><?php echo $school_row['school']; ?></option>

<?php

}

}

?>

Link to comment
https://forums.phpfreaks.com/topic/136214-solved-while-help/#findComment-710540
Share on other sites

try this:

 

<option value="<?php echo $row['school']; ?>" selected><?php echo $row['school']; ?></option>

<?

while($school_row = mysql_fetch_array($school_results))

{

if($row['school'] != $school_row['school']) {

?>

<option value="<?php echo $school_row['school']; ?>"><?php echo $school_row['school']; ?></option>

<?php

}

}

?>

 

Use code tags next time please.

 

<option value="<?php echo $row['school']; ?>" selected><?php echo $row['school']; ?></option>
<?php
while($school_row = mysql_fetch_array($school_results))
{
    if($row['school'] != $school_row['school']) {
?>
<option value="<?php echo $school_row['school']; ?>"><?php echo $school_row['school']; ?></option>
<?php
    }
}
?> 

Link to comment
https://forums.phpfreaks.com/topic/136214-solved-while-help/#findComment-710551
Share on other sites

If you want the option selected when $row['school'] == $school_row['school']

 

(I think thats what you asking it to do, ignore me though if its not :P)

 

Then try

 

<tr>
<td>School 1</td>
<td>:</td>
<td>
<select name="school_1">
<?php
//school_query
$school_query = "SELECT * FROM schools";

//get the results
$school_results = mysql_query($school_query);

while($school_row = mysql_fetch_array($school_results))
{
?>
<option <?php if( $school_row['school'] == $row['school']) echo "selected" ?> value="<?php echo $school_row['school']; ?>"><?php echo $school_row['school']; ?></option>
<?php
}
?>
</select>
</td>
</tr>

Link to comment
https://forums.phpfreaks.com/topic/136214-solved-while-help/#findComment-710554
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.