I am developing an application where I need to return result of two rows and create a tag in middle of them to show a VS icon (this is competition). At some stage I need to split results correct? How would that be achieved,
Result should be like
<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>
this is what I've done so far
$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 what you want to accomplish is to loop through results and show Result 1 VS Result 2 then move on to the next pairing, you could add in a counter in your loop to figure out where you're at in the pattern.
$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;
}
}
}
This would force a 1 vs 2, 3 vs 4, 5 vs 6 pairing from your table since you're sorting by ID. If you wanted to specify a different pairing there are a few other ways you could do it.
Add your results to two different arrays.
After the while loop you can do two foreaches ( http://www.php.net/manual/en/control-structures.foreach.php ) and split your data that way.
An example: $select_comp = mysql_query("SELECT * FROM nominate WHERE accepted='1' ORDER BY id DESC"); if (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]
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.