用于打印MYSQLi数组的PHP循环无法正常工作
所以现在的情况是,我正在将mysql数据库中的4行打印到一个div中,然后关闭该div,然后创建一个新div,然后打印4行,然后结束该div。依此类推,直到我的数据库中不再有任何内容 现在完成后,它将打印一个表单,用户可以在该表单中添加到DB(打印姓氏后riht)(如果一个div中已经有4个名称,那么它将不得不创建另一个div) 下面的代码是我目前拥有的,它可以工作,但是它有一些问题。 这些问题是: -数据库的第一个条目未打印,它从第二个条目开始打印。 -在打印完所有名称后,它将打印内容用于打印MYSQLi数组的PHP循环无法正常工作,php,mysqli,Php,Mysqli,所以现在的情况是,我正在将mysql数据库中的4行打印到一个div中,然后关闭该div,然后创建一个新div,然后打印4行,然后结束该div。依此类推,直到我的数据库中不再有任何内容 现在完成后,它将打印一个表单,用户可以在该表单中添加到DB(打印姓氏后riht)(如果一个div中已经有4个名称,那么它将不得不创建另一个div) 下面的代码是我目前拥有的,它可以工作,但是它有一些问题。 这些问题是: -数据库的第一个条目未打印,它从第二个条目开始打印。 -在打印完所有名称后,它将打印内容div,
div
,但其中没有名称。(其中1或2名)
-如果一个div中已有4个名称,则表单不会为表单创建另一个div
通过摆弄$count2
变量,我可以让它去掉那些空的content div,但是随后我就失去了提交按钮的功能(它就在那里,但是你不能点击它)
有人能发现并纠正问题所在吗
谢谢大家
顺便说一句,我尽我所能地发表评论,以便你们能理解我认为应该发生的事情
$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)
}
// 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>
$countsql=如果我理解正确,数据库记录应该以四条记录为一组输出,直到没有更多记录为止
将数据库内容提取到一个数组中,然后遍历该数组可能更容易
但是,这里有一个与您上面提供的代码类似的方法。
对代码进行注释是为了解释发生了什么
$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)
}
// 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>
//初始化记录计数器
$count=0;
//循环查询结果
while($row->fetch_assoc($stmt)){
//增量记录计数器
$count++;
//如果这是此组中的第一条记录,请启动一个新的页面div
如果($count==1){
?>
名称:
死因:
您的问题:
1.数据库的第一个条目未打印,它从第二个条目开始打印。
- 这是因为您有两个嵌套的“fetch”循环,您只需要一个
2.在打印完所有名称后,它会打印内容div,但其中没有名称。
- 这是因为在“fetch”循环中有一个“page count”循环。
因此,即使在最后一条记录上,它仍然输出四个内容框
3.如果一个div中已有4个名称,则表单不会为表单创建另一个div。
- 这是因为表单输出依赖于包含四个项的最后一个组