Reputation: 13
I am trying to extract data from a table and display it in a readable format. The table has 15 columns. I can do it horizontally, but it is hard to read that way. I have figured out how to get the data to come out vertical but I can not figure out how to label each row. It would even work if I could print the column name with the data. Below is my query:
<?php
$con=mysqli_connect("localhost","username","password","database");
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM DailyNumber WHERE location='Discount25'");
$post = array();
while($row = mysqli_fetch_assoc($result))
{
$post[] = $row;
}
foreach ($post as $row)
{
foreach ($row as $element)
{
echo $element."<br>";
}
}
?>
Output
Discount25
2014-03-03
1
2
4
5
6
7
8
9
1
2
3
4
It would output like something like this:
Location: Discount25
DAte: 2014-03-03
Sales: 1
Gross Profit: 2
Expenses: 3
Cat 1: 4
Cat 2: 5
etc
Upvotes: 1
Views: 38
Reputation: 24692
$row as $key => $value
and then place it into a neat little table.
echo '<table>';
foreach ($post as $row)
{
foreach ($row as $key => $value)
{
echo ' <tr><th> ' . $key . ' </th><td> ' . $value . ' </td></tr> ';
}
}
echo '</table>';
Upvotes: 0
Reputation: 3642
You should use $key => $value
:
foreach ($post as $row)
{
foreach ($row as $key => $value)
{
echo $key . ' - '. $value . '<br />';
}
}
Upvotes: 1
Reputation: 23500
I think you just need to extract keys as well in your iteration and print them out
foreach ($post as $row)
{
foreach ($row as $key => $element)
{
echo $key . ': '. $element."<br>";
}
}
Upvotes: 0