PHP从MySql以水平样式显示数据

时间:2017-11-01 06:58:14

标签: php mysql wordpress

我试图在我的WordPress网站的成员页面中显示我的所有网站成员信息与头像和一些信息。为此,我编写了以下代码。它正在连续显示一个用户的数据。我是什么尝试是在一行中水平显示多个用户,例如4到5个成员,然后开始第二行,类似于此https://stackoverflow.com/users 以下是我的code.how可以实现这个

 <?php

    global $wpdb;
    $result = $wpdb->get_results( "SELECT id,display_name as pt,user_registered as re FROM wp_users  group by id"); /*mulitple row results can be pulled from the database with get_results function and outputs an object which is stored in $result */

    foreach($result as $row)
    {
     echo '<table><tr>';      
     echo '<td>'.get_avatar( $row->id,40 );
     echo '</td><td>'.$row->id."  ".$row->pt. "<br>" .$row->re. "</td></tr> 
    </table>";

    }

 ?> 

2 个答案:

答案 0 :(得分:1)

我更喜欢使用计数来解决问题并定义固定数量的列。

<?php

    global $wpdb;
    $result = $wpdb->get_results( "SELECT id,display_name as pt,user_registered as re FROM wp_users  group by id"); /*mulitple row results can be pulled from the database with get_results function and outputs an object which is stored in $result */

    $count = count($result);
    $columns = 5;

    echo '<table><tr>';

    foreach($result as $i => $row) {
        echo '<td>' . get_avatar( $row->id,40 ) . '</td>';
        echo '<td>' . $row->id . '  ' . $row->pt . '<br>' . $row->re . '</td>';

        $i++;
        if($i != $count && $i >= $columns && $i % $columns == 0)
            echo '</tr><tr>';
    }

    echo '</tr></table>';

?> 

答案 1 :(得分:0)

试试这个,

<?php
global $wpdb;
$result = $wpdb->get_results( "SELECT id,display_name as pt,user_registered as re FROM wp_users  group by id");

&GT;

<div class="container">
<?php foreach($result as $row) { ?>
<div class="row">
  <div class="col-lg-3">
     <?php get_avatar( $row->id,40 ); ?><br>
     <?php echo $row->id." ".$row->pt;?><br>
     <?php echo $row->re;?>
  </div>
</div>
<?php } ?>
</div>