【问题标题】:Iterating through Values of Row from a JOIN of multiple tables PHP/MYSQLi从多个表的 JOIN 中迭代行的值 PHP/MYSQLi
【发布时间】:2015-10-22 15:32:18
【问题描述】:

我有一个由多个表组成的 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['indexname'] 其次,我的列标题在同一行中也重复了两次。

【问题讨论】:

    标签: php loops mysqli iteration


    【解决方案1】:

    使用array_keys() 可以获得列名。然后使用implode(),您可以使用&lt;th&gt;&lt;/th&gt; 构建标题行。要仅在第一个循环中回显标题行,您可以使用布尔变量,即。 $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";
    }
    

    【讨论】:

    • 谢谢肖恩。它工作得很好,但我的加入工作不正常。它为借用者部分的 2 个条目返回多行。这是主表单208.109.186.112/kushalmn/demo1 的链接。你能看看吗!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-09-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多