gpt4 book ai didi

php - 如何在 PHP 的 while 循环中分割返回的结果

转载 作者:行者123 更新时间:2023-11-29 12:57:44 25 4
gpt4 key购买 nike

我正在开发一个应用程序,我需要返回两行结果并在它们中间创建一个标签以显示 VS 图标(这是竞争)。在某个阶段我需要分割结果,对吗?如何实现这一点,

结果应该像

<div class='hero_comp'>

<div class='hero_comp_img'><img src='images/$picture' alt='$name' width='200' height='200'/></div>

<div class='hero_comp_txt'>$reason</div>

<div class='hero_comp_btn'><form action='' method='post'><input type='hidden' name='id' value='$id'/><button type='submit' class='btn btn-primary border-radius-zero' name='vote'><i class='fa fa-thumbs-up'></i> Vote for me</button></form></div>

<div class='vs'></div>


<div class='hero_comp'>

<div class='hero_comp_img'><img src='images/$picture' alt='$name' width='200' height='200'/></div>

<div class='hero_comp_txt'>$reason</div>

<div class='hero_comp_btn'><form action='' method='post'><input type='hidden' name='id' value='$id'/><button type='submit' class='btn btn-primary border-radius-zero' name='vote'><i class='fa fa-thumbs-up'></i> Vote for me</button></form></div>

这就是我到目前为止所做的

$select_comp=mysql_query("SELECT * FROM nominate WHERE accepted='1' ORDER BY id DESC");
if (mysql_num_rows($select_comp) >=2 ) {

while ($row=mysql_fetch_array($select_comp)) {

$picture=$row['picture'];
$reason=$row['reason'];
$id=$row['id'];
$name=$row['name'];

echo"

<div class='hero_comp'>

<div class='hero_comp_img'><img src='images/$picture' alt='$name' width='200' height='200'/></div>

<div class='hero_comp_txt'>$reason</div>

<div class='hero_comp_btn'><form action='' method='post'><input type='hidden' name='id' value='$id'/><button type='submit' class='btn btn-primary border-radius-zero' name='vote'><i class='fa fa-thumbs-up'></i> Vote for me</button></form></div>

</div>

";

}
}

最佳答案

如果您想要完成的是循环遍历结果并显示结果 1 VS 结果 2,然后继续进行下一个配对,您可以在循环中添加一个计数器来确定您在模式中的位置。

$count=1;
$select_comp=mysql_query("SELECT * FROM nominate WHERE accepted='1' ORDER BY id DESC");
if (mysql_num_rows($select_comp) >=2 ) {

while ($row=mysql_fetch_array($select_comp)) {

$picture=$row['picture'];
$reason=$row['reason'];
$id=$row['id'];
$name=$row['name'];

echo"

<div class='hero_comp'>

<div class='hero_comp_img'><img src='images/$picture' alt='$name' width='200' height='200'/></div>

<div class='hero_comp_txt'>$reason</div>

<div class='hero_comp_btn'><form action='' method='post'><input type='hidden' name='id' value='$id'/><button type='submit' class='btn btn-primary border-radius-zero' name='vote'><i class='fa fa-thumbs-up'></i> Vote for me</button></form></div>

</div>

";

if ($count==1) {

/* Display VS Button since the 1st result has been posted, increase count to 2 for next loop */

$count=2;

} elseif ($count==2) {

/* If you want to insert something to separate the Result 1 VS Result 2 just echoed, go for it here, otherwise we set the count back to 1. */

$count=1;

}
}
}

这会强制在您的表中进行 1 vs 2、3 vs 4、5 vs 6 配对,因为您是按 ID 排序的。如果您想指定不同的配对,还有其他几种方法可以实现。

关于php - 如何在 PHP 的 while 循环中分割返回的结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23785983/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com