[英]Display data from database inside <table> using wordpress $wpdb
Can someone help me, how to code the logic to print the data from my database into a <table>
? 有人可以帮助我,如何编写逻辑来将数据从我的数据库打印到
<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>
i know this is so basic, but im really having a hard time to make this work. 我知道这是如此基本,但我真的很难让这项工作。
Try this: 尝试这个:
<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 }
?>
You just need to put <tr>
inside your foreach loop ,and add .
你只需要将
<tr>
放在foreach循环中,然后添加.
concatenation operator in your line ,you nee alsotry this : 在你的行中连接运算符,你需要这样:
<td></td>
inside <tr></tr>
in foreach loop <td></td>
包装在<tr></tr>
中 .
.
concatenation operator in line that contains firstname variable. If you have duplicate values , then add this parameter ARRAY_A
to your query 如果您有重复值,请将此参数
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>
You're missing .
你错过了
.
in echo '<td>' $print->firstname.'</td>';
在
echo '<td>' $print->firstname.'</td>';
Try this 尝试这个
<?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>';
}
?>
Try this: 尝试这个:
$result = $wpdb->get_results("SELECT * FROM myTable" , ARRAY_A); //get result as associative array
Then the usual cycle: 那么通常的周期:
//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.