kaulainais
kaulainais

Reputation: 125

MYSQL PHP Select Order By

I have example table date_table, as a basis of data filter 2 columns - day and day_of_week I do query in SQL

select distinct day_of_week,day  from date_table
order by day_of_week

No problem

day_of_week        day
1                  Monday
2                  Tuesday
3                  Wednesday
4                  Thursday
5                  Friday
6                  Saturday
7                  Sunday

Then I implement this into select box in PHP , so I can choose value

$query = "
select distinct day_of_week,day  from date_table
order by day_of_week
";
$res = mysql_query($query) or die ("Error in query: $query. ".mysql_error());
$option = '';
while($row = mysql_fetch_array($res))
    {
if(!empty($_REQUEST["sday"]))
   {
  if($row['day_of_week'] == $_REQUEST["sday"])
    $option .= '<option  value = "'.$row['day_of_week'].'"

    selected="selected">'.$row['day'].'</option>';
   else
   $option .= '<option  value =
    "'.$row['day_of_week'].'">'.$row['day'].'</option>';        
}
  else
 $option .= '<option  value = "'.$row['day_of_week'].'">'.$row['day'].'</option>';  
}

And Select Box

<select name="sday" class="sday">
<option value="">Select Day</option>
<?php echo $option; ?>
</select>

the end Result is something Like

Monday
Friday
Saturday
Sunday
Thursday
Tuesday
Wednesday

When I select Value "Monday" , all fine . It passes value "1"

When I select "Tuesday", It selects value 2,3 and 7. Same with wednesday and sunday.

There are few other select boxes, that are made with same idea, works fine. There are no data duplicates.

What could be the problem?

Upvotes: 0

Views: 192

Answers (4)

Strawberry
Strawberry

Reputation: 33935

I don't get it. Why not do this instead (the array isn't even necessary, but hey)...

<select>
<?php
$days = array('monday','tuesday','wednesday','thursday','friday','saturday','sunday');

for($i=0;$i<count($days);$i++){

echo "<option>$days[$i]</option>\n";

}

?>

</select>

Upvotes: 0

bear
bear

Reputation: 11625

Have you tried to

 ORDER BY day_of_week ASC

You may want to restructure your code a but such that

$selectedString = ($row['day_of_week'] == $_REQUEST["sday"]) ? ' selected ' : '';

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

Upvotes: 1

iubema
iubema

Reputation: 349

Try this code:

    if(!empty($_REQUEST["sday"]))
{
    if($row['day_of_week'] == $_REQUEST["sday"])
    {
        $option .= '<option  value = "'.$row['day_of_week'].'"
        selected="selected">'.$row['day'].'</option>';
    }
    else
    {
        $option .= '<option  value ="'.$row['day_of_week'].'">'.$row['day'].'</option>';
    }
}
else
{
$option .= '<option  value = "'.$row['day_of_week'].'">'.$row['day'].'</option>';

}

Its cleaner and you will dont have problems with bracktes

Upvotes: 0

ziollek
ziollek

Reputation: 1993

you use variable $option5, instead of $option

Upvotes: 0

Related Questions