Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I am using PHP and MYSQL. here is my code

 <?php 

    $result = mysql_query("SELECT * FROM `item` order by `ID` limit 0, 5");
    $rows = mysql_num_rows ($result); 
    $item = 0 ; 

     while ($item < $rows){

        $title = mysql_result($result,$list,"name");
        $id = mysql_result($result,$list,"ID");
        echo "<div id="new"> $id . $title  </div> ";

     $item++;
    }
     ?>   

My question is that, is it possible if the items are more than 5 (as you can see I limited items), then create a new div id=new to display the rest items?

many thanks in advance

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
175 views
Welcome To Ask or Share your Answers For Others

1 Answer

Change the while() loop this way:

 echo '<div>'; // Starting DIV
 while ($item < $rows){
    $title = mysql_result($result,$list,"name");
    $id = mysql_result($result,$list,"ID");
    echo "<div id="new"> $id . $title  </div> ";
    $item++;
    echo ($item % 5 == 0) ? '</div><div>' : ""; // Close and Open a New DIV
}
echo '</div>'; // Ending DIV

Happy to Help! :)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...