0

I'm querying some data from a mysql database and this all works perfectly on my live version of the site hosted at blahblah. I then re-created the database on my local machine for development so I can play with some real data. My issue is, in the HTML section of the code, It's giving me

Notice: Undefined index: boarded

It says this for each of the entries it's trying to echo. I know for a fact that the array it's pulling from does have an element "boarded" and so on.

Here is the first entry of the array dump.

results.array(101){ [0]=> array(7) { ["boarded"]=>
> string(1) "5" ["stop"]=> string(11) "Anthony - N" ["timestamp"]=>
> string(19) "2019-02-02 19:06:13" ["date"]=> string(10) "2019-02-02"
> ["loop"]=> string(9) "Blue Loop" ["driver"]=> string(5) "Yikes"
> ["id"]=> string(3) "368" }

HTML Section

 <table>
   <tr>
    <th>Boarded</th>
    <th>Stop</th>
    <th>Timestamp</th>
    <th>Date</th>
    <th>Loop</th>
    <th>Driver</th>
    <th>ID</th>
  </tr>
   <? foreach ($log as $logs): ?>
    <tr>
        <td><?= $logs["boarded"] ?></td>
        <td><?= $logs["stop"] ?></td>
        <td><?= $logs["timestamp"] ?></td>
        <td><?= $logs["date"] ?></td>
        <td><?= $logs["loop"] ?></td>
        <td><?= $logs["driver"] ?></td>
        <td><?= $logs["id"] ?></td>
    </tr>
   <? endforeach ?>
  </ul>
  </table>

Array being created

if($result = mysqli_query($con,$sql))
{
  $cr = 0;
  while($row = mysqli_fetch_assoc($result))
  {
    $logs[$cr]['boarded'] = $row['boarded'];
    $logs[$cr]['stop'] = $row['stop'];
    $logs[$cr]['timestamp'] = $row['timestamp'];
    $logs[$cr]['date'] = $row['date'];
    $logs[$cr]['loop'] = $row['loop'];
    $logs[$cr]['driver'] = $row['driver'];
    $logs[$cr]['id'] = $row['id'];
    $cr++;
  }
}

Any ideas?

Keithers
  • 354
  • 6
  • 23
  • do you want to ignore the error or skip the entry which do not have "boarded" ? – sumit Feb 04 '19 at 01:25
  • @sumit No I want the entire array to output to the table. It works fine on the live site. My issue is that it's not printing when it should be since it's there. (on my local machine with xampp) – Keithers Feb 04 '19 at 01:32

1 Answers1

0

You might having an issue on your <? foreach ($log as $logs): ?>, it seems to be inverted.

The below works just fine here.

<?php
if($result = mysqli_query($con,$sql))
{
  $logs = array();
  while($row = mysqli_fetch_assoc($result))
  {
    array_push( $logs, $row);
  }
}
?>

HTML

<table>
   <tr>
    <th>Boarded</th>
    <th>Stop</th>
    <th>Timestamp</th>
    <th>Date</th>
    <th>Loop</th>
    <th>Driver</th>
    <th>ID</th>
  </tr>
   <?php foreach ($logs as $log): ?>
    <tr>
        <td><?php echo $log["boarded"]; ?></td>
        <td><?php echo $log["stop"]; ?></td>
        <td><?php echo $log["timestamp"]; ?></td>
        <td><?php echo $log["date"]; ?></td>
        <td><?php echo $log["loop"]; ?></td>
        <td><?php echo $log["driver"]; ?></td>
        <td><?php echo $log["id"]; ?></td>
    </tr>
   <?php endforeach; ?>
  </ul>
  </table>
caiovisk
  • 3,667
  • 1
  • 12
  • 18