Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/scala/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Loops DIV循环中每n次之后的PHP_Loops - Fatal编程技术网

Loops DIV循环中每n次之后的PHP

Loops DIV循环中每n次之后的PHP,loops,Loops,嗨,我想在循环中每第三个div之后添加内容。下面是代码,但我甚至没有得到渲染内容“嗨,这是3d div” 它不是每三个div检测一次 <?php function q_list_item($q_item) { $count = 0; $this->output('<DIV>'); $this->my_items; $this->output('</DIV>')

嗨,我想在循环中每第三个div之后添加内容。下面是代码,但我甚至没有得到渲染内容“嗨,这是3d div”

它不是每三个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';
        }

    }
?>

---[实际功能]-----------------------------------------------------

<?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 -->', '');

}
?>

您正在将此函数顶部的
$count
重置为0,因此在函数末尾运行if语句时,它将始终为1

这可能有助于解决您的问题,尽管我无法判断您的代码是否在类中,因为它看起来不像,但您在其中使用了
$This->
。本质上,将计数器的实例化移到函数之外:

<?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';
        }

    }
?>



在循环外初始化$count,否则当它到达if语句时,count将始终为1

我对PHP不是很熟练,但除非我遗漏了什么,否则每次调用此函数时,您的计数都将重置为零。因为您正在函数内声明计数。giving error
Parse error:语法错误,意外的T_变量,在
giving error
Parse error:语法错误,意外的T_变量,在
@pixelngrain中意外的T_函数您发布了整个代码示例还是仅仅是一个片段。如果你想要一个完美的答案,你可能需要显示更多的代码。简而言之,无论在哪里定义了
$this->output
,都需要定义
private$q\u list\u count=0
然后在函数中使用
$this->q\u list\u count
。我已经修改了问题并添加了实际函数。请引导我。谢谢
<?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';
        }

    }
?>