0

Here I stored the data in JSON format and want to display in HTML table. Here I have tried to fetch but only first row has shown, other are not showing and display some errors like this:

Notice: Undefined index: tuesday_start in C:\xampp\htdocs\Hospital\adminpanel\show_schedule.php on line 262

Notice: Undefined index: tuesday_end in C:\xampp\htdocs\Hospital\adminpanel\show_schedule.php on line 263

Notice: Undefined index: wednesday_start in C:\xampp\htdocs\Hospital\adminpanel\show_schedule.php on line 267

Notice: Undefined index: wednesday_end in C:\xampp\htdocs\Hospital\adminpanel\show_schedule.php on line 268

and so on

Here the data is store like this:

[{"monday_start":"09.00 A.M","monday_end":"10.00 A.M"},{"tuesday_start":"10.00 A.M","tuesday_end":"11.00 A.M"},{"wednesday_start":"Select Date","wednesday_end":"Select Date"},{"thursday_start":"Select Date","thursday_end":"Select Date"},{"friday_start":"Select Date","friday_end":"Select Date"},{"saturday_start":"Select Date","saturday_end":"Select Date"},{"sunday_start":"Select Date","sunday_end":"Select Date"}]

I have tried to fetch the values like following:

    <table class="table table-hover mb-0">
        <thead>
            <tr>
                <th>Day</th>
                <th>Time (From)</th>
                <th>Time (To)</th>
            </tr>
            </thead>
        <tbody>

               <?php 
            $stmt = $conn->prepare("SELECT * FROM schedule WHERE user_id=? ");
            $stmt->bind_param("s", $_GET['userid']);
            $stmt->execute();
            $result = $stmt->get_result();
            if($result->num_rows === 0)  exit('No rows');
            while($row = $result->fetch_assoc()) {

            $times = json_decode($row['available_days'],true);

            if (is_array($times) || is_object($times)) {

            foreach($times as $key => $object) {

?>


                <tr>
                    <td>Monday</td>
                    <td><?php echo   $object['monday_start']; ?></td>
                    <td><?php echo  $object['monday_end']; ?></td>
                </tr>

                <tr>
                    <td>TuesDay</td>
                    <td><?php echo   $object['tuesday_start']; ?></td>
                    <td><?php echo  $object['tuesday_end']; ?></td>
                </tr>
                <tr>
                    <td>wednesday</td>
                    <td><?php echo   $object['wednesday_start']; ?></td>
                    <td><?php echo  $object['wednesday_end']; ?></td>
                </tr>
                <tr>
                    <td>thursday</td>
                    <td><?php echo   $object['thursday_start']; ?></td>
                    <td><?php echo  $object['thursday_end']; ?></td>
                </tr>
                <tr>
                    <td>friday</td>
                    <td><?php echo   $object['friday_start']; ?></td>
                    <td><?php echo  $object['friday_end']; ?></td>
                </tr>
                <tr>
                    <td>saturday</td>
                    <td><?php echo   $object['saturday_start']; ?></td>
                    <td><?php echo  $object['saturday_end']; ?></td>
                </tr>
                <tr>
                    <td>sunday</td>
                    <td><?php echo   $object['sunday_start']; ?></td>
                    <td><?php echo  $object['sunday_end']; ?></td>
                </tr>
                        <?php  } }   }

                            ?>
                </tbody>
            </table>

Please help me...

ROOT
  • 11,363
  • 5
  • 30
  • 45

1 Answers1

2

The issue here is that, you are trying to access data that is not available with each iteration, for instance the first iteration of the foreach it will pull the first array element which only have {"monday_start":"09.00 A.M","monday_end":"10.00 A.M"} array elements, but for the rest of your code in foreach you are trying to access other dates values, like wednesday_start, wednesday_end.

so to fix the warning you have to check if the current $object does have attributes you are trying to access like this:

...
<?php if(!empty($object['monday_start']) && !empty($object['monday_end']): ?>
<tr>
  <td>Monday</td>
  <td><?php echo $object['monday_start']; ?></td>
  <td><?php echo $object['monday_end']; ?></td>
</tr>
<?php endif; ?>
...

you have to do it for all other element.

Heikanu
  • 5
  • 3
ROOT
  • 11,363
  • 5
  • 30
  • 45