简体   繁体   中英

php mysql display the records in table

I'm doing an availability system. I need to display the records in a table.

My system is basically about real estate which tracks if a property is available or not. Sample is I have 20 floors in condominium each floor has 10 units so I need to loop the number of floors and also display each unit under a specific floor.

1st flr | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 2nd flr | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10

This is what I've tried so far:

<table class="table table-bordered">
            <?php
                for($i=0; $row3 = $stmt3->fetch(); $i++){
                  $floor = $row3['floor'];
            ?>
          <tr>
            <td><?php echo $floor; ?></td>
            <?php
                for($i=0; $row4 = $stmt4->fetch(); $i++){
                  $unit_code = $row4['unit_code'];
            ?>
            <td><?php echo $unit_code; ?></td>
            <?php 
              }
            ?>

          </tr>
          <?php 
            }
          ?>
        </table>

But it's just display all the records in the first floor.

This is what actually happened:

1st flr| 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 
2nd flr

What could be the correct way to achieve my needs? Any ideas? I would gladly appreciate your help. Thanks.

UPDATE:

  $stmt3 = $conn->prepare( "SELECT DISTINCT floor
  FROM tblunitsmaster 
  WHERE project_code = :code" );
  $stmt3->execute(array(':code' => $code));


  $stmt4 = $conn->prepare( "SELECT unit_code
  FROM tblunitsmaster 
  WHERE project_code = :code
  AND floor = :floor
  AND sub_project_code = 'SUB-AX0001'" );

I don't know if you have already solved it, but Satish Sharma code has a typo on line 6. The line should be

$floor = $row3['floor'];

This will solve your problem. I really should comment on the answer but don't have enough points.

Updated code below

UPDATE: Latest update for each floor with its own set of units.

<?php
$code = 'so';
$stmt3 = $pdo->prepare( "SELECT DISTINCT floor
FROM tblunitsmaster 
WHERE project_code = :code" );
$stmt3->execute(array(':code' => $code));


$stmt4 = $pdo2->prepare( "SELECT unit_code
FROM tblunitsmaster 
WHERE project_code = :code
AND floor = :floor
AND sub_project_code = 'SUB-AX0001'" );

?>
<table border="1px" class="table table-bordered">
    <?php
    $rows3 = $stmt3->fetchAll();

    foreach ($rows3 as $row3) {
        $floor = $row3['floor'];
        ?>
        <tr>
            <td><?php echo $floor; ?></td>
            <?php
            $stmt4->execute(array(':code' => $code, ':floor' => $floor));
            $rows4 = $stmt4->fetchAll();            
            foreach ($rows4 as $row4) {
                $unit_code = $row4['unit_code'];
                ?>
                <td><?php echo $unit_code; ?></td>
                <?php
            }
            ?>

        </tr>
        <?php
    }
    ?>
</table>

OUTPUT:

1st flr 1   2   3   4   5   6   7   8   9   10
2nd flr 1   2   3   4   5   6   7   8   9   10
3rd flr 1   2   3   4   5   6   7   8   9   10

I think this might solve your problem:

<table class="table table-bordered">
<?php
    $stmt3 = $conn->prepare("SELECT DISTINCT `floor` FROM `tblunitsmaster` WHERE `project_code` = :code")->execute(array(':code' => $code))->fetchAll(PDO::FETCH_ASSOC);
    $stmt4 = $conn->prepare("SELECT `unit_code` FROM `tblunitsmaster` WHERE `project_code` = :code AND `floor` = :floor AND `sub_project_code` = 'SUB-AX0001");

    foreach ($stmt3 as $row3){
        $floor = $row3['floor'];

        echo '<tr></td>'.$floor.'</td>';

        if ($stmt4->execute(array(':code' => $code, ':floor' => $floor))){
            foreach ($stmt4->fetchAll(PDO::FETCH_ASSOC) as $row4){
                echo '<td>'.$row4['unit_code'].'</td>';
            }
        }

        echo '</tr>';
    }
?>
</table>

you need to reset your result for $stmt4-

try this

<table class="table table-bordered">
            <?php
                $rows3 = $stmt3->fetchAll();
                $rows4 = $stmt4->fetchAll();
                foreach($rows3 as $row3){
                  $floor = $row['floor'];
            ?>
          <tr>
            <td><?php echo $floor; ?></td>
            <?php
                foreach($rows4 as $row4){
                  $unit_code = $row4['unit_code'];
            ?>
            <td><?php echo $unit_code; ?></td>
            <?php 
              }

            ?>

          </tr>
          <?php 
            }
          ?>
        </table>

for($i=0; $row3 = $stmt3->fetch(); $i++)

What is in $stmt3?

I think you are using the FOR loop wrong. The second part, your $row3 = $stmt3->fetch() , should be an evaluation and not a setting of a variable.

You are probably looking for something like: for($i=0; $stmt3->fetch() <> null; $i++){ $row3 = $stmt3->fetch()

Same for the second FOR loop.

I could be a little wrong with the answer.

It's really not a good idea to put a sql call in a loop. Instead, structure your db call to group the data properly - "select floor, condo_unit group by floor, condo_unit". Then, iterate through the result set.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM