Reply
Show database info with pages
Old 08-06-2005, 07:08 PM Show database info with pages
Junior Talker

Posts: 3
Location: Leicester, UK
Ok, underneath is the page im using to display my members in a table but it displays everyone of my members and with this number increasing i need to find a way to display a set number of results per page then a navigation like this:
<Previous 2 3 4 Next>

can anyone help me with this?

PHP Code:
<?

include("db.php");

?>
<table width=100% style="border: 1px solid #9E9E9E ">
<tr><td class="mtext">Username</td><td class="mtext">Member No.</td><td class="mtext">Userlevel</td><td class="mtext">Profile</td><td class="mtext">PM</td></tr>
<?
$r 
mysql_query("SELECT * FROM user ORDER BY `id` ASC");

while (
$re mysql_fetch_array($r)) {
$user $re['user'];
$name $re['name'];
$loc $re['location'];
$lev $re['userlevel'];
if (
$lev == 9) {
$lev "Admin";
}
else if (
$lev == 7) {
$lev "Moderator";
}
else {
$lev "User";
}
$id $re['id'];
echo 
"<tr><td class=\"mans\">".$user."</td>";
echo 
"<td class=\"mans\">".$id."</td>";
echo 
"<td class=\"mans\">".$lev."</td>";
echo 
"<td class=\"mans\"><a href=\"sprofile.php?id=$id\">Profile</a></td>";
echo 
"<td class=\"mans2\"><a href=\"swrite.php?whoto=$user\">Send PM</a></td></tr>";
}
?>
</table>

Last edited by jstarwind : 08-06-2005 at 08:47 PM.
jstarwind is offline
Reply With Quote
View Public Profile Visit jstarwind's homepage!
 
When You Register, These Ads Go Away!
     
Old 08-06-2005, 10:04 PM Re: SQL: LIMIT (INT)
Experienced Talker

Posts: 31
Location: Simi Valley, CA
Try adding to your SQL query: LIMIT X (Where "X" is the number of records you want to return from the database)

Here is a sample of code from an application I've built to return only the first record:

PHP Code:
$query "SELECT * FROM events WHERE Active = 1 ORDER BY Date ASC LIMIT 1"
-Andrew
__________________
Andrew
http://www.insitecs.com/
ademaskoo is offline
Reply With Quote
View Public Profile Visit ademaskoo's homepage!
 
Old 08-06-2005, 11:33 PM Disregard
Experienced Talker

Posts: 31
Location: Simi Valley, CA
Disregard the above, please.

Here's the code you can use: (I wrote it all out for you, yes today has been very boring and I needed something to do )

If you want to expand on what I've done, you can make it run alot faster by figuring out how to specify a range in SQL. I'm not sure how this is done, and a quick search didn't do me any good either. I'm assuming you probably won't have thousands of records in your database table, so dumping it all out to an array is fine.

PHP Code:
<?

include("db.php");

?>
<table width=100% style="border: 1px solid #9E9E9E ">
<tr><td class="mtext">Username</td><td class="mtext">Member No.</td><td class="mtext">Userlevel</td><td class="mtext">Profile</td><td class="mtext">PM</td></tr>
<?
$r 
mysql_query("SELECT * FROM user ORDER BY `id` ASC");


// page record numbers start at 1, not 0
if(isset($_GET['begin'])) {
    
$page_records_begin_at $_GET['begin'];
    if(
$page_records_begin_at == 0)
        
$page_records_begin_at 1;
}
else
    
$page_records_begin_at 1;

// users listed per page
$USERS_PER_PAGE 10;

// find the number of users in the database
$num_rows_user mysql_num_rows($r); 

// find how many pages it will take to display these users
$num_pages_user $num_rows_user/$USERS_PER_PAGE;

// convert database resource data into array data (2D)
while($arr mysql_fetch_array($r)) {
    
$re[]=$arr;
}



for(
$x=0$x<count($re); $x++) {
$user $re[$x]['user'];
$name $re[$x]['name'];
$loc $re[$x]['location'];
$lev $re[$x]['userlevel'];
if (
$lev == 9) {
$lev "Admin";
}
else if (
$lev == 7) {
$lev "Moderator";
}
else {
$lev "User";
}
$id $re[$x]['id'];
echo 
"<tr><td class=\"mans\">".$user."</td>";
echo 
"<td class=\"mans\">".$id."</td>";
echo 
"<td class=\"mans\">".$lev."</td>";
echo 
"<td class=\"mans\"><a href=\"sprofile.php?id=$id\">Profile</a></td>";
echo 
"<td class=\"mans2\"><a href=\"swrite.php?whoto=$user\">Send PM</a></td></tr>";


}
?>
</table>

<?php 

// sculpts <Previous 2 3 4 Next>
$str "<p>";
for(
$x 1$x-1<=$num_pages_user$x++) {
    
$str .= "&lt;";
    if(
$x == && $page_records_begin_at != 1)
        
$str .= "<a href=\"?begin=".($x-1)*$USERS_PER_PAGE."\">Previous</a> ";
    else if(
$x == $num_pages_user && $page_records_begin_at+$USERS_PER_PAGE $num_rows_user) {
        
$str .= "<a href=\"?begin=".($x+1)*$USERS_PER_PAGE."\">Next</a>";
    else
        
$str .= "<a href=\"?begin=".$x*$USERS_PER_PAGE."\">".$x." ";
    
$str .= "&gt;";
}
$str .="</p>";
echo 
$str;
?>
__________________
Andrew
http://www.insitecs.com/

Last edited by ademaskoo : 08-08-2005 at 03:09 PM. Reason: Typo in code
ademaskoo is offline
Reply With Quote
View Public Profile Visit ademaskoo's homepage!
 
Old 08-07-2005, 03:58 AM
OmuCuSucu's Avatar
Vi Veri Veniversum Vivus

Posts: 1,167
Name: Dragos-Valentin
Location: Cluj-Napoca, RO
Code:
"SELECT * FROM user ORDER BY id DESC LIMIT " . $p . " , " . $nr
$p is the number of the page.
$nr is the number of members displayed.

with every page you increment $p and multiply it by $nr.
the paging links can be obtained by using:

Code:
"SELECT COUNT(*) FROM user"
you take the result and divide it by $nr (make sure you round the result to a full integer, 3,1 would be 4).

hope you understood and that it helps you.
__________________
.
» Please remember to add to my Talkupation if you enjoyed my post. Thank you :)
.
OmuCuSucu is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to Show database info with pages
 

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off




   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML

 


Page generated in 0.14889 seconds with 13 queries