如何在弹出模式中从多个表中获取数据

问题描述 投票:0回答:0

我有一个表格,其中一个单元格可以单击并打开一个弹出模式以显示有关数据的更多信息。根据数据,jobregister_id、customer_name、machine_type、job_description、reason 和 jobregisterlastmodify_at 将从 job_register 表中获取,用户名(在表图片中为 assistant)将从 assistants 表中获取。我的问题是,如果该行没有用户名,它将不会从 job_register 表中获取其他数据。有人可以帮我修改我的代码,这样即使该行没有助手,它仍然可以显示 job_register 表中的其余数据。

table example

带弹出模式的表格代码:

<!-- Incomplete Job -->
<?php
    include 'dbconnect.php';
    
    $numRow = "SELECT * FROM job_register LEFT JOIN assistants ON job_register.jobregister_id=assistants.jobregister_id 
               WHERE job_register.job_status = 'Incomplete' AND (job_register.job_cancel = '' OR job_register.job_cancel IS NULL)
               LIMIT 50";
    
    $numRow_run = mysqli_query ($conn,$numRow);
    
    if ($row_Total = mysqli_num_rows($numRow_run)) {
        echo '<b>Incomplete Job - '.$row_Total.'</b>';
    }
    
    else {
        echo '<b>Incomplete Job - No Data</b>';
    }
?>
    
    <table class="table table-bordered" id="auto" style="box-shadow:none; border-color: black; background-color:#ffffff;">
        <thead style="box-shadow:none;">
            <tr>
                <th style="border-color: black;"></th>
                <th style="border-color: black;">Leader</th>
                <th style="border-color: black;">Assistant</th>
                <th style="border-color: black;">Place</th>
                <th style="border-color: black;">Machine</th>
                <th style="border-color: black;">Reason</th>
                <th style="border-color: black;">Last Update Date</th>
            </tr>
        </thead>
        
        <?php
            
            include 'dbconnect.php';
            
            $results = $conn->query("SELECT * FROM job_register LEFT JOIN assistants ON job_register.jobregister_id=assistants.jobregister_id 
                                     WHERE job_register.job_status = 'Incomplete' AND (job_register.job_cancel = '' OR job_register.job_cancel IS NULL)
                                     ORDER BY job_register.job_assign ASC, job_register.jobregisterlastmodify_at DESC LIMIT 50");
                    
            while($row = $results->fetch_assoc()) {
                          
            $jobregisterlastmodify_at = $row['jobregisterlastmodify_at'];
            $datemodify = substr($jobregisterlastmodify_at,0,11); 
        ?>
        
        <tbody>
            <tr>
                <td style="border-color: black;"></td>
                <td style="border-color: black;" class="clickable-cell" data-row-id="<?php echo $row['jobregister_id']?>"><?php echo $row['job_assign']?></td>
                <td style="border-color: black;"><?php echo $row['username']?></td>
                <td style="border-color: black;"><?php echo $row['customer_name']?></td>
                <td style="border-color: black;"><?php echo $row['machine_type']?> - <?php echo $row['job_description']?></td>
                <td style="border-color: black;"><?php echo $row['reason']?></td>
                <td style="border-color: black;"><?php echo $datemodify ?></td>
            </tr>
        </tbody>   
        <?php } ?>
    </table>
    
    <!-- Job Info Popup Modal -->
    <div class="modal fade" id="popup-modal">
        <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-header">
                    <h4 class="modal-title">Job Information</h4>
                    <button type="button" class="close" data-dismiss="modal">&times;</button>
                </div>
                
                <div class="modal-body">

                </div>
                
                <div class="modal-footer">
                    <button type="button" class="btn btn-danger" data-dismiss="modal">Close</button>
                </div>
            </div>
        </div>
    </div>
    
    <script>
        $(document).ready(function(){
            $(".clickable-cell").click(function(){
                var jobregister_id = $(this).data("row-id");
                $.ajax({
                    url: "AdminJobTableJobInfo.php",
                    type: "post",
                    data: {jobregister_id: jobregister_id},
                    success: function(response){
                        $("#popup-modal .modal-body").html(response);
                        $("#popup-modal").modal("show");
                    }
                });
            });
        });
    </script>
<!-- End of Incomplete Job -->

在弹出模式中获取数据的代码:

<?php
    include 'dbconnect.php';
    
    $jobregister_id = $_POST["jobregister_id"];
    
    $query = "SELECT * FROM job_register LEFT JOIN assistants 
              ON job_register.jobregister_id=assistants.jobregister_id 
              WHERE job_register.jobregister_id = '$jobregister_id' AND assistants.jobregister_id = '$jobregister_id' ";

    $result = mysqli_query($conn, $query);
    
    // Check if query was successful
    if (!$result) {
        die("Query failed: " . mysqli_error($conn));
    }
    
    $html = '';
    while ($row = mysqli_fetch_assoc($result)) {
        // Create HTML for the popup modal
        $html .= "<h4>Row ID: " . $row["jobregister_id"] . "</h4>";
        $html .= "<p>Leader: " . $row["job_assign"] . "</p>";
        
        if ($row["username"] !== NULL) {
            echo $html;
        } else {
            // Create HTML for the popup modal when $html is empty
            $html = "<p>No data available for this job register ID.</p>";
            echo $html;
        }
                
        $html .= "<p>Place: " . $row["customer_name"] . "</p>";
        $html .= "<p>Machine: " . $row["machine_type"] . " - " . $row["job_description"] . "</p>";
        $html .= "<p>Reason: " . $row["reason"] . "</p>";
        $html .= "<p>Last Update Date: " . $row["jobregisterlastmodify_at"] . "</p>";
        $html .= "<hr>";
    }
    
    // Check if $html is not empty
    if (!empty($html)) {
        echo $html;
    } else {
        // Create HTML for the popup modal when $html is empty
        $html = "<p>No data available for this job register ID.</p>";
        echo $html;
    }
?>

这是我尝试过但没有一个有效的其他查询:


SELECT * FROM job_register INNER JOIN assistants ON job_register.jobregister_id = assistants.jobregister_id WHERE job_register.jobregister_id = '$jobregister_id'


SELECT * FROM job_register LEFT OUTER JOIN assistants ON job_register.jobregister_id = assistants.jobregister_id WHERE job_register.jobregister_id = '$jobregister_id

php mysql ajax popup
© www.soinside.com 2019 - 2024. All rights reserved.