简体   繁体   English

PHP的变量增加和回声

[英]php variable increasing and echo

I have a problem whit this function. 我有这个功能的问题。 i want for every 3 fetched rows to put in begining and in the end. 我想将每3个提取的行放在开头和结尾。 the problem is that it works only first time only for first 3 rows...after that he output the div tag on each fetched row. 问题是它仅在前3行中只有第一次工作...之后,他在每条获取的行上输出div标签。

    function projects($mysqli)
    {   $ret = "";
    $count = 0;
    $divstart = "";
    $divend = "";
    $statement = $mysqli->query("SELECT id,name,description FROM projects");
    while($row = $statement->fetch_array())
    { $count++;
        if ($count == 4 ) {$divstart ='<div class="row">'; $divend = "</div>"; $count == 0;  }
        $ret = $ret. ''.$divstart.'
                        <div class="col-lg-4">
                        <div class="panel panel-primary">
                            <div class="panel-heading">
                                Primary Panel
                            </div>
                            <div class="panel-body">
                                <p>'.$row['description'].'</p>
                            </div>
                            <div class="panel-footer">
                                Panel Footer
                            </div>
                        </div>
                      </div>
                      '.$divend.' ';                                                            
    }
    return $ret;
}

您需要的是一个模数运算符,以影响第4行。

 if ($count % 4 == 0) {$divstart = //....etc

You are not clearing your start/end variables, you want to clear them when $count != 4 also you had two == on your reset of $count when you only wanted one. 您不是要清除开始/结束变量,而是要在$count != 4时清除它们,而在重置$count时只有两个==时才需要。

    if ($count == 4 ) {
      $divstart = '<div class="row">'; 
      $divend = "</div>";
      $count = 0;  
    } else {
      $divstart = '';
      $divend = '';
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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