0

I have a table in MySQL DB which stores Menu served by restaurant on each weekday and in time slots.

Table structure is as below:

  i_type      i_name    i_cost   i_day   i_start     i_end
  --------------------------------------------------------
  Breakfast   Prantha   20       Sunday   07:00      11:00
  Lunch       Special   80       Sunday   11:01      15:00
  Dinner      Special   100      Sunday   15:01      21:00
  Breakfast   Prantha1  50       Monday   07:00      11:00
  Lunch       Special1  70       Monday   11:01      15:00
  Dinner      Special1  130      Monday   15:01      21:00

I want to display menu as below:

<b>Sunday</b><br/>
    Breakfast<hr/>Prantha       20     Add<br/><br/>
    Lunch<br/><hr/>Special      80     Add<br/><br/>
    Dinner<br/><hr/>Special     100    Add<br/><hr/>
<b>Monday</b><br/>
   Breakfast<hr/>Prantha       20     Add<br/><br/>
   Lunch<br/><hr/>Special      80     Add<br/><br/>
   Dinner<br/><hr/>Special     100    Add<br/><hr/>

Currently i am handling it as below:

$sql = SELECT * FROM `tabl1` WHERE 1;      
$data = DB::instance()->prepare($sql)->execute()->fetchAll();
foreach($data as $row){
       //print values
 }

but this approach print Sunday and Monday multiple times. 

3 Answers 3

1

Just keep track of which day you've output, e.g. use a "state machine":

$previousday = null;
while($row = ... fetch from db... ) {
    if ($row['day'] != $previousday) {
       ... output day header
       $previousday = $row['day'];
    }
    ... output row data ...
}
Sign up to request clarification or add additional context in comments.

1 Comment

this is perfect and served the purpose
1

It will of course print three Sundays and three Mondays.

You could for example Select all different days first (SELECT DISTINCT (i_day) FROM menu), then for each day select all menus (SELECT * FROM menu WHERE i_day = ?)

$sql = "SELECT DISTINCT (i_day) FROM menu";      
$data = DB::instance()->prepare($sql)->execute()->fetchAll();
foreach($data as $row){
  // Add all i_day's to an associative array $i_days
}
foreach($i_days as $day) {
  $sql = "SELECT * FROM menu WHERE i_day = $day";      
  $data = DB::instance()->prepare($sql)->execute()->fetchAll();
    foreach($data as $row){
      // Print values
    }
}

Comments

-1

Try this:

$stat = "SELECT * from TABLE_NAME";
$conn -> query ($stat);
foreach($conn -> query($stat) as $menu) {

echo $menu["i_day"];
echo $menu["i_type"];
echo $menu["i_name"];
echo $menu["i_cost"];

}

Format the above data basing on your need. I've just provided you the basic code.

Alternately,

$stat = "SELECT * from TABLE_NAME where i_day = 'DAY_GOES_HERE'";
$conn -> query ($stat);
foreach($conn -> query($stat) as $menu) {

echo $menu["i_type"];
echo $menu["i_name"];
echo $menu[i_cost];

}

Repeat it for each day.

1 Comment

because same has been described in above answer.

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.