简体   繁体   English

Foreach循环多次显示相同的行

[英]Foreach loop are displaying same rows multiple times

I have a problem with my first attempt at making a foreach loop. 我第一次尝试进行foreach循环时遇到问题。

My problem is, that I'm only trying to call out two rows to be displayed, which kinda works, although they are being displayed as many times as there are different rows in my table. 我的问题是,我只想调出要显示的两行,这行得通,尽管它们的显示次数与表中不同行的显示次数相同。

My code looks like this: 我的代码如下所示:

$sql = "SELECT * FROM webpages";
$result = mysql_query($sql);
$assoc_query = mysql_fetch_assoc($result);

<?php foreach ($assoc_query as $value) { ?>
<tr>
   <td>
     <div id='pageimg'><img src= <?php echo $assoc_query['pic'];?>  ></div>
   </td>
   <td>
     <div id="pagename"><?php echo $assoc_query['name']; ?> </div>
   </td>
</tr>
<?php } ?>

It's being displayed like so on the page: 它在页面上的显示方式如下:

/picture/ DAK
/picture/ DAK
/picture/ DAK
/picture/ DAK

Hope you can help me:) 希望你能帮我:)

<?php 
$sql = "SELECT * FROM webpages";
$result = mysql_query($sql);

$counter=0;
while($row= mysql_fetch_assoc($result))
{
    $assoc_query[$counter] = $row;
    $counter++;
}

foreach ($assoc_query as $value) { ?>
    <tr>
        <td>
            <div id='pageimg'><img src= <?php echo $value['pic'];?>  ></div>
        </td>
        <td>
            <div id="pagename"><?php echo $value['name']; ?> </div>
        </td>
    </tr>
<?php } ?>

this can't be done with foreach loop unless you gonna show 1 user's information only using limit Clause or Where Clause otherwise that it's useless 除非您仅使用limit子句或Where子句显示1个用户的信息,否则无法使用foreach循环来完成此操作,否则将无用

instead you need a while loop like this 相反,您需要这样的while循环

<?php while ($assoc_query = mysql_fetch_assoc($result)) { ?>
<tr>
   <td>
     <div id='pageimg'><img src= <?php echo $assoc_query['pic'];?>  ></div>
   </td>
   <td>
     <div id="pagename"><?php echo $assoc_query['name']; ?> </div>
   </td>
</tr>
<?php } ?>

Instead of foreach you will want a while. 而不是foreach,您将需要一段时间。 What you are doing here is looping the elements of array $assoc_query. 您在这里正在循环$ assoc_query数组的元素。

A while loop will get the next result set to be used. while循环将获取下一个要使用的结果集。

while($assoc_query = mysql_fetch_assoc($result)){
    //Do your thing
}

Also please consider updating from mysql_ to mysqli_ or PDO. 另外,请考虑从mysql_更新到mysqli_或PDO。 What you are using is depreciated and there is added security with the newer ones. 您使用的设备已过时,并且较新的设备增加了安全性。

You should do it like this: 您应该这样做:

$sql = "SELECT * FROM webpages";
$result = mysql_query($sql);
while ($assoc_query = mysql_fetch_assoc($result)) {
?>
<tr>
   <td>
     <div id='pageimg'><img src= <?php echo $assoc_query['pic'];?>  ></div>
   </td>
   <td>
     <div id="pagename"><?php echo $assoc_query['name']; ?> </div>
   </td>
</tr>
<?php } ?>

That should work. 那应该工作。 But as said before you shouldn't code like this. 但是正如之前所说,您不应该这样编写代码。 Use mysqli or PDO like nerdlyist says. 像nerdlyist所说的那样使用mysqli或PDO。

@Nerdlyist @书呆子

$sql = "SELECT * FROM webpages";
$result = mysql_query($sql);
$assoc_query = mysql_fetch_assoc($result);


<?php while ($assoc_query = mysql_fetch_assoc($result)) {
?>
<tr>
   <td>
     <div id='pageimg'><img src= <?php echo $assoc_query['pic'];?>  ></div>
   </td>
   <td>
     <div id="pagename"><?php echo $assoc_query['name']; ?> </div>
   </td>
</tr> 
<?php } ?>

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

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