如何让这个php脚本重复表中的行数?我有点迷失如何开始,再次感谢。
<?php
$con = mysqli_connect("localhost", "root", "xxx", "xxx");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con, "SELECT * FROM caseStudies");
$row = mysqli_fetch_array($result);
for ($i = 0; $i < 1; $i++)
{
echo "<div class='case' id='case$i'>";
echo "<div class='row'>";
echo "<div class='header'>";
echo '<h4>' . $row['caseName'] . '</h4>';
echo "</div>";
echo "</div>";
echo "<div class='row'>";
echo "<div class='caseimg'>";
echo '<img src="' . $row['caseImage'] . '" class="Responsive image"/>';
echo "</div>";
echo "<div class='caseimg'>";
echo '<img src="' . $row['caseImage'] . '" class="Responsive image"/>';
echo "</div>";
echo "<div class='caseimg'>";
echo '<img src="' . $row['caseImage'] . '" class="Responsive image"/>';
echo "</div>";
echo "</div>";
$i++;
}
}
答案 0 :(得分:2)
你在forloop中的状况不正确。使用方法:
$i = 0;
while ($row = mysqli_fetch_assoc($result)) {
echo ...
$i++; //if you need $i
}
代替
答案 1 :(得分:2)
您可以使用mysqli_num_rows和mysqli_fetch_assoc()
试试这个: -
$result = mysqli_query($con,"SELECT * FROM caseStudies");
$ispresent = @mysqli_num_rows($result);
$i = 0;
if($ispresent > 0) // if row present then only run your while loop
{
while($row = mysqli_fetch_assoc($result))
{
echo "<div class='case' id='case$i'>";
echo "<div class='row'>";
echo "<div class='header'>";
echo '<h4>' . $row['caseName'] . '</h4>';
echo "</div>";
echo "</div>";
echo "<div class='row'>";
echo "<div class='caseimg'>";
echo '<img src="' . $row['caseImage'] . '" class="Responsive image"/>';
echo "</div>";
echo "<div class='caseimg'>";
echo '<img src="' . $row['caseImage'] . '" class="Responsive image"/>';
echo "</div>";
echo "<div class='caseimg'>";
echo '<img src="' . $row['caseImage'] . '" class="Responsive image"/>';
echo "</div>";
echo "</div>";
$i++;
}
}
答案 2 :(得分:1)
您可以使用while
或foreach
<?php
$con=mysqli_connect("localhost","root","xxx","xxx");
// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM caseStudies");
$i=1;
while($row = mysqli_fetch_array($result))
{
echo "<div class='case' id='case$i'>";
echo "<div class='row'>";
echo "<div class='header'>";
echo '<h4>' . $row['caseName'] . '</h4>';
echo "</div>";
echo "</div>";
echo "<div class='row'>";
echo "<div class='caseimg'>";
echo '<img src="' . $row['caseImage'] . '" class="Responsive image"/>';
echo "</div>";
echo "<div class='caseimg'>";
echo '<img src="' . $row['caseImage'] . '" class="Responsive image"/>';
echo "</div>";
echo "<div class='caseimg'>";
echo '<img src="' . $row['caseImage'] . '" class="Responsive image"/>';
echo "</div>";
echo "</div>";
$i++;
}
?>
答案 3 :(得分:1)
在这种情况下,您应该使用WHILE循环,如下所示:
$con=mysqli_connect("localhost","root","xxx","xxx");
// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM caseStudies");
$i=0;
while($row = mysqli_fetch_array($result)){
echo "<div class='case' id='case$i++'>";
echo "<div class='row'>";
echo "<div class='header'>";
echo '<h4>' . $row['caseName'] . '</h4>';
echo "</div>";
echo "</div>";
echo "<div class='row'>";
echo "<div class='caseimg'>";
echo '<img src="' . $row['caseImage'] . '" class="Responsive image"/>';
echo "</div>";
echo "<div class='caseimg'>";
echo '<img src="' . $row['caseImage'] . '" class="Responsive image"/>';
echo "</div>";
echo "<div class='caseimg'>";
echo '<img src="' . $row['caseImage'] . '" class="Responsive image"/>';
echo "</div>";
echo "</div>";
}
你可以使用for循环,如果你真的想要/需要通过用for mysqli_num_rows()函数替换for循环的比较部分中的1:
for($i=0;$i<mysqli_num_rows($result);$i++)
答案 4 :(得分:0)
$countRes = mysqli_num_rows($result);
for($i=0; $i<$countRes; $i++){ ... }
使用mysqli_num_rows($result)
获取记录数。
答案 5 :(得分:0)
使用: while($row = mysqli_fetch_array($result))
而不是: for($i = 0; $i < 1; $i++)
答案 6 :(得分:0)
你可以使用while
循环,因为其他人说每行都会这样做
(虽然会自动停止和最后一行,但您不需要知道您拥有的行数)
如果您只需要两次,请保留您的代码但是:您有缺少</div>
编辑:您的编辑似乎已更正了遗失的</div>