需要标记数据输出

时间:2014-03-07 00:05:01

标签: html mysqli

我正在尝试从表中提取数据并以可读格式显示它。该表有15列。我可以横向进行,但很难以这种方式阅读。我已经弄清楚如何让数据垂直出来,但我无法弄清楚如何标记每一行。如果我可以用数据打印列名,它甚至可以工作。以下是我的询问:

<?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>";
            }        
        }     
?>  

输出

     Discount25
     2014-03-03 
     1
     2 
     4
     5
     6 
     7 
     8 
     9 
     1 
     2 
     3 
     4

它会像这样输出:

     Location:      Discount25
     DAte:          2014-03-03
     Sales:         1
     Gross Profit:  2
     Expenses:      3
     Cat 1:         4
     Cat 2:         5
    etc

3 个答案:

答案 0 :(得分:1)

您应该使用$key => $value

foreach ($post as $row) 
{ 
    foreach ($row as $key => $value)
    {
        echo $key . ' - '. $value . '<br />';
    }        
}

答案 1 :(得分:0)

我认为您只需要在迭代中提取密钥并将其打印出来

 foreach ($post as $row) 
 { 
    foreach ($row as $key => $element)
    {
        echo $key . ': '. $element."<br>";
    }        
}

答案 2 :(得分:0)

$row as $key => $value然后把它放在一张整洁的小桌子里。

echo '<table>';

foreach ($post as $row) 
{ 
    foreach ($row as $key => $value)
    {
        echo ' <tr><th> ' . $key . ' </th><td> ' . $value . ' </td></tr> ';
    }        
}

echo '</table>';