doukanzhuo4297 2016-08-24 08:58
浏览 34
已采纳

使用PHP在列中显示数据

I'm trying to display some data in columns in php with wordpress. I have the next snippet to do that:

 $args = array(
     'taxonomy'     => $taxonomy,
     'orderby'      => $orderby,
     'show_count'   => $show_count,
     'pad_counts'   => $pad_counts,
     'hierarchical' => $hierarchical,
     'title_li'     => $title,
     'parent'       =>19,
     'hide_empty'   => $empty
);
$columnas=0;
$all_categories = get_categories( $args );
echo '<table><tr>';
foreach ($all_categories as $cat) {
  $category_id = $cat->term_id;       
   if((($columnas%4)!==0)and($columnas!==0)){
     echo '<td><a href="'. get_term_link($cat->slug, 'product_cat') .'">'. $cat->name .'</a></td>';
     $columnas=$columnas+1;     
     echo 'Las columnas son: '.$columnas.'<br/>';
   }else
   {
     echo 'Columnas es: '.$columnas;
     echo '<td><a href="'. get_term_link($cat->slug, 'product_cat') .'">'. $cat->name .'</a></td></tr><tr>';
   }


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

So what I'm doing wrong?, I don't get what I want which is a table with four columns and many rows. Surely, it's a silly thing but I don't see it.

  • 写回答

2条回答 默认 最新

  • dongsha1544 2016-08-24 09:38
    关注

    I assume you need to display the categories in 4 column. Is that so then this code will help you to show in 4 column table.

     $args = array(
     'taxonomy'     => $taxonomy,
     'orderby'      => $orderby,
     'show_count'   => $show_count,
     'pad_counts'   => $pad_counts,
     'hierarchical' => $hierarchical,
     'title_li'     => $title,
     'parent'       =>19,
     'hide_empty'   => $empty
     );
     $columnas=0;
     $all_categories = get_categories( $args );
    
     echo '<table><tr>';
     foreach($all_categories as $cat) {
        if(($columnas%4) == 0 && $columnas > 0)
            echo '</tr><tr>';
        else
    
        $category_id = $cat->term_id;
        echo '<td><a href="'. get_term_link($cat->slug, 'product_cat') .'">'. $cat->name .'</a></td>';
        $columnas++;
    }
    $cnt = (($columnas + 1)%4);
    if($cnt > 0) {
        echo '<td colspan="' . (($columnas-1) - $cnt) . '"></td>';
    }
    echo '</tr></table>';
    

    But my suggetion are you going to display it in 4X list gor for ul li. Easy to do the loop.

    Thanks

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100