迭代多个表的JOIN中的行的值PHP / MYSQLi

时间:2015-07-31 04:10:30

标签: php loops mysqli iteration

我有一个由多个表组成的JOIN。

$sql_entries = "SELECT transaction_information . *, customer_information . * , property_information . *, borrowers . *,  lenders . *, listing_agents . *, payoff . *,  sellers . *, selling_agents .*
                        FROM transaction_information
                        JOIN customer_information ON transaction_information.entry_no = customer_information.entry_no
                        JOIN property_information ON transaction_information.entry_no = property_information.entry_no
                        JOIN borrowers ON transaction_information.entry_no = borrowers.entry_no
                        JOIN lenders ON transaction_information.entry_no = lenders.entry_no
                        JOIN listing_agents ON transaction_information.entry_no = listing_agents.entry_no
                        JOIN payoff ON transaction_information.entry_no = payoff.entry_no
                        JOIN sellers ON transaction_information.entry_no = sellers.entry_no
                        JOIN selling_agents ON transaction_information.entry_no = selling_agents.entry_no
                       ";

它返回大约50+列。我想在列的顶部和下方显示列名称。

我正在尝试使用以下代码,但它没有给我所需的结果。

      $result_entries = $conn->query($sql_entries);



                 if ($result_entries->num_rows > 0) {
                    echo '<div id="total"><table class="table table-striped table-bordered table-responsive">';

                    echo "<tr>";
                            //$entries = array();
                        while($row = $result_entries->fetch_assoc()) {



                             foreach ($row as $key => $value) {



                                 echo '<th>'.$key.'</th>';
                            }

                        echo '</tr>';

                        }

                     //ROw of Table heading ends.
      // Fetch values in the columns under the respective heads. 

                        while($row1 = $result_entries->fetch_assoc()) {
                            echo '<tr>';

                             foreach ($row1 as $col) {

                              echo '<td>'.$col.'</td>'; 
//This wil return Object and I know. But I don't want to use   $row['indexName'] and repeat myself for fetching values as there are too many columns.

                            }
                          echo '</tr>';
                        }



                    echo "</table></div>";
                    } else {
                        echo "0 results for Entries";
                    }

好的,我的第一个问题是迭代每一行值而不执行$ row [&#39; indexname&#39;]之类的操作 其次,我的列标题在同一行中重复了两次。

1 个答案:

答案 0 :(得分:0)

使用array_keys(),您可以获取列名称。然后使用implode(),您可以使用<th></th>构建标题行。要仅回显第一个循环上的标题行,可以使用布尔变量,即。 $header = true;。然后在回显标题行后将其设置为false,它将不再输出。您的代码看起来像 -

$result_entries = $conn->query($sql_entries);

if ($result_entries->num_rows > 0) {
    echo '<div id="total"><table class="table table-striped table-bordered table-responsive">';

    $headers = true;

    while($row = $result_entries->fetch_assoc()) {

        // echo headers on 1st iteration
        if($headers){
            echo "<tr><th>".implode("</th><th>",array_keys($row))."</th><tr>";
            $headers = false;
        }

        // echo row values
        echo "<tr>";
        foreach ($row as $key => $value) {
            echo "<td>".$value."</td>"; 
        }
        echo "</tr>";
    }
    echo "</table></div>";
} else {
    echo "0 results for Entries";
}