【问题标题】:Display data from database inside <table> using wordpress $wpdb使用 wordpress $wpdb 在 <table> 中显示数据库中的数据
【发布时间】:2013-09-20 09:25:59
【问题描述】:

有人可以帮我吗,如何编写将数据库中的数据打印到&lt;table&gt; 的逻辑?

<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>

我知道这是非常基本的,但我真的很难做到这一点。

【问题讨论】:

  • 你在echo '&lt;td&gt;' $print-&gt;firstname.'&lt;/td&gt;';中缺少.
  • 另外,在将文本附加到 HTML 时,您应该始终使用 htmlspecialchars()
  • 嗨@rid你能解释一下htmlspecialhchars()做什么吗? PHP 手册技术性太强,无法理解。谢谢。
  • @user1933824,基本上它将&lt;&gt;等字符替换为显示这些字符所需的HTML。例如,如果名字是“”,那么没有htmlspecialchars() 的HTML 将是&amp;lt;bill&amp;gt;,这是一个标签,不会显示。 htmlspecialchars() 将其替换为 &amp;lt;bill&amp;gt;
  • 你在wordpress的哪里执行那个代码???

标签: php wordpress html-table


【解决方案1】:

试试这个:

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

【讨论】:

  • @DS9 我在哪里可以放置这个代码,你能告诉我吗? - 提前感谢
  • @Alejo_Blue 结束 function.php 文件中的行..!
【解决方案2】:

您只需将&lt;tr&gt; 放入您的foreach 循环中,并在您的行中添加. 连接运算符,您还需要试试这个:

  • 您需要在 foreach 循环中将 &lt;td&gt;&lt;/td&gt; 包裹在 &lt;tr&gt;&lt;/tr&gt;
  • 您需要在包含名字变量的行中添加. 连接运算符。
  • 如果您有重复的值,则将此参数 ARRAY_A 添加到您的查询中

    $result = $wpdb-&gt;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>
    

【讨论】:

    【解决方案3】:

    试试这个:

    $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>';
    }
    

    【讨论】:

      【解决方案4】:

      您在echo '&lt;td&gt;' $print-&gt;firstname.'&lt;/td&gt;'; 中缺少.

      试试这个

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-06-27
        • 2020-10-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-06-28
        • 1970-01-01
        相关资源
        最近更新 更多