androidnation
androidnation

Reputation: 636

No data echoed inside html table

Var dump result

enter image description here

I am fetching data in MySQL into an HTML table:

<div id="toggleDiv" class="">
  <div class="box-body" id="toggleDiv_2">
    <div class="row">
      <div class="col-md-6">
        <?php
        $select_patient_info_cash =
            "SELECT * FROM patient_info WHERE id_logged = :id_logged".
            " AND patient_id = :patient_id AND payment_type = :pt";
        $select_patient_info_cash_stmt = 
            $conn->prepare($select_patient_info_cash);
        $select_patient_info_cash_stmt->bindValue(":id_logged", $id_logged);
        $select_patient_info_cash_stmt->bindValue(":patient_id", $patient_id);
        $select_patient_info_cash_stmt->bindValue(":pt", "cash");
        $select_patient_info_cash_stmt->execute();
        $select_patient_info_cash_stmt->fetch(PDO::FETCH_ASSOC);
        $select_patient_info_cash_stmt_count =
            $select_patient_info_cash_stmt->rowCount();
        if($select_patient_info_cash_stmt_count > 0){ ?>
        <table style="text-align:center"
               class="table table-bordered table-striped table-hover">
          <thead>
            <tr>
              <th>Project Description</th>
              <th>Project Cost</th>
              <th>Date Of Pay</th>
            </tr>
          </thead>
            <?php foreach ($select_patient_info_cash_stmt as $cash) { ?>
            <tr>
              <td><?php echo $cash['project'] ?></td>
              <td><?php echo $cash['project_cost'] ?></td>
              <td><?php echo $cash['date_now'] ?></td>
            </tr>
            <?php } ?>
        </table>
    <?php } else { ?>

    <?php } ?>
</div><!-- /.col -->

Now I test it for a user that have data in patient info where payment_type != cash, and the <thead> didn't show up. But when I test it where payment_type=cash the <thead> shows up but no data are echoed into line.

enter image description here

It should show me 2 new lines after <thead> and I can't figure out why data are not displayed on the page

Upvotes: 0

Views: 91

Answers (1)

hungneox
hungneox

Reputation: 9829

I think you miss ->fetch() from your prepared statement. According to PHP docs:

<?php
$stmt = $dbh->prepare("SELECT * FROM REGISTRY where name = ?");
if ($stmt->execute(array($_GET['name']))) {
  while ($row = $stmt->fetch()) {
    print_r($row);
  }
}
?>

Therefore you need to modify your code to something like:

<?php while ($cash = $select_patient_info_cash_stmt->fetch()) {  ?>
  <tr>
    <td><?php echo $cash['project'] ?></td>
    <td><?php echo $cash['project_cost'] ?></td>
    <td><?php echo $cash['date_now'] ?></td>
  </tr>
<?php } ?>

I also suggest that you should use a MVC framework or some sort of template engines. Mixing PHP and HTML is a very bad practice.

References: http://php.net/manual/en/pdo.prepared-statements.php

Upvotes: 3

Related Questions