如何在连续显示5件事后制作内爆?

时间:2012-09-07 05:33:31

标签: php

我想让implode只显示5个数组,之后它会自动创建一个新行,再次显示5个数组,并且它会不断重复。喜欢是否可以使用<br />或其他我必须使用的东西?

$result = mysql_query("SELECT * FROM `im_album` WHERE username = '".$user_data['username']."'  ");
$types = array();
$d_array = array();
while(($row =  mysql_fetch_assoc($result))) {
    $types[] = $row['name'];
    $d_array[] = $row['description'];
}
echo "<h1>".implode($types )."</h1>"

2 个答案:

答案 0 :(得分:2)

你可以这样做:

<?php

//sample array
$types = array("John", "Doe", "Bar", "Baz", "Stock", "Overflow", "Meta" );

//Count the number of elements in $types
$types_count = count($types);

//Use $foo1, $foo2, $foo3 as the output array names
$out_types_count = 1;
$out_types_arr = "foo".$out_types_count;


for($i=1;$i<=$types_count;$i++){
    $out_types_arr[] = $types[$i];
    if(($i%5) == 0){ // if we $i == 5,10,15 etc, 
        $out_types_count++; // use $foo2
        $out_types_arr = "foo".$out_types_count;
    }
}// for loop ENDS

//Echo all the output    
for($i=1;$i<=$out_types_count;$i++){
    for($j=0;$j<=5;$j++){
        echo $out_types_arr.$i[$j];
    }
    echo "<br />".PHP_EOL;
}

?>

P.S。代码有一些错误&amp;次要的不希望的输出。因为OP得到了他想要的答案,所以我稍后会在家里纠正它。

答案 1 :(得分:2)

试试这个:

$result = mysql_query("SELECT * FROM `im_album` WHERE username = '".$user_data['username']."'  ");
$types = array();
$d_array = array();
$types_str="";
$types_str_array=array();
$temp=1;
while(($row =  mysql_fetch_assoc($result))) {

    $types_str .=$row['name'];

    if($temp!=1 && $temp%5==0)
    {
        $types_str_array[]=$types_str;
        $types_str="";
    }

    $types[] = $row['name'];
    $d_array[] = $row['description'];

    $temp++;
}
echo "<h1>".implode("<br />",$types_str_array )."</h1>";