【问题标题】:Generate multiple table (For viewing)生成多个表(供查看)
【发布时间】:2018-04-21 15:56:48
【问题描述】:

我想问一下如何让我的表格显示如下:

但我只能设法让我的表格看起来像这样:

如您所见,表格是根据他们存放东西的不同年份或学期分开的。它基本上是那个特定人的历史存储。不幸的是,我不知道如何将生成的表格附加到同一年和同一学期的表格中,而不是将其分开。甚至编号也会受到影响。以下是我到目前为止的代码:

<?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
}
?>

非常感谢任何想法。

【问题讨论】:

标签: php mysql html-table


【解决方案1】:

将这些移出while循环:

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

           echo'</table>';

【讨论】:

  • 您好,感谢您的回复。但是,如果我将您显示的语句移出 while 循环,则意味着表头只会生成一次。我想要的是根据年份和学期显示表格标题。这意味着它会出现不止一次。对于我发布的案例,它将是两次。 :)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-08-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-03-17
  • 1970-01-01
相关资源
最近更新 更多