Alberto Falso
Alberto Falso

Reputation: 103

How to Limit the lines my Php List is showing

I have a submit form that displays into a list format and I'm wondering what I can do to make it so that the list only displays a certain number of the most current submitted info. I'm testing it and currently the list is on a page and just displays 50+ submissions stretching out the page very long.

    <?php
$query='select * from article order by `article`.`time` DESC';
$result=mysql_query($query); 
echo '<table width="600px">';
while($row = mysql_fetch_array($result))
{
    echo "<td><a href='".$row['url']."'>".$row['title']."</a></td> <td>".$row['description']."</td><td>".$row['type']."</td></tr>";
}
echo '<table>';
 ?>

Upvotes: 3

Views: 89

Answers (2)

Zar
Zar

Reputation: 6882

Even though you only should select the data you need, you might want to take a look at a for-loop, which is useful if you know how many times you want to run something. You might end up with a loop which looks like this:

for($i = 0; $i < 10 && $row = mysql_fetch_array($result); $i++) {
   echo "<td><a href='".$row['url']."'>".$row['title']."</a></td> <td>".$row['description']."</td><td>".$row['type']."</td></tr>";
}

This code runs 10 times IF you have enough data.

Upvotes: 1

Dennis Hackethal
Dennis Hackethal

Reputation: 14275

Welcome to SO! Modify your sql statement as follows:

$query='SELECT * FROM article ORDER BY `article`.`time` DESC LIMIT 10';

Change 10 to however many entries should be displayed.

Upvotes: 6

Related Questions