简体   繁体   English

根据count生成带有php的数字字符串

[英]Generate number string with php based on count

I'm building a website where I use ascensor.js ( ascensor.js 我正在建立一个网站,我使用ascensor.js( ascensor.js

In order for this to work we have to provide a string like this "1|1 & 1|2 & 1|3 & 1|4 & 2|1 & 2|2 & 2|3 & 2|4 & 3|1 & 3|2 & 3|3 & 3|4" 为了实现这一点,我们必须提供类似这样的字符串“1 | 1&1 | 2&1 | 3&1 | 4&2 | 1&2 | 2&2 | 3&2 | 4&3 | 1 &3 | 2&3 | 3&3 | 4“

This will allow the script to build a kind of navigation. 这将允许脚本构建一种导航。

I adapted the script to into wordpress so that layout and ascensor is automatically build, even in category archive pages. 我将脚本改编成wordpress,以便自动构建布局和提升器,即使在类别存档页面中也是如此。

The only thing that I'm stuck with is generate this string based on the number of post to display. 我唯一坚持的是根据要显示的帖子数生成此字符串。

For example if I have 10 posts to display, I would like to have 4 rows, with 3 post by rows (in the fourth rows, there would be of course only one post). 例如,如果我要显示10个帖子,我希望有4行,每行3个(在第四行中,当然只有一个帖子)。 I would like to always have something more or less squared, 4 by 5 or 7 by 8 or 6 by 6. So if i have 12 posts for example, the string I would need would be something like the one above. 我想总是有一些或多或少的平方,4乘5或7乘8或6乘6.所以如果我有12个帖子例如,我需要的字符串就像上面的那个。

how would you generate this with PHP ? 你会如何用PHP生成这个? because for the moment I do it like this ;-) 因为目前我这样做;-)

 $countpost=count($postslist);
 if ( $countpost === 10) {$AscensorMap="1|1 & 1|2 & 1|3 & 1|4 & 2|1 & 2|2 & 2|3 & 2|4 & 3|1 & 3|2";}
 if ( $countpost === 11) {$AscensorMap="1|1 & 1|2 & 1|3 & 1|4 & 2|1 & 2|2 & 2|3 & 2|4 & 3|1 & 3|2 & 3|3";}
 if ( $countpost === 12) {$AscensorMap="1|1 & 1|2 & 1|3 & 1|4 & 2|1 & 2|2 & 2|3 & 2|4 & 3|1 & 3|2 & 3|3 & 3|4";}
 if ( $countpost === 13) {$AscensorMap="1|1 & 1|2 & 1|3 & 1|4 & 2|1 & 2|2 & 2|3 & 2|4 & 3|1 & 3|2 & 3|3 & 3|4 & 4|1";}
 if ( $countpost === 14) {$AscensorMap="1|1 & 1|2 & 1|3 & 1|4 & 2|1 & 2|2 & 2|3 & 2|4 & 3|1 & 3|2 & 3|3 & 3|4 & 4|1 & 4|2";}
 if ( $countpost === 15) {$AscensorMap="1|1 & 1|2 & 1|3 & 1|4 & 2|1 & 2|2 & 2|3 & 2|4 & 3|1 & 3|2 & 3|3 & 3|4 & 4|1 & 4|2 & 4|3";}
 if ( $countpost === 16) {$AscensorMap="1|1 & 1|2 & 1|3 & 1|4 & 2|1 & 2|2 & 2|3 & 2|4 & 3|1 & 3|2 & 3|3 & 3|4 & 4|1 & 4|2 & 4|3 & 4|4";}

As in some pages, I will have around 50 posts to list, I would like to generate this with php. 在某些页面中,我将有大约50个帖子列出,我想用php生成这个。

Any help welcome ! 欢迎任何帮助!

Thank you. 谢谢。

I would suggest calculating the lowest square number greater than your actual number of pages: 我建议计算大于实际页数的最小平方数:
$columns = pow(ceil(sqrt($countpost)),2);

Then you can determine the number of rows there will be: $rows = ceil($countpost/$columns); 然后你可以确定将有的行数: $rows = ceil($countpost/$columns);

Now you can use a for loop to generate your string: 现在,您可以使用for循环生成字符串:

$arr = []; // array() before PHP 5.4
for( $y=0; $y<$rows; $y++) {
    for( $x=0; $y*$columns+$x < $countpost && $x < $columns; $x++) {
        $arr[] = ($y+1)."|".($x+1);
    }
}
$AscensorMap = implode(" & ",$arr);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM