拆分查询结果为2个div

时间:2014-01-21 16:30:39

标签: php html mysql

我有这个php mysql查询

<?php
  $product = mysql_query('SELECT * FROM products LIMIT 6 ');
  $pro  = mysql_fetch_assoc($product);
?>

现在该查询将从数据库中返回6个产品,我想要做的是回复<div>内的3个产品和另一个<div>内的其他3个产品

<div class="first-3>
///Here i want to echo 3 products from the query from 1-3
   <?php echo $pro['title']; ?>
</div>

<div class="second-3>
///Here i want to echo the rest 3 products of the query from 4-6
   <?php echo $pro['title']; ?>
</div>

6 个答案:

答案 0 :(得分:3)

迭代并输出值。

<?php
    $product = mysql_query('SELECT * FROM products LIMIT 6 ');
    $i = 1;

    echo '<div class="first-3">';

    while ( $pro  = mysql_fetch_assoc($product) ) {
        if ($i === 3) {
             echo '</div><div class="second-3">';
        }

        echo $pro['title'];
        $i++;
    }

    echo '</div>';

?>

请注意,使用mysql_query并不安全,您应该使用mysqli或最好使用PDO。

答案 1 :(得分:3)

<?php
    $num = 6;
    $product = mysql_query('SELECT * FROM products LIMIT $num');

    $firstDiv = "";
    $secondDiv = "";
    $i = 0;
    while ($pro  = mysql_fetch_assoc($product)) {
        if ($i < ($num /2)) {
            $firstDiv .= $pro['title'];
        }
        else {
            $secondDiv .= $pro['title'];
        }
        $i++;
    }
?>

<div class="first-3>
    <?php $firstDiv ?>
</div>

<div class="second-3>
    <?php $secondDiv ?>
</div>

答案 2 :(得分:2)

mysql_fetch_assoc用于检索结果集中的行。

Doc:http://php.net/manual/es/function.mysql-fetch-assoc.php

需要循环来迭代每一行。

一个非常简单的例子:

// Get a collection of 6 results
$products = mysql_query('SELECT * FROM products LIMIT 6 ');


// iterate over the 6 results
$i=0;
echo '<div class="first-3>';
while ($pro = mysql_fetch_assoc($products)) {
    $i++;

    // Print an item
    echo $pro["title"];

    // If 3 items are printed end first div and start second div
    if($i==3){
        echo '</div><div class="second-3">';
    }

}
echo '</div>';

// Free the collection resources
mysql_free_result($products);

答案 3 :(得分:2)

只需设置一个计数器即可分为3组:

$count = 0;

while (...)
{
  // your code

  $count++;
  if ( ($count % 3) === 0 )
  {
    echo '</div><div class="...">';
  }
}

请注意,mysql_*函数已弃用,您应该切换到PDO或mysqli。

答案 4 :(得分:1)

$product = mysqli_query($conn, 'SELECT * FROM products LIMIT 6 ');
$results = array();
while($pro = mysqli_fetch_assoc($product)) {
    $results[] = $pro;
}
echo '<div class="first-3">';
for($i = 0; $i < 3; $i++) {
    echo $results[$i]['title'];
}
echo '</div><div class="second-3">';
for($i = 3; $i < 6; $i++) {
    echo $results[$i]['title'];
}
echo '</div>';

答案 5 :(得分:0)

每当你得到3的倍数,($ k%3 == 0)你将增加flag变量,你可以用变量flag做一些条件,我在这里使用迭代器因为hasNext()美。

Example

$pro = array(1, 2, 3, 4, 5, 6);
$flag = 0;
$pro = new CachingIterator(new ArrayIterator($pro));

foreach ($pro as $k => $v) {

    // if multiples 3
    if ($k % 3 == 0) {
        $flag++;

        if ($flag == 1) {
            echo '<div class="first-3" style="border:1px solid black;margin-bottom:10px;">';
        } else if ($flag == 2) {
            echo '</div>'; // Closes the first div
            echo '<div class="second-3" style="border:1px solid red">';
        }else{ // if you have more than 6
                echo '</div>';
        }
    }

    // insert Data
    echo $v . '<br/>';

    if (!$pro->hasNext())
        echo '</div>'; // if there is no more closes the div
}