0

我想让 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>"
4

2 回答 2

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;
}

?>

PS 代码有一些错误和少量不想要的输出。因为 OP 已经得到了他想要的答案,所以我稍后会在家里更正它。

于 2012-09-07T05:47:53.037 回答
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>";
于 2012-09-07T05:55:53.057 回答