通过用户选择的随机ID显示无限行(由php创建)

时间:2019-02-23 05:09:30

标签: php mysqli

我需要显示具有不同ID的不同表中的行,当我添加新车或新驾驶员时,我将其生成为php

例如

在此图像表中,记录了相同驾驶员和不同汽车的情况

reports image 并在这张图片中记录了汽车的随机ID

cars image

我需要按日期显示同一位驾驶员及其使用的汽车的所有记录

注意:在这种情况下,其显示如下

wrong view

我需要这样显示

right view

很抱歉,arabic database条记录告诉我是否有不理解的地方

我的代码

<?php
require 'config.php';
$query = "SELECT * FROM `driversuser`";
$result1 = mysqli_query($con, $query);

?>
<div class="container">
    <div class="addingcar">
        <form dir="rtl" action="#" method="post">

            <div class="">
                <b>عرض وردية سائق</b>
            </div>

            <br/>
            <label>
                <b>اختر السائق</b>
                <select name="insdname" id="framework" class="align-right selectpicker" data-live-search="true">
                    <option style="text-align: right" value="">اختر السائق ...</option>
                    <?php while($row1 = mysqli_fetch_array($result1)):; ?>
                        <option style="text-align: right" value="<?php echo $row1[10]; ?>"><?php echo $row1[1]; ?></option>
                    <?php endwhile; ?>
                </select>
            </label>
            <br />
            <label>
                <b>من</b>
                <input type="date" name="datefrom">
            </label>
            <br />
            <label>
                <b>الي</b>
                <input type="date" name="dateto">
            </label>
            <br /><br />
            <input type="hidden" name="hidden_framework" id="hidden_framework" />
            <input type="submit" name="submit" class="btn btn-info" value="عــــرض" />

        </form>
        <?php
        if(isset($_POST['submit'])){
            $selected_val = $_POST['insdname'];
            $date_val = $_POST['datefrom'];
            $dateto_val = $_POST['dateto'];
            $report = mysqli_query($con, "SELECT * FROM `reports` WHERE Datefrom BETWEEN '$date_val' AND '$dateto_val' AND DriverCode = '$selected_val' ORDER BY Datefrom");
            while ($datareport = mysqli_fetch_array($report)) {
                echo $datareport['Datefrom']." ".$datareport['DateTo']." ".$datareport['Price']." ".$datareport['PriceTaken']." ";
                


            $report2 = mysqli_query($con, "SELECT * FROM `reports` WHERE DriverCode = '$selected_val' GROUP BY CarCode");
            while ($datareport2 = mysqli_fetch_array($report2)) {
                $carcode = $datareport2['CarCode'];

            }
            $report3 = mysqli_query($con, "SELECT * FROM `cars` WHERE Car_ID = '$carcode'");
            while ($datareport3 = mysqli_fetch_array($report3)) {
                echo $datareport3['CarName']." ".$datareport3['CarModel']." ".$datareport3['CarNumber']." ".$datareport3['CarColor']." ";
                echo '<br/>';
            }};
        }
        ?>

    </div>
</div>

1 个答案:

答案 0 :(得分:0)

我没有您的数据库架构,但是我尝试创建一个用于测试的架构。因此,请尝试以下操作:

SELECT * FROM reports r1 INNER JOIN cars c1 ON r1.carcode = c1.car_id ORDER BY r1.date

r1是报告表的快捷方式(将其更改为您喜欢的任何内容)。

c1是cars表的快捷方式(也可以更改)。

您可以了解有关INNER JOIN的更多信息。