【问题标题】:PHP loop for printing MYSQLi array not working properly用于打印 MYSQLi 数组的 PHP 循环无法正常工作
【发布时间】:2013-11-08 00:45:29
【问题描述】:

所以发生的事情是,我将 mysql 数据库中的 4 行打印到一个 div 中,然后关闭该 div,然后创建一个新 div,然后打印 4 行,然后结束该 div。依此类推,直到我的数据库中不再有任何内容。

现在完成后,它将打印一个用户可以添加到数据库的表单(在打印姓氏之后)(如果一个 div 中已经有 4 个名称,那么它必须创建另一个分) 下面的代码是我目前拥有的,它可以工作,但是它有一些问题。 问题是: - 不打印 DB 的第一个条目,它从第二个条目开始打印。 - 在打印所有名称的末尾,它会打印内容div,但其中没有名称。 (其中 1 或 2 个) - 如果一个 div 中已经有 4 个名称,则该表单不会为该表单创建另一个 div

通过摆弄$count2 变量,我可以让它摆脱那些空的内容 div,但是我的提交按钮失去了功能(它在那里,但你不能点击它)

谁能发现并纠正问题所在?

非常感谢大家!

P.S 我尽可能地发表评论,以便您了解我认为应该发生的事情。

$countsql = <<<SQL
            SELECT *
            FROM `deathnote`
SQL;

if ($stmt = mysqli_prepare($db, $countsql)) {

    /* execute query */
    mysqli_stmt_execute($stmt);

    /* store result */
    mysqli_stmt_store_result($stmt);

    $countresult = mysqli_stmt_num_rows($stmt);
}
$count2=0; //count how many overall names have been printed
$pagecount=0; //count for how many names are on a page
while($result->fetch_assoc() != NULL){ //While result isn't empty
 echo '<div style="background-image:url(images/paper.jpg);">'; //start new page div
    while ($pagecount < 4) { //loop to put only 4 names on 1 page
    $row = $result->fetch_assoc(); //grab name and cod
                echo '<div class="content"><div class="name">'  . $row['victim'] . '</div><div class="cod">'  . $row['cod'] . '</div></div>'; //display name and cod inside a content div
        $pagecount++; //increase the count of amount of names on page
        $count2++; //increase the overall names printed
        if ($count2 == $countresult) { //if the overall names printed = the total count of whats in the database (meaning there is nothing left to print)
        echo '<div class="content"><form action="write.php" method="post"><div class="name">Name: <br/> Cause Of Death: </div><div class="cod"><textarea type="text" name="name" maxlength="25" placeholder="Input Name" required></textarea> <br /> <textarea type="text" name="cod" maxlength="130" rows="4" placeholder="Cause of Death" required></textarea> <br /><input type="submit" id="button" value="Submit"></div></form></div>'; //print the form for user to add to the database
        }

    }
$pagecount=0; //because there is 4 names printed, we have to set the count back to 0
echo '</div>'; //end the 'page' div (which will end the page)
}

【问题讨论】:

    标签: php mysqli


    【解决方案1】:

    如果我理解正确,数据库记录应该以四条记录为一组输出,直到没有更多记录为止。

    将数据库内容提取到数组中然后遍历数组可能更容易。

    但是,这里的方法与您上面提供的代码类似。
    代码被注释以试图解释发生了什么。

    // initialize record counter
    $count=0;
    
    // loop through query result
    while($row->fetch_assoc($stmt)){
    
        // increment record counter
        $count++;
    
        // if this is the first record in this group, start a new page div
        if ($count==1) {
          ?><div style="background-image:url(images/paper.jpg);"><?php
        }
    
        //display name and cod inside a content div
        ?><div class="content">
          <div class="name"><?=$row['victim']?></div>
          <div class="cod"><?=$row['cod']?></div>
        </div><?php
    
        // if this is fourth record in group, close page div and reset counter
        if ($count==4) {
          ?></div><?php
          $count=0;
        }
    
    }
    
    // if the last group had less than four items, close final page div.
    if ($count>0) {
      ?></div><?php
    }
    
    //print the form for user to add to the database
    ?><div class="content">
        <form action="write.php" method="post">
            <div class="name">
                Name:<br/>
                Cause Of Death:
            </div>
            <div class="cod">
                <textarea type="text" name="name" maxlength="25" placeholder="Input Name" required></textarea><br />
                <textarea type="text" name="cod" maxlength="130" rows="4" placeholder="Cause of Death" required></textarea><br />
                <input type="submit" id="button" value="Submit">
            </div>
        </form>
    </div>
    

    您的问题:

    1.不打印 DB 的第一个条目,它从第二个条目开始打印。

    • 这是因为您有两个嵌套的“获取”循环。您只需要一个。

    2。在打印所有名称结束时,它会打印内容 div,但其中没有名称。

    • 这是因为您的“获取”循环中有一个“页数”循环。
      因此,即使在最后一条记录上,它仍然会输出四个内容框。

    3.如果 div 中已经有 4 个名称,则该表单不会为该表单创建另一个 div。

    • 这是因为您的表单输出取决于包含四个项目的最后一组。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-04-13
      • 2015-02-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-01-01
      相关资源
      最近更新 更多