簡體   English   中英

使用JOIN在表格中顯示數據

[英]Using JOIN to display data in a table

感謝您閱讀我的問題

我試圖使表repair_jobs中的 * clients_id *作為表聯系人名稱出現

但我沒有運氣我有2個SQL查詢,這是錯誤的嗎?

第一

$query = "select * from repair_jobs";

這可以幫助我顯示我需要的關於來自repair_jobs和works字段的信息

這是第二

$query = "SELECT repair_jobs.client_id, contacts.name
FROM repair_jobs
INNER JOIN contacts
ON repair_jobs.client_id=contacts.name";

在此之下,我有這個嘗試顯示客戶端的名稱

echo "<td>{$client_id}</td>";

但它只顯示數字,而不顯示我需要的數據(客戶名稱)

我想念什么嗎?


附加信息

client_id(repair_jobs)是一個數字,與id(聯系人)相同,但希望顯示姓名(聯系人)

客戶

Id – name – surname – phone – address

維修

Id – clients_id (same as id in clients) – unit – date – price

當前代碼

<?php
//include database connection
include 'db_connect.php';

//query all records from the database
$query = "select * from repair_jobs";

//execute the query
$result = $mysqli->query( $query );

//get number of rows returned
$num_results = $result->num_rows;

//this will link us to our add.php to create new record
if( $num_results > 0){ //it means there's already a database record

    //start table
    //creating our table heading
    echo " <table class='table_basic'>";
    echo "<thead><tr>";
        echo "<th>Job #</th>";
        echo "<th>Name Of Unit</th>";
        echo "<th>Client</th>";
        echo "<th>Estimated Value</th>";
    echo "</thead></tr><tbody><tr>";

    //loop to show each records
    while( $row = $result->fetch_assoc() ){
            //extract row
            //this will make $row['firstname'] to
            //just $firstname only
            extract($row);

            //creating new table row per record
            echo "<tr>";
                echo "<td width='40px'><a href='rdetails.php?id={$id}'># {$id}</a></td>";
                echo "<td>{$rmake} {$rmodel}</td>";

$query = "SELECT rj.client_id, c.name AS client_name FROM repair_jobs rj INNER JOIN contacts c ON rj.client_id=c.id";
echo "<td>{$client_name}</td>";

echo '<td align="center"><span class="badge badge-success">£';
$lhours = $labour;
$repaircosts = $ourcosts;
$labourpay = $labourcharge;
$sum_total = $repaircosts +($lhours * $labourpay);




print ($sum_total);
echo '</span></td>';
echo "</td>";
echo "";
    }

    echo "</tr></table>";//end table

}else{
    //if database table is empty
    echo "No records found.";
}

//disconnect from database
$result->free();
$mysqli->close();

?>

將您的第一個查詢更改為聯接查詢,因為沒有理由在代碼中間進行第二個查詢。 (同樣,您也從未執行過該查詢)。

//query all records from the database
$query = "SELECT repair_jobs.*, contacts.name as client_name
FROM repair_jobs
INNER JOIN contacts
ON repair_jobs.client_id=contacts.id";

然后在表格中保留$client_name

echo "<td>{$client_name}</td>";
<?php
include 'db_connect.php';
$query = "SELECT rj.Id AS job_number, rj.unit, rj.make, rj.model, c.name AS client_name, rj.price FROM repair_jobs rj INNER JOIN contacts c ON rj.clients_id = c.id ORDER BY c.date";
$result = $mysqli->query( $query );
$num_results = $result->num_rows;
if( $num_results > 0){ //it means there's already a database record
    echo " <table class='table_basic'>";
    echo "<thead><tr>";
    echo "<th>Job #</th>";
    echo "<th>Name Of Unit</th>";
    echo "<th>Client</th>";
    echo "<th>Estimated Value</th>";
    echo "</tr></thead><tbody>";
    while( $row = $result->fetch_assoc() ){
        extract($row);
        echo "<tr>";
        echo "<td width='40px'><a href='rdetails.php?id={$job_number}'>#{$job_number}</a></td>";
        echo "<td>{$make} {$model}</td>";
        echo "<td>{$client_name}</td>";
        echo "<td align='center'><span class='badge badge-success'>£";
        $lhours = $labour;
        $repaircosts = $ourcosts;
        $labourpay = $labourcharge;
        $sum_total = $repaircosts +($lhours * $labourpay);
        echo $sum_total;
        echo '</span></td>';
        echo "</td>";
        echo "</tr>";
    }
    echo "</tbody></table>";
} else {
    echo "No records found.";
}
$result->free();
$mysqli->close();
?>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM