0

有没有办法修改这个 PHP 代码

    foreach ($categories as $category)    
    {
        print("<tr>");               
        print("<td><a href=\"category.php?category=" . $category["id"] . "\">{$category["category"]}</a></td>");            
        print("</tr>");    
    }

将内容显示在三个较短的列中,而不是只显示一个非常长的列?喜欢这里http://www.registrar.fas.harvard.edu/courses-exams/courses-instruction

显示顺序需要是这样的:

1 4 7
2 5 8 
3 6 9
4

4 回答 4

1

您将希望在每个行标签中使用 3 个单元格标签:

$cols = 3;
$col = 0;
foreach ($categories as $category)    
{
    if($col == 0)
        print("<tr>");            
    if($col < $cols) {
        print("<td><a href=\"category.php?category=" . $category["id"] . "\">{$category["category"]}</a></td>"); 
        $col++;
    } else {
        print("</tr>");  
        $col = 0;
    }  
}
if($col != 0)
    print("</tr>");
于 2012-12-08T20:31:01.757 回答
0

假设 $categories 是一个数组。

$columns = 3;
$rows = ceil( sizeof( $categories ) / $columns );
echo "<table>\n";    
for ( $row = 1; $row <= $rows; $row++ )    
{
    echo "<tr>\n";  
    for ($column = 1; $column <= $columns; $column++) {
        $p = (($column - 1) * $rows) + $row - 1;
        $value = sizeof($categories) > $p ? $categories[$p] : '&nbsp;';
        echo "<td>{$value} {$p}</td>\n";
    }
    echo "</tr>\n";
 }
echo "</table>";

订单将

1 4 7
2 5 8 
3 6 9
于 2012-12-09T17:57:26.130 回答
0
$i=1;
foreach ($categories as $category)    
{
    if($i==1) {
      print("<tr>");  
    }             
    print("<td><a href=\"category.php?category=" . $category["id"] . "\">{$category["category"]}</a></td>");  
    if($i==3) {       
       print("</tr>"); 
       $i=1; 
    }
    $i++;  
}
于 2012-12-08T20:35:05.040 回答
0

这将列出如下:

//    0,1,2
//    3,4,5

echo '<table><tr>';
foreach ($categories as $k=>$category){
    if($k%3==0 && $k!=0){
        echo '</tr><tr>';
    }
    echo '<td><a href="category.php?category='.$category["id"].'">'.$category["category"].'</a></td>';

}
echo '</table>';
于 2012-12-08T20:35:55.540 回答