douzhenzu0247 2017-11-01 06:58
浏览 32
已采纳

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

I am trying to display all my site members info with avatar and some information in members page of my WordPress site.For this, I have written following code.It is displaying data with one single user in a row.What I am trying is to display multiple users like 4 to 5 members horizontally in a single row then start 2nd row similar like this https://stackoverflow.com/users Following is my code.how can achieve this

 <?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条回答 默认 最新

  • dongyao9762 2017-11-01 07:08
    关注

    I prefer to solve the problem by using an count and define a fixed number of columns.

    <?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条)

报告相同问题?

悬赏问题

  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分
  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题
  • ¥15 linux驱动,linux应用,多线程