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

时间:2014-05-21 14:20:41

标签: php mysql arrays

我正在开发一个应用程序,我需要返回两行结果并在其中间创建一个标记以显示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>

    ";

    }
}

2 个答案:

答案 0 :(得分:1)

如果你想要完成的是循环结果并显示结果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;

        }
    }
}

由于您按ID排序,因此会从您的表中强制执行1对2,3对4,5对6配对。如果您想指定不同的配对,可以采用其他几种方法。

答案 1 :(得分:0)

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

在while循环之后,你可以做两个foreaches(http://www.php.net/manual/en/control-structures.foreach.php)并以这种方式分割你的数据。

一个例子: $ select_comp = mysql_query(“SELECT * FROM nominate WHERE accepted ='1'ORDER BY id DESC”);     if(mysql_num_rows($ select_comp)&gt; = 2){

    $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]
}