简体   繁体   中英

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

<?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

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

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

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; after echo 'script' .

You said:

Its displaying the script in all replies
Its displaying script 3 times after each reply

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? yes. Second iteration replyCount gets value of 2 etc etc.

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

foreach loop starts
first IF fires, nothing happens

Case 2: count($replies) = 1

foreach loop starts
go into else
print script
$counter is now 1
count($replies) == $counter
stop loop
result is that we have 'script' printed 1 time.

Case 3: count($replies) = 2

foreach loop starts
go into else
print script
$counter is now 1
go into else
print script
$counter is now 2
count($replies) == $counter
stop loop
result is that we have 'script' printed 2 times.

Case 4: count($replies) = 3

foreach loop starts
go into else
print script
$counter is now 1
go into else
print script
$counter is now 2
go into else
print script
$counter is now 3
count($replies) == $counter
stop loop
result is that we have 'script' printed 2 times.

Case 5: count($replies) > 3

result is that we have 'script' printed 3 times.

These are the results if you go over the iterations of this foreach loop.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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