2017-04-24 23 views
0

第1步:这是返回结果集的功能如何显示在PHP结果数据表

public function getLogById($reg_id) { 

     $stmt = $this->conn->prepare("SELECT * FROM tracking_log WHERE registration_id = ? LIMIT 5"); 
     $stmt->bind_param("s", $reg_id); 

     if ($stmt->execute()) { 

      $result = $stmt->get_result()->fetch_all(); 
      $stmt->close(); 
      return $result; 
     } 
     else { 

      return false; 
     } 


    } 

步骤2:在这里,我调用该函数

if(isset($_SESSION['registration_id'])){ 

    $id = $_SESSION['registration_id']; 

    $result = $logs->getLogById($id); 

    echo "<table>"; 

    foreach($tracking_result as $result) 
         { 
          foreach($result as $key => $value) 

          { 
           //echo "<li>$key : $value</li>"; 
           echo "<tr class=\"active\" role=\"row\">"; 
           echo "<td>Here I want to Display colum-1 of my DB-Table</td>"; 
           echo "<td>"Here I want to Display colum-3 of my DB-Table"</td>"; 
           echo "<td>Here I want to Display colum-4 of my DB-Table</td>"; 
           echo "</tr>"; 

          } 
         } 
    echo "<\table>"; 
} 

当我print_r($result);它显示我这样的结果

1 

35124 

2017-04-12 00:00:00 

30.102261 

-81.711777 

2 

35124 

2017-04-10 00:00:00 

30.102261 

-81.711457 

3 

35124 

2017-04-11 00:00:00 

30.063936 

-81.711307 

4 

35124 

2017-04-12 00:00:00 

30.102451 

-81.711957 

5 

35124 

2017-08-12 00:00:00 

30.102261 

-81.795777 

请帮我解决这个问题。

+0

解决什么问题?你期望输出是什么? –

+0

你在哪里声明实际的表标签? – clearshot66

+0

我想把表中的所有(5)记录从表格中放入五行html表格中。而已。问题是将结果集格式化为html表格,这正是我想要的。顺便说一句,我是新手。帮助将不胜感激。感谢 – s4starb4boy

回答

0

你只需要一个foreach循环。你必须遍历你的db $result

根据您的抓取风格,您可以在foreach循环中使用$row['some_column_name']$row[0]来访问您的列。

foreach($result as $row) { 

    echo "<tr class=\"active\" role=\"row\">"; 
    echo "<td>".$row[0]."</td>"; 
    echo "<td>".$row[1]."</td>"; 
    echo "<td>".$row[2]."</td>"; 
    echo "<td>".$row[3]."</td>"; 
    echo "</tr"; 

} 
+0

他的循环结果'$ tracking_result'会使它非常糟糕,因为我们看不到它是什么......也许'undefined'。 –

+0

非常感谢@Yolo这就是我在寻找hwo来显示表中的数据... foreach($ result as $ row){ echo“”; echo“​​”。$ row [0]。“”; echo“​​”。$ row [1]。“”; echo“​​”。$ row [2]。“”; echo“​​”。$ row [3]。“”; echo“ s4starb4boy

0

我可以在这里看到一些沉淀。我不明白你为什么要这样做的第一件事情就是你的回路$tracking_result。第二件事是错误的字符串"<td>"Here I want to Display colum-3 of my DB-Table"</td>"。无论如何,你需要做的第一件事就是在没有任何PHP交互的情况下创建一个空的HTML示例,只是为了看看你想要实现什么。例如:

<table> 
    <tr> 
     <th>Header 1</th> 
     <th>Header 2</th> 
     <th>Header 3</th> 
    <tr/> 
    <tr> 
     <td>Row 1 Value 1</td> 
     <td>Row 1 Value 2</td> 
     <td>Row 1 Value 3</td> 
    <tr/> 
    <tr> 
     <td>Row 2 Value 1</td> 
     <td>Row 2 Value 2</td> 
     <td>Row 2 Value 3</td> 
    <tr/> 
    <tr> 
     <td>Row 3 Value 1</td> 
     <td>Row 3 Value 2</td> 
     <td>Row 3 Value 3</td> 
    <tr/> 
</table> 

这是一个简单的HTML表格。如果你想与已知数据和已知头填充它,你只需要专注于自己的行这样的:

<table> 
    <tr> 
     <th>Header 1</th> 
     <th>Header 2</th> 
     <th>Header 3</th> 
    <tr/> 
    <?php foreach($results as $row) { ?> 
    <tr> 
     <td><?php echo $row['value1']; ?></td> 
     <td><?php echo $row['value2']; ?></</td> 
     <td><?php echo $row['value3']; ?></</td> 
    <tr/> 
    <?php } ?> 
</table> 

但是,如果你想与未知数据或标题来填充它,然后我们将开始更多的东西复杂。遵守此规则,您可以实现:

变量$ result必须是一个字符串索引数组。

这意味着我们可以将我们的Keys键作为我们可读的标题。有一个有用的PHP函数可以帮助我们:array_keys。有了这个,我们可以将这些标题作为一个新的数组。很酷,对吧?但它只适用于在$results中至少有一行。如果它只是一个空阵列,我们什么也得不到。所以我们需要在开始之前检查它。

<?php 
    $isEmpty = empty($results); 
    if($isEmpty) { 
     echo "I'm sorry, we got nothing. :("; 
    } else { 
     $headers = array_keys($results[0]); // I'm assuming that all of them have the same indexes. 
?> 
<table> 
    <tr> 
     <?php foreach($headers as $header) { ?> 
     <th><?php echo $header; ?></th> 
     <?php } ?> 
    <tr/> 
    <?php foreach($results as $row) { ?> 
    <tr> 
     <?php foreach($headers as $header) { ?> 
     <td><?php echo $row[$header]; ?></td> 
     <?php } ?> 
    <tr/> 
    <?php } ?> 
</table> 
<?php 
    } 
?> 

我真的不推荐使用一般的东西,但就是这样。

+0

谢谢@Kiko Garcia ...你也帮助我用不同的逻辑我得到/学习了一种新的技术。谢谢 – s4starb4boy

+0

不客气的@ s4starb4boy良好的学习。 –