Jump to content

[SOLVED] while help


ngreenwood6

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

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.