生成多个表(供查看)

时间:2018-04-21 01:42:00

标签: php mysql html-table

我想询问如何使我的表格显示如下:

How it supposed to look like

但我只是让我的桌子看起来像这样:

Current appearance of the table

如您所见,该表根据存储其内容的不同年份或学期分开。它基本上是该特定人的历史存储。不幸的是,我不知道如何将生成的表连接在一起,用于同一年和一学期的表,而不是分开它。即使编号受到影响。下面是我到目前为止的代码:

<?php  
include("connect.php");
include("header.php");

if(isset($_GET['invstuview_bag'], $_GET['invstuview_name']))
{
    $get_id = $_GET['invstuview_bag'];  
    $get_name = $_GET['invstuview_name']; 

?>

<br/>
<ul class="breadcrumb">
    <li class="breadcrumb-item"><a href="view.php">View History</a></li>
    <li class="breadcrumb-item-active">Baggage Detail History</a></li>
</ul>
<div id="cssword">Baggage Detail History For <?php echo $get_name; ?>(<?php echo $get_id; ?>)</div>
<div class="container" style="width:70%;"> 
    <div class="table-responsive">
    <?php
        $sql_join = "SELECT student.*,location.*, inventory.*, baggage.*
                FROM 
                    student,location, inventory, baggage
                WHERE
                    student.student_id = inventory.invstu_id AND
                    baggage.baggage_id = inventory.invbag_id AND
                    location.location_id = inventory.invloc_id AND
                    invstu_id = '$get_id'
                ORDER BY inventory_id";

        $result_join= mysqli_query($connect,$sql_join); 

        $prev_year = "";
        $prev_sem = 0;
        $get_year = "";
        $get_sem = 0;
        while($row_join=mysqli_fetch_assoc($result_join))
        {
            $counter = 1;
            $prev_year =  $row_join["invstu_year"];
            $prev_sem =  $row_join["invstu_sem"];

            //if the data is of same year and semester
            if(($prev_year!="") && ($prev_sem!=0) && ($prev_year == $get_year) && ($prev_sem == $get_sem))
            {
                $get_year = $row_join["invstu_year"];
                $get_sem = $row_join["invstu_sem"];
    ?>
                <table class="table table-bordered">
                <tr>
                    <th>No.</th>
                    <th>Baggage Types</th>
                    <th>Quantity</th>
                    <th>Location</th>
                </tr>
                <tr>
                    <td><?php echo $counter; ?></td>
                    <td><?php echo $row_join["baggage_type"]; ?></td>
                    <td><?php echo $row_join["invbag_quantity"]; ?></td>
                    <td><?php echo $row_join["location_house"]; ?></td>
                    </tr>

                <?php
                $counter++; 
                echo'</table>';

            }
            //if data is not of same year or semester
            else
            {
                $get_year = $row_join["invstu_year"];
                $get_sem = $row_join["invstu_sem"];
            ?>
                </br></br>
                Room: <?php echo $row_join["invstu_room"]; ?><br/>
                Year: <?php echo $row_join["invstu_year"]; ?><br/>
                Semester: <?php echo $row_join["invstu_sem"]; ?><br/>

                <table class="table table-bordered">
                <tr>
                    <th>No.</th>
                    <th>Baggage Types</th>
                    <th>Quantity</th>
                    <th>Location</th>
                </tr>
                <tr>
                    <td><?php echo $counter; ?></td>
                    <td><?php echo $row_join["baggage_type"]; ?></td>
                    <td><?php echo $row_join["invbag_quantity"]; ?></td>
                    <td><?php echo $row_join["location_house"]; ?></td>
                    </tr>
                <?php
                    $counter++; 
                    echo'</table>';
            }
        }
            ?>
    </div>
    <div align="right">
            <button type="button" name="back" class="btn btn-success" id="back" style="width:200px; display:inline-block; margin-top:2%; margin-bottom:1%; background-color: #4CAF50" onclick="window.location.href='view.php'">Back</button>
    </div>

</div>
<?php
}
?>

非常感谢任何想法。

1 个答案:

答案 0 :(得分:0)

将这些移出while循环:

           <table class="table table-bordered">
            <tr>
                <th>No.</th>
                <th>Baggage Types</th>
                <th>Quantity</th>
                <th>Location</th>
            </tr>

           echo'</table>';