首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在while循环PHP中拆分返回的结果

问如何在while循环PHP中拆分返回的结果
EN

Stack Overflow用户
提问于 2014-05-21 14:20:40
回答 2查看 329关注 0票数 0

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

结果应该是

代码语言:javascript
复制
<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>

这就是我迄今所做的

代码语言:javascript
复制
$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>

    ";

    }
}
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2014-05-21 14:31:04

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

代码语言:javascript
复制
$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;

        }
    }
}

这将迫使1vs2,3vs4,5vs6从您的表中进行排序,因为您是按ID排序的。如果您想指定一个不同的配对,还有其他几种方法可以做到。

票数 1
EN

Stack Overflow用户

发布于 2014-05-21 14:27:57

将结果添加到两个不同的数组中。

在while循环之后,您可以执行两个预见(http://www.php.net/manual/en/control-structures.foreach.php)操作,并以这种方式拆分数据。

例如:$select_comp = mysql_query("SELECT * FROM *FROM=‘1’ORDER BY id DESC");if (mysql_num_rows($select_comp) >= 2) {

代码语言:javascript
复制
    $rows = array();
    while ($row=mysql_fetch_array($select_comp)) {
        $rows[] = $row;
    }

    // Present the data as you wish here
    // The first row will be $rows[0] and the seconds $rows[1]
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/23785983

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档