[英]Php Array Count to run script run for each reply till 3rd reply and not more than that
I have a Forum in which adscript is displayed after every reply 我有一个论坛,每次回复后都会显示adscript
<?php if( count( $replies ) <= 3 ){ ?>
---- script ----
<?php } ?>
Earlier there was a clause that there could be maximum 3 replies per Forum post - but now that clause is changed and there are 5, 7 or 10 replies even 之前有一个条款,每个论坛帖子最多可以回复3条-但是现在该条款已更改,甚至有5、7或10条回复
I want to modify code so EVEN If there are more than 3 replies - then script should run after each reply till 1st 3 replies and not from 4th reply onwards 我想修改代码,即使是超过3个回复-脚本也应在每个回复后运行,直到第1个3个回复,而不是从第4个回复开始
I believe an array is to used likely 我相信数组很可能会用到
<?php
$replyCount = 0;
foreach( $replies as $reply )
{
$replyCount++;
$replyNumber = array(1, 2, 3);
if (in_array($replyCount, $replyNumber)) {
echo "script";
}
?>
But - what the above script is doing is that 但是-以上脚本正在执行的操作是
Its displaying the script in all replies
它在所有回复中显示脚本
Its displaying script 3 times after each reply
每次回复后显示其脚本3次
Can some one help and advise in modification so that 可以提供一些帮助和建议以进行修改,以便
Script should run once after each reply
脚本应在每次回复后运行一次
Script should run till 3rd reply and not more than that
脚本应运行到第三次答复为止
Pls help and advise 请帮助和建议
add break;
加
break;
after echo 'script'
. echo 'script'
。
You said: 你说:
Its displaying the script in all replies
它在所有回复中显示脚本
Its displaying script 3 times after each reply每次回复后显示其脚本3次
That's because your for loop is doing 3 iterations since on the first iteration replyCount
gets value of 1, is 1 in the array? 这是因为您的for循环正在执行3次迭代,因为在第一次迭代中
replyCount
的值为1,数组中是否为1? yes. 是。 Second iteration
replyCount
gets value of 2 etc etc. 第二次迭代
replyCount
的值为2等,等等。
EDIT: Answer to your comments: 编辑:回答您的评论:
<?php
/* CASE Where number of replies is 3 or less. */
/* Prints "script" 1 extra time than count($replies) */
$counter = 0;
if(count($replies) <= 3) {
foreach($replies as $reply) {
if(count($replies) == $counter) {
break;
}
else {
echo "script";
}
$counter++;
}
}
/* CASE Where number of replies is 4 or more print script 3 times. */
else {
echo 'script' . '<br />';
echo 'script' . '<br />';
echo 'script' . '<br />';
}
?>
Please read the current code posted here and then see the cases: 请阅读此处发布的当前代码,然后查看案例:
Case 1: count($replies) = 0
情况1:
count($replies) = 0
foreach loop starts
foreach循环开始
firstIF
fires, nothing happensIF
第一次发生火灾,什么也没发生
Case 2: count($replies) = 1
情况2:
count($replies) = 1
foreach loop starts
foreach循环开始
go intoelse
进入
else
print script打印脚本
$counter
is now 1$counter
现在是1
count($replies) == $counter
stop loop停止循环
result is that we have 'script' printed 1 time.结果是我们打印了1次“脚本”。
Case 3: count($replies) = 2
情况3:
count($replies) = 2
foreach loop starts
foreach循环开始
go intoelse
进入
else
print script打印脚本
$counter
is now 1$counter
现在是1
go intoelse
进入
else
print script打印脚本
$counter
is now 2$counter
现在是2
count($replies) == $counter
stop loop停止循环
result is that we have 'script' printed 2 times.结果是我们将“脚本”打印了两次。
Case 4: count($replies) = 3
情况4:
count($replies) = 3
foreach loop starts
foreach循环开始
go intoelse
进入
else
print script打印脚本
$counter
is now 1$counter
现在是1
go intoelse
进入
else
print script打印脚本
$counter
is now 2$counter
现在是2
go intoelse
进入
else
print script打印脚本
$counter
is now 3$counter
现在是3
count($replies) == $counter
stop loop停止循环
result is that we have 'script' printed 2 times.结果是我们将“脚本”打印了两次。
Case 5: count($replies) > 3
情况5:
count($replies) > 3
result is that we have 'script' printed 3 times.
结果是我们打印了3次“脚本”。
These are the results if you go over the iterations of this foreach
loop. 如果您遍历此
foreach
循环的迭代,这些就是结果。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.