2013-03-16 136 views
2

我有一個超過50返回結果的MySQL查詢。現在我需要在3行3列的表格中顯示結果。在表中顯示查詢結果

事情是這樣的:

<table> 
    <tr> 
     <td>Content</td>      
     <td>Content</td>      
     <td>Content</td>           
    </tr> 
    <tr> 
     <td>Content</td>      
     <td>Content</td>      
     <td>Content</td>           
    </tr> 
    <tr> 
     <td>Content</td>      
     <td>Content</td>      
     <td>Content</td>           
    </tr> 
</table> 

我用PHP試着這樣說:

$q = "SELECT name, address, content FROM mytable"; 
$r = mysqli_query($dbc, $q); 

while ($row = mysqli_fetch_array($r, MYSQLI_ASSOC)) { 
    $name = $row['name']; 
    $address = $row['address']; 
    $content = $row['content']; 

    //Create new output 
    $output = "<p>$name</p>"; 
    $output .= "<p>$address</p>"; 
    $output .= "<p>$content</p>"; 

    //Add output to array 
    $mycontent[] = $output;  
} 

然後我打印我的表中的內容數組是這樣的:

<tr> 
    <td><?php echo $mycontent[0]; ?></td>     
    <td><?php echo $mycontent[1]; ?></td>     
    <td><?php echo $mycontent[2]; ?></td>     
</tr> 
<tr> 
    <td><?php echo $mycontent[3]; ?></td>     
    <td><?php echo $mycontent[4]; ?></td>     
    <td><?php echo $mycontent[5]; ?></td>          
</tr> 
<tr> 
    <td><?php echo $mycontent[6]; ?></td>     
    <td><?php echo $mycontent[7]; ?></td>     
    <td><?php echo $mycontent[8]; ?></td>           
</tr> 

使用此代碼,我只能顯示9個內容。我的問題是我想顯示更多的內容。我將使用pagination來顯示內容;類似0-9,10-18,19-27等

注:我可以做分頁部分。

我希望有人會給我這個正確的方向。

謝謝。

+1

請考慮接受一個答案(點擊左側刻度),如果它實際上回答了你的問題 – michi 2013-04-14 12:46:37

回答

2

嘗試類似:

echo "<table>"; 
while ($row = mysqli_fetch_array($r, MYSQLI_ASSOC)) { 
$name = $row['name']; 
$address = $row['address']; 
$content = $row['content']; 
echo "<tr><td>".$name."</td><td>".$address."</td><td>".$content."</td></tr>"; 
} 
echo "</table>"; 

這個例子將表格打印所有查詢結果。 如果你只想限制一些結果,那麼限制sql查詢。 例如:

$q = "SELECT name, address, content FROM mytable limit 50"; 

在TD得到每個內容,姓名,地址,以及爲myContent(內容:姓名,地址)的TR嘗試這個的3:

$c= mysql_query("select COUNT(name) from mytable"); 
$n=mysql_fetch_array($c); 
$maxname= $n[0]; 
$i=0; 
$tr=0; 
echo "<table>"; 
while ($tr < $maxname) 
{ 
echo "<tr>"; 
while ($row = mysqli_fetch_array($r, MYSQLI_ASSOC) and $i<$tr) { 
$name = $row['name']; 
$address = $row['address']; 
$content = $row['content']; 
echo "<td>".$name." | ".$address." | ".$content."</td>"; 
$i++; 
} 
echo "</tr>"; 
$tr= $tr+3; 
} 
echo "</table>"; 
+0

的姓名,地址和內容應該是發生在一個TD沒有在3 TD – TNK 2013-03-16 15:28:12

+0

你的意思是你的表像這個: 每個custermer在​​,和3個custermers在每個? – BenB 2013-03-16 16:18:37

+0

@TNK我編輯了我的答案,在1 TD中有姓名,地址和內容。 – BenB 2013-03-16 17:47:32

0

使用for - 循環迭代您$mycontent -array:

編輯:我作了知道$mycontent是建立不同於我首先假定:

$h = "<table>"; // we are going to build the table in $h 

$maxcount = count($mycontent); // get the number of values within the array 

for ($i = 0;$i < $maxcount;$i++) { // counting $i up from 0 to $maxcount -1 ... 

    $h.= "<tr><td>{$mycontent[$i]}</td></tr>"; // build row 

} 

$h.= "</table>"; // close the table 
echo $h; // echo it 

--->現場演示:http://3v4l.org/g1E1T

+0

我對你的代碼有點迷惑。你能解釋一下嗎? – TNK 2013-03-16 14:36:49

+0

@TNK:看我的編輯。隨時問問! – michi 2013-03-16 14:44:26

+0

爲什麼使用'for'循環,當你可以使用while來迭代結果? – Kermit 2013-03-16 14:46:38

0

您可以在一個變量存儲如果u [R計劃在函數中使用它,或者你可以打印出來。無論哪種方式...

$q = "SELECT name, address, content FROM mytable"; 
$r = mysqli_query($dbc, $q); 

$str = "<table>"; 

while ($row = mysqli_fetch_array($r, MYSQLI_ASSOC)) { 
    $str .= "<tr>"; 
    $str .= "<td>" . $row['name'] . "</td>"; 
    $str .= "<td>" . $row['address'] . "</td>"; 
    $str .= "<td>" . $row['content'] . "</td>"; 
    $str .= "</tr>"; 
} 

$str .= "</table>" 

echo $str; 

你走了!

+0

只打印表 – TNK 2013-03-16 14:51:25

+0

只需調用這段代碼即可。它會爲你創建整個桌子。 – 2013-03-16 14:54:11

+0

名稱,地址和內容應該放在一個td不在3 td – TNK 2013-03-16 15:12:02

1

嘗試這樣的事情。它將您的值放置在易於使用的關聯數組中。然後,你只需循環。

<?php 
while ($row = mysqli_fetch_array($r, MYSQLI_ASSOC)) { 
    $values[] = array(
     'name' => $row['name'], 
     'address' => $row['address'], 
     'content' => $row['content'] 
    ); 
} 
?> 
<table> 
<?php 
foreach($values as $v){ 
    echo ' 
    <tr> 
     <td>'.$v['name'].'</td> 
     <td>'.$v['address'].'</td> 
     <td>'.$v['content'].'</td> 
    </tr> 
    '; 
} 
?> 
</table>