简体   繁体   中英

how to show two mysql tables values at one while loop using php

I have created two tables, table1 and table2 table1 includes (id, codes and titles). table2 includes id, table1_id, column1, column2, column3 I want Insert value on table1(codes, titles) and show in table using PHP and insert table2(column1, column2, column3) and show it under table1 values. This my codes

<?php   
include('config.php');
$ret=" SELECT * from table1
";
$stmt= $mysqli->prepare($ret);
//$stmt->bind_param('i',$aid);
$stmt->execute() ;//ok
$res=$stmt->get_result();
$cnt=1;
while($row=$res->fetch_object())
{
?>

 <div class="card shadow">
 <div class="card-header py-0">
 <table class="table">
 <tr >
 <td style="border: none"><?php echo $row->codes; ?></td>
 <td style="border: none"><?php echo $row->titles; ?></td>
 <td style="border: none">edit</td>
 </tr>
 </table>
 </div>
<div class="card-body">
                       
<div class="table-responsive table mt-2" id="dataTable" role="grid" aria- 
describedby="dataTable_info">
<table class="table my-0" id="dataTable">
<thead>
                              
<tr>
<th>column 1</th>
<th>column 2</th>
<th> column 3 </th>
                                       

</tr>
</thead>
<tbody>
                                <?php  



$ret=" SELECT * from table2";
$stmt= $mysqli->prepare($ret);

$stmt->execute();
 $res=$stmt->get_result();
 while($row=$res->fetch_object()    )
 {

 ?>                                 
 <tr>
 <td><?php echo $row->column1 ?></td>
 <td> <?php echo $row->column3 ?></td>
 <td> <?php echo $row->column3 ?></td>
</tr>

 <?php } ?>                           
</tbody>
<tfoot>
<tr></tr>
</tfoot>
</table>
</div>




 <h3 class="text-dark mb-4"> <button class="btn btn-info " data-toggle="modal" data- 
  target="#login_itech3">adding column</button></h3>
                    </div>
                </div>



<br>
<?php } ?>

I expected to shows me like this but when I run that code it shows me, one value

You made two mistakes:

  1. Used same variable name $row for iteration in outer and inner loop
  2. Didn't specified table1_id in where condition in second query Try below code, I am, sure It will work
<?php
include('config.php');
$ret = " SELECT * from table1
";
$stmt = $mysqli->prepare($ret);
//$stmt->bind_param('i',$aid);
$stmt->execute(); //ok
$res = $stmt->get_result();
$cnt = 1;
while ($row1 = $res->fetch_object()) {
?>

    <div class="card shadow">
        <div class="card-header py-0">
            <table class="table">
                <tr>
                    <td style="border: none"><?php echo $row1->codes; ?></td>
                    <td style="border: none"><?php echo $row1->titles; ?></td>
                    <td style="border: none">edit</td>
                </tr>
            </table>
        </div>
        <div class="card-body">

            <div class="table-responsive table mt-2" id="dataTable" role="grid" aria- describedby="dataTable_info">
                <table class="table my-0" id="dataTable">
                    <thead>

                        <tr>
                            <th>column 1</th>
                            <th>column 2</th>
                            <th> column 3 </th>


                        </tr>
                    </thead>
                    <tbody>
                        <?php

                        $ret = " SELECT * from table2 where table1_id = ".$row1->id;
                        $stmt = $mysqli->prepare($ret);

                        $stmt->execute();
                        $res = $stmt->get_result();
                        while ($row2 = $res->fetch_object()) {

                        ?>
                            <tr>
                                <td><?php echo $row2->column1 ?></td>
                                <td> <?php echo $row2->column3 ?></td>
                                <td> <?php echo $row2->column3 ?></td>
                            </tr>

                        <?php } ?>
                    </tbody>
                    <tfoot>
                        <tr></tr>
                    </tfoot>
                </table>
            </div>




            <h3 class="text-dark mb-4"> <button class="btn btn-info " data-toggle="modal" data- target="#login_itech3">adding column</button></h3>
        </div>
    </div>

    <br>
<?php } ?>

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