簡體   English   中英

如何在while循環PHP中拆分返回的結果

[英]How can I split returned results in while loop PHP

我正在開發一個應用程序,我需要返回兩行的結果並在其中中間創建一個標記以顯示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與結果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配對。 如果要指定其他配對,則可以使用其他幾種方法。

將結果添加到兩個不同的數組中。

在while循環之后,您可以執行兩個foreach( http://www.php.net/manual/en/control-structures.foreach.php )並以這種方式拆分數據。

一個例子:$ select_comp = mysql_query(“ SELECT * FROM提名在哪里接受='1'ORDER BY ID DESC”); 如果(mysql_num_rows($ select_comp)> = 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]
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM