0

Hi I want to add content after every 3rd div in the loop. Here is below code but I am not getting even render content "Hi this is the 3d div"

Its not detecting every 3rd div.

<?php
    function q_list_item($q_item)
    {
        $count = 0;

        $this->output('<DIV>');
        $this->my_items;    
        $this->output('</DIV>');

        $count++;           

        if($count % 3 == 0) {
            echo 'Hi this is the 3rd div';
        }

    }
?>

----[Actual Function]-----------------------------------------------

<?php

function q_list_item($q_item)
{

    $this->output('<DIV CLASS="qa-q-list-item'.rtrim(' '.@$q_item['classes']).'" '.@$q_item['tags'].'>');

    $this->q_item_stats($q_item);
    $this->q_item_main($q_item);
    $this->q_item_clear();

    $this->output('</DIV> <!-- END qa-q-list-item -->', '');

}
?>
1
  • I'm not fluent in PHP, but unless I'm missing something, your count is getting reset to zero every time this function is called. Since you're declaring count inside the function. Commented Jun 7, 2012 at 15:13

2 Answers 2

2

You are resetting the $count to 0 at the top of this function, so it will always be 1 when you run the if statement at the end of the function.

This may help with your issue, although I can't tell if your code is in a class or not as it doesn't look like it is, but you are using $this-> in there. Essentially, move the instantiation of the counter outside of the function:

<?php
    $q_list_count = 0;

    function q_list_item($q_item)
    {
        $q_list_count++;

        $this->output('<DIV>');
        $this->my_items;    
        $this->output('</DIV>');

        if($q_list_count % 3 == 0) {
            echo 'Hi this is the 3rd div';
        }

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

3 Comments

giving error Parse error: syntax error, unexpected T_VARIABLE, expecting T_FUNCTION in
@pixelngrain have you posted an entire code sample or just a snippet. You may need to show more of your code if you want the perfect answer. In short, wherever $this->output is defined, you need to define private $q_list_count = 0; and then use $this->q_list_count in your function.
I have modified the question and added actual function. Please guide me. Thanks a lot
0
<?php
    $count = 0;
    function q_list_item($q_item)
    {
        $this->output('<DIV>');
        $this->my_items;    
        $this->output('</DIV>');

        $count++;           

        if($count % 3 == 0) {
            echo 'Hi this is the 3rd div';
        }

    }
?>

initialize the $count outside the loop, otherwise count will always be 1 when it reaches the if statement

1 Comment

giving error Parse error: syntax error, unexpected T_VARIABLE, expecting T_FUNCTION in

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.