简体   繁体   English

使用JOIN在表格中显示数据

[英]Using JOIN to display data in a table

Thanks for reading my question 感谢您阅读我的问题

i am trying to make *clients_id* from the table repair_jobs appear as the name from the table contacts 我试图使表repair_jobs中的 * clients_id *作为表联系人名称出现

but i am having no luck i have got 2 sql query's is this wrong? 但我没有运气我有2个SQL查询,这是错误的吗?

the 1st 第一

$query = "select * from repair_jobs";

this helps me display the information i need regarding the fields from repair_jobs and works 这可以帮助我显示我需要的关于来自repair_jobs和works字段的信息

this is the 2nd 这是第二

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

under that i have this to try to display the name of the client 在此之下,我有这个尝试显示客户端的名称

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

but it is only displaying the number and not the data (clients name) that i need 但它只显示数字,而不显示我需要的数据(客户名称)

am i missing something? 我想念什么吗?


Additional information 附加信息

The client_id (repair_jobs) is a number and is the same as id (contacts) but wanting to display the name (contacts) client_id(repair_jobs)是一个数字,与id(联系人)相同,但希望显示姓名(联系人)

CLIENTS 客户

Id – name – surname – phone – address

REPAIRS 维修

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

current code 当前代码

<?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();

?>

Change your 1st query to you join query, as there is no reason to do a 2nd query in the middle of your code. 将您的第一个查询更改为联接查询,因为没有理由在代码中间进行第二个查询。 (also you never executed that query anyway). (同样,您也从未执行过该查询)。

//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";

Then in your table keep the $client_name 然后在表格中保留$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