Nicholas Pandie
Nicholas Pandie

Reputation: 1

Show 15 results only

I'm having trouble with my members list. It shows EVERY username but i would like it to show only 15 per row.

The code:

    <table width="100%" border="0" cellspacing="0" cellpadding="0">
  <tr>
    <td width="23%"><em><strong>Username</strong></em></td>
  </tr>
  <?
  $sql = mysql_query("select * from usr_users");
  while($m = mysql_fetch_array($sql))
  { ?>
  <tr>
<td width="23%"><div style="float: left;" onMouseover="ddrivetip('<em><? echo("$m[username]");?></em> <br /> <b>Rank:</b> <? echo("$m[rankerslevel]");?><br /> <b>Bits:</b> <? echo("$m[credits]");?><br /> <b>Score:</b> <? echo("$m[points]");?><br /> <b>Mood:</b> <? echo("$m[usrstatus]");?><br /> <b>ID:</b> <? echo("$m[id]");?><br /> <b>Sex:</b> <? echo("$m[sexmalefemale]");?><br /> <b>Country:</b> <? echo("$m[countrywhere]");?><br />','white', 100)";
     onMouseout="hideddrivetip()"><img src="/bolt.png" alt="member_icon"/> <a href="memb.php?user=<? echo("$m[username]");?>"><font color="<? echo("$m[usercolour]");?>"><? echo("$m[username]");?></font></a></td></div>
    <? } ?>

Thanks in advance!

Upvotes: 0

Views: 134

Answers (4)

Alex
Alex

Reputation: 1398

Supposing you have an 'id' column - and it is set as Auto_Increment (as-well as an INT)

SELECT * FROM usr_users ORDER BY -id LIMIT 15

This will grab the 15 most latest users, and show them in order.

Upvotes: 0

John Woo
John Woo

Reputation: 263703

the simpliest soultion would be:

SELECT * FROM usr_users LIMIT 15

Upvotes: 1

Daniil Ryzhkov
Daniil Ryzhkov

Reputation: 7596

Use LIMIT clause. Here is usage:

LIMIT [offset,] rows

Examples:

SELECT * from usr_users LIMIT 0, 10

This query will retrieve 1-10 rows (from 0 to 10).

SELECT * from usr_users LIMIT 10, 10

This query will retrieve 11-19 rows.

If you want to get row with specific ids use IN statement:

SELECT * from usr_users WHERE id IN (1,2,3)

Also read this: http://dev.mysql.com/doc/refman/5.0/en/select.html

If you want to learn how to make pagination, look at this topic: http://www.codediesel.com/php/simple-pagination-in-php/

Upvotes: 1

Shyju
Shyju

Reputation: 218722

use mysql limit

select * from usr_users order by createddate desc LIMIT 0, 15

assuming you have a column called createddate on which you are doing an order by.

This will get you the last 15 users created.

You can get the total number of records and divide it by the "items to be displayed " to get the number of paging elements.

http://www.dharshin.com/2007/09/paging-results-with-php-and-mysql-part.html

Upvotes: 0

Related Questions