Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/image/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
用PHP自动生成图库_Php_Image_Function_Math_Count - Fatal编程技术网

用PHP自动生成图库

用PHP自动生成图库,php,image,function,math,count,Php,Image,Function,Math,Count,我有一个最小的2个图像和最多8个图像每页。根据我拥有的图像数量,它将生成不同的图库 这是一张它应该如何工作的图表 8 - 4x4 7 - 4x3 6 - 3x3 5 - 3x2 4 - 4 3 - 3 2 - 2 第一列是图像总数。以下数字是一行中的图像 示例 如果我有7个图像,它应该返回如下数组: array(4,3) 最后的结果是,我将在第一行循环出4个图像,然后在第二行循环出3个较大的图像 If语句 我可以用if语句来计算,但我想可能有一种方法来计算这个 适合所有人的解决方案 如果这对

我有一个最小的2个图像和最多8个图像每页。根据我拥有的图像数量,它将生成不同的图库

这是一张它应该如何工作的图表

8 - 4x4
7 - 4x3
6 - 3x3
5 - 3x2
4 - 4
3 - 3
2 - 2
第一列是图像总数。以下数字是一行中的图像

示例

如果我有7个图像,它应该返回如下数组:

array(4,3)
最后的结果是,我将在第一行循环出4个图像,然后在第二行循环出3个较大的图像

If语句

我可以用if语句来计算,但我想可能有一种方法来计算这个

适合所有人的解决方案

如果这对更多的人有用,那么它需要这样一个函数:

function getGalleryRows($count, $row_limit) {
}

其中,
$count
是图像的数量,
$row\u limit
是一行中可以包含的最大图像数量。

我不知道我是否理解正确,但这是您想要的吗

function getGalleryRows($count, $row_limit) {
  $arr = array();
  while ($count > 0){
    array_push($arr, (($count > $row_limit) ? $row_limit : $count));
    $count -= $row_limit;
  }
  return $arr;
}
例子

echo json_encode(getGalleryRows(7,2)); // returns [2,2,2,1]
echo json_encode(getGalleryRows(7,4)); // returns [4,3]

我不知道我是否理解正确,但这是你想要的吗

function getGalleryRows($count, $row_limit) {
  $arr = array();
  while ($count > 0){
    array_push($arr, (($count > $row_limit) ? $row_limit : $count));
    $count -= $row_limit;
  }
  return $arr;
}
例子

echo json_encode(getGalleryRows(7,2)); // returns [2,2,2,1]
echo json_encode(getGalleryRows(7,4)); // returns [4,3]

天哪!你弄明白了!又短又甜。我真的想打破它,但我做不到。干得好天哪!你弄明白了!又短又甜。我真的想打破它,但我做不到。干得好