0

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>
  • Problem 1

the end Result is something Like

Monday
Friday
Saturday
Sunday
Thursday
Tuesday
Wednesday
  • Problem 2

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?

10
  • is there a typo in second last line $option5 .=? Commented Feb 19, 2014 at 12:46
  • $query = "select day_of_week, day from date_table order by day_of_week"; and also re-write $option instead of $option5. Commented Feb 19, 2014 at 12:48
  • 1
    Why are you using MySQL at all for this!?!? Commented Feb 19, 2014 at 12:48
  • What values are in $_REQUEST["sday"]? Commented Feb 19, 2014 at 12:49
  • @Strawberry some Calendar systems store days in the database. Commented Feb 19, 2014 at 12:50

4 Answers 4

1

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>'
Sign up to request clarification or add additional context in comments.

4 Comments

+1 for ($row['day_of_week'] == $_REQUEST["sday"]) ? ' selected ' : '';
Problem is in "sday". If I choose Sunday, It gives value 2 or 3 or 7
@kaulainais can you GROUP BY day_of_week? There's no reason why all three of them should be selected, unless your input is incorrect.
I think I know whee the problem is. Thanks Shamil
0

you use variable $option5, instead of $option

1 Comment

I changed it. that is not an issue
0

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

Comments

0

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>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.