0

Pl advise on PHP IF statement. How to use it twice like where an ad script when count of listing reaches 4th number and 9th number on a page with multiple listing

When used with below code it does not works in.

    <?php
    $listings = explode("<hr/>", $list);
    $numberOfListings = count($listings);
    for($i = 0; $i < $numberOfListings; ++$i) 
    {
    if ($i == 4)
    { ?> 

   <some ad script runs in>

    if ($i == 9)

    { ?> 

   <some ad script runs in>

    <?php }
    echo $listings[$i] . "<hr/>";
    }
    ?> 
4
  • What's your desired output? What have you tried so far? Commented Jan 8, 2014 at 7:24
  • Do you close your first if statement? Commented Jan 8, 2014 at 7:27
  • You code seems to be syntaxically wrong, so it can't work, If you close PHP, you're displaying HTML, if you want to resume your script with PHP, you have to open again your PHP scripts with opening tag <?php . You should also close your statements... Commented Jan 8, 2014 at 7:30
  • Check out Krish R's answer. Commented Jan 8, 2014 at 7:31

1 Answer 1

2

Can you try this, Added $Reset variable so every loop >10 it reset the count

    <?php
    $listings = explode("<hr/>", $list);
    $numberOfListings = count($listings);
    $Reset =1;
    for($i = 0; $i < $numberOfListings; ++$i) 
    {
        if ($Reset == 4)
        { ?> 

   <some ad script runs in>

   <?php } if ($Reset == 9) { ?> 

   <some ad script runs in>

    <?php }
         echo $listings[$i] . "<hr/>";

         if($Reset>10){
                $Reset =1;
          }
          $Reset++;

    }
    ?>
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks Krish. I have set the reset after 15 as every page is having 15 listing count. Your code works awesome and it was pretty fast response too. Thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.