[英]Is there a better way to handle this in php while?
I know this is simple for you guys but I am not able to apply my brain. 我知道这对你们来说很简单,但是我无法动脑筋。
I want to print <p>
with same content n times depending on the value of n . 我想 根据n的值 打印 具有相同内容的 <p>
n次 。
<?php while ($row = mysqli_fetch_assoc($result)) { ?>
if($row['i'] == 1) {
<p>Content</p>
} else if($row['i'] == 2) {
<p>Content</p>
<p>Content</p>
} else if($row['i'] == 3) {
<p>Content</p>
<p>Content</p>
<p>Content</p>
}
If $row['i'] == 100
then i have to write 100 times. 如果$row['i'] == 100
那么我必须写100次。
Any Better Solution. 任何更好的解决方案。 Thanks. 谢谢。
w3schools.com : PHP for loops execute a block of code a specified number of times. w3schools.com:PHP for循环执行代码块指定次数。
In your case, specified number of times will be $row['i']
. 在您的情况下,指定的次数将是$row['i']
。
for($i=0;$i<$row['i'];$i++){
echo '<p>Content</p>';
}
Try this. 尝试这个。 Use a for loop
inside while
and add upper limit to $row['i']
; 在while
内使用for loop
,并将上限添加到$row['i']
; so it will print your content as many times as the value of $row['i']
因此它将打印您内容的次数是$row['i']
<?php while ($row = mysqli_fetch_assoc($result)) {
for($i=0;$i<$row['i'];$i++){
echo '<p>Content</p>';
}
}
I think this code will help you: 我认为这段代码将帮助您:
while ($row = mysqli_fetch_assoc($result)) {
$j=1;
for($i=0;$i<$j;$i++){
echo '<p>Content</p>';
}
$j++;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.