I have a JOIN made from multiple tables.
$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
";
It returns around 50+ Columns. I want to display the column names on top and below it the values.
I am trying to use the following code but it's not giving me the desired result.
$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";
}
Okay, my first question will be to iterate over each row value without doing something like $row['indexname'] Second, that my column headings are being repeated twice as well in the same row.
Using array_keys()
you can get your column names. Then using implode()
you can build your header row, with <th></th>
. To only echo the header row on the first loop, you can use a boolean var, ie. $header = true;
. Then after echoing the header row set it to false and it won't output again. Your code would look something like -
$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";
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.