Reputation: 6189
How can I use PHP to show five rows from a MySQL database, then create a new line and show another five, etc?
Upvotes: 0
Views: 2318
Reputation: 6177
Something like this may be helpful:
$result = mysql_query($query);
if($result) {
while($row = mysql_fetch_assoc($result)) {
if(++$i%5==0 && $i>0) {
/* do the extra thing... new line some styles */
}
}
}
Upvotes: 1
Reputation: 1189
$total = 20;//get total number here;
$limit = 5;
for($i = 0;$i< $total/$limit;$i++)
{
$sql = $result = $rows = "";
$start = $limit * $i;
$sql = "select * from m_table order by id desc limit $start,$limit";
$result = mysql_query($query);
while($rows = mysql_fetch_assoc($result))
{
//print the result here;
}
}
You can fetch 5 rows every time from mysql without fetching all the rows at once.
Upvotes: 0
Reputation: 268344
Use the LIMIT clause if you want to limit the amount of results returned from the query.
If you want to print an <hr/>
after every fifth record you can check it via the modulus operator:
$counter = 1;
while ($row = mysql_fetch_assoc($rst)) {
// print $row stuff
if ($counter % 5 == 0)
print "<hr />";
$counter++;
}
Basically, we have a variable used to count how many records we've printed. Once that counter can be divided by five, and leave no remainder, we print our horizontal-rule.
Upvotes: 3
Reputation: 21565
Err.. you mean something like:
SELECT * FROM `tablename` WHERE ... LIMIT 5
Upvotes: 0