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

I need to display row from different table with random id that what i make it php generate it when i add new car or new driver

For example :

In this image table record's for same driver and different Car

reports image and in this image the records for random id for the car

cars image

I need to show every records by date for same driver and Which car he use

Note: In this case its show like this

wrong view

and I need it show like this

right view

and sorry for arabic database records tell me if there anything not understood

my code:

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

</div>

I don't have your database schema, but I tried to create one for testing. So, try this:

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

r1 is a shortcut for reports table ( change it to whatever you like ).

c1 is a shortcut for cars table ( also, you can change it ).

You can read more about INNER JOIN.