![](/img/trans.png)
[英]Display data from database inside <table> using wordpress $wpdb insdie a shortcode
[英]Display data from database inside <table> using wordpress $wpdb
有人可以幫助我,如何編寫邏輯來將數據從我的數據庫打印到<table>
?
<table border="1">
<tr>
<th>Firstname</th>
<th>Lastname</th>
<th>Points</th>
</tr>
<tr>
<?php
global $wpdb;
$result = $wpdb->get_results ( "SELECT * FROM myTable" );
foreach ( $result as $print ) {
echo '<td>' $print->firstname.'</td>';
}
?>
</tr>
</table>
我知道這是如此基本,但我真的很難讓這項工作。
嘗試這個:
<table border="1">
<tr>
<th>Firstname</th>
<th>Lastname</th>
<th>Points</th>
</tr>
<?php
global $wpdb;
$result = $wpdb->get_results ( "SELECT * FROM myTable" );
foreach ( $result as $print ) {
?>
<tr>
<td><?php echo $print->firstname;?></td>
</tr>
<?php }
?>
你只需要將<tr>
放在foreach循環中,然后添加.
在你的行中連接運算符,你需要這樣:
<td></td>
包裝在<tr></tr>
中 .
包含firstname變量的連接運算符。 如果您有重復值,請將此參數ARRAY_A
添加到您的查詢中
$result = $wpdb->get_results ( "SELECT * FROM myTable",ARRAY_A );
。
<table border="1"> <tr> <th>Firstname</th> <th>Lastname</th> <th>Points</th> </tr> <?php global $wpdb; $result = $wpdb->get_results ( "SELECT * FROM myTable" ); foreach ( $result as $print ) { echo '<tr>'; echo '<td>' . $print->firstname .'</td>'; echo '<td>' . $print->lastname .'</td>'; echo '<td>' . $print->points .'</td>'; echo '</tr>'; } ?> </table>
你錯過了.
在echo '<td>' $print->firstname.'</td>';
嘗試這個
<?php
global $wpdb;
$result = $wpdb->get_results ( "SELECT * FROM myTable" );
foreach ( $result as $print ) {
echo '<tr>';
echo '<td>' . $print->firstname.'</td>';
echo '<td>' . $print->lastname.'</td>';
echo '<td>' . $print->points.'</td>';
echo '</tr>';
}
?>
嘗試這個:
$result = $wpdb->get_results("SELECT * FROM myTable" , ARRAY_A); //get result as associative array
那么通常的周期:
//spare memory
$count = count($result);
//fastest way to perform the cycle
for ($i = $count; $i--;) {
echo '<td>'. $print[$i]['firstname'].'</td>';
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.