开发者

php mysql_fetch_array every 25 items wrap a div

I want to mysql_fetch_array 100 items from mysql database. then every 25 items wrap a div. also, I make a total items result check.

My code as below, but it will add <div> to every item, no as my require. So how to make it easier? Thanks.

if (mysql_num_rows($result) != 0) {
    $total = mysql_num_rows($result);
    $num = 1;
    while ($row = mysql_fetch_array($result)) {
        if ($num === 1) {
            echo '<div>';
        }
        echo $row['title'] . '<br />';
        if ($total < 26) {
            echo '</div>';
        }
        else {
            if ($num === 26) {
                echo '<div>';
            }
            if ($total < 51) {
                echo '</div>';
            }
            else {
                if ($num === 51) {
                    echo '<div>';
                }
                if ($total < 76) {
                    echo '</div>';
                }
                else {
                    if ($num === 75) {
                        echo '<div>';
                    }
            开发者_如何学编程        if ($total < 101) {
                        echo '</div>';
                    }
                }
            }
        }
        $num++;
    }
}


You can use the mod operator.

See: http://php.net/manual/en/internals2.opcodes.mod.php

echo '<div>';
while($row = mysql_fetch_array($result)){
  ....
  if (($num % 25) === 1) { echo '</div><div>' }
  $num++;
}
echo '</div>';


Try This...

if(mysql_num_rows($result)!=0){
    $total = mysql_num_rows($result);
    $num=1;

    while($row = mysql_fetch_array($result)){
        if($num===1)
        echo '<div>';

        echo $row['title'].'<br />';

        if($num==25){
         echo '</div>';
         $num=1;
        }

        $num++;
    }
}


use the mod operator.

$num =1; while($row = mysql_fetch_array($result)){

  if (($num % 25) === 1) { echo '<div>' }


    -------here data ----

  if (($num % 25) === 0) { echo '</div>' }
  $num++;

}


My advice is to separate your concerns... Give it a shot more like this:

// first get all your data.
$results = array();
while($row = mysql_fetch_array($result)){
    $results[] = $row;
}

// now you have an array of all of your records.

if (!empty($results)) {

    // total count of the array up front.
    $total = count($results);

    $interval = 0;

    // save the data in a buffer for echoing later.
    $buffer = array('<div>');

    foreach($results as $row) {
        $buffer[] = $row['title'] . '<br />';
        if (++$interval === 24) { // notice the prefix ++
            $interval = 0;

            // close and re-open divs
            $buffer[] = '</div><div>';
        }
    }
    // one final close tag
    $buffer[] = '</div>';

    // now we zip it up and echo the content.
    echo implode('', $buffer);
}


Here is how I do it...

$count = 0;
    while($row = mysql_fetch_array($result)){

    if ($count%$25 == 0)
            {
                echo "<div>";   
            }

       //whatever your data goes here

    count++

    if ($count%$25 == 0)
                {
                    echo "</div>";  
                }
    }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜