interpim Posted March 15, 2007 Share Posted March 15, 2007 OK... I got my script working before, displaying everything... but I decided I wanted to paginate the results. Well, after all the work boo_lolly went through to help me getting the other script working I have broken it again LOL... well, here is my code... I am getting no output, and honestly, I am setting myself up to get embarrassed again i know. <?php include('config.php'); include('includes/functions.php'); loggedin_check(); $user=$_COOKIE['user']; if(!isset($_GET['page'])){ $page=1; }else{ $page=$_GET['page'] } $max_results=20; $from = (($page * $max_results) - $max_results); $sql = mysql_query("SELECT * FROM main WHERE level > 0 ORDER BY level LIMIT $from, $max_results"); echo "<center><table border='2' bgcolor=#aaaaaa><tr><td>ID</id><td>NAME</td><td>LEVEL</td><td>WINS</td><td>LOSSES</td><td> </td></tr>"; while($row = mysql_fetch_assoc($sql)){ if($row['user_name'] != $user){ echo '<tr><td>' . $row['id'] . '</td><td>' . $row['toon_name'] . '</td><td>' . $row['level'] . '</td><td>' . $row['win'] . '</td><td>' . $row['loss'] . '</td><td><a href="battle.php?atk=' . $row['id'] . '">ATTACK!</a></td></tr>'; } } echo "</table>"; $total_results = mysql_result(mysql_query("SELECT COUNT(*) as Num FROM main"),0); $total_pages = ceil($total_results / $max_results); echo "SELECT A PAGE<br />"; if($page > 1){ $prev =($page - 1); echo "<a href=\"" .$_SERVER['PHP_SELF']."?page=$prev\"><<PREVIOUS</a> "; } for($i=1;$i<=$total_pages;$i++){ if(($page)==$i){ echo "$i "; }else{ echo "<a href=\"".$_SERVER['PHP_SELF']."?page=$i\">$i</a> "; } } if($page < $total_pages){ $next = ($page + 1); echo "<a href=\"".$_SERVER['PHP_SELF']."?page=$next\">NEXT>></a>"; } echo "</center>"; ?> Quote Link to comment Share on other sites More sharing options...
interpim Posted March 15, 2007 Author Share Posted March 15, 2007 *bump Quote Link to comment Share on other sites More sharing options...
mmarif4u Posted March 15, 2007 Share Posted March 15, 2007 Try this for ur script. Its easy to adopt and working fine. http://www.php-mysql-tutorial.com/php-mysql-paging.php Quote Link to comment Share on other sites More sharing options...
interpim Posted March 15, 2007 Author Share Posted March 15, 2007 Tried that script... I am getting nothing from it here is what I have trying that tutorial script... <?php include 'config.php'; mysql_connect($server,$username,$password); mysql_select_db($database) or die("unable to select database because ".mysql_error()); // how many rows to show per page $rowsPerPage = 30; // by default we show first page $pageNum = 1; // if $_GET['page'] defined, use it as page number if(isset($_GET['page'])) { $pageNum = $_GET['page']; } // counting the offset $offset = ($pageNum - 1) * $rowsPerPage; $query = " SELECT FROM main WHERE level > 0 " . " LIMIT $offset, $rowsPerPage"; $result = mysql_query($query) or die('Error, query failed'); // print the random numbers $bgcolor = "#E0E0E0"; // light gray echo "<center><table border='2'><tr bgcolor='E0E0E0'><td>ID</id><td>NAME</td><td>LEVEL</td><td>WINS</td><td>LOSSES</td><td> </td></tr>"; while($row = mysql_fetch_array($result)) { if ($bgcolor == "#E0E0E0"){ $bgcolor = "#FFFFFF"; }else{ $bgcolor = "#E0E0E0"; } echo("<tr bgcolor=".$bgcolor.">n<td>"); echo($row["toon_name"]); echo("</td>n<td>"); echo($row["level"]); echo("</td>n</td>"); echo($row["win"]); echo("</td>n</td>"); echo($row["loss"]); echo("</td>n</td>"); echo("a href=\"battle.php?atk=$row['id']>ATTACK THIS PLAYER!</a></td></tr>"); } echo "</table>"; // how many rows we have in database $query = "SELECT COUNT(level) AS numrows FROM main"; $result = mysql_query($query) or die('Error, query failed'); $row = mysql_fetch_array($result, MYSQL_ASSOC); $numrows = $row['numrows']; // how many pages we have when using paging? $maxPage = ceil($numrows/$rowsPerPage); // print the link to access each page $self = $_SERVER['PHP_SELF']; $nav = ''; for($page = 1; $page <= $maxPage; $page++) { if ($page == $pageNum) { $nav .= " $page "; // no need to create a link to current page } else { $nav .= " <a href=\"$self?page=$page\">$page</a> "; } } // creating previous and next link // plus the link to go straight to // the first and last page if ($pageNum > 1) { $page = $pageNum - 1; $prev = " <a href=\"$self?page=$page\">[Prev]</a> "; $first = " <a href=\"$self?page=1\">[First Page]</a> "; } else { $prev = ' '; // we're on page one, don't print previous link $first = ' '; // nor the first page link } if ($pageNum < $maxPage) { $page = $pageNum + 1; $next = " <a href=\"$self?page=$page\">[Next]</a> "; $last = " <a href=\"$self?page=$maxPage\">[Last Page]</a> "; } else { $next = ' '; // we're on the last page, don't print next link $last = ' '; // nor the last page link } // print the navigation link echo $first . $prev . $nav . $next . $last; // and close the database connection mysql_close(); // ... and we're done! ?> Quote Link to comment Share on other sites More sharing options...
mmarif4u Posted March 15, 2007 Share Posted March 15, 2007 This is ur main page. <?php include 'config.php'; mysql_connect($server, $username, $password) or die("ERROR--CAN'T CONNECT TO SERVER"); mysql_select_db($database) or die("ERROR--CAN'T CONNECT TO DB"); $rowsPerPage = 20; $pageNum = 1; if(isset($_GET['page'])) { $pageNum = $_GET['page']; } $offset = ($pageNum - 1) * $rowsPerPage; $query = " SELECT FROM main WHERE level > 0 LIMIT $offset, $rowsPerPage" ; $result = mysql_query($query) or die('Error, query failed'); // print the random numbers while($row = mysql_fetch_array($result)) { if ($bgcolor == "#E0E0E0"){ $bgcolor = "#FFFFFF"; }else{ $bgcolor = "#E0E0E0"; } echo("<tr bgcolor=".$bgcolor.">n<td>"); echo($row["toon_name"]); echo("</td>n<td>"); echo($row["level"]); echo("</td>n</td>"); echo($row["win"]); echo("</td>n</td>"); echo($row["loss"]); echo("</td>n</td>"); echo("a href=\"battle.php?atk=$row['id']>ATTACK THIS PLAYER!</a></td></tr>"); } $query = "SELECT COUNT(level) AS numrows FROM main WHERE level > 0"; $result = mysql_query($query) or die('Error, query failed'); $row = mysql_fetch_array($result, MYSQL_ASSOC); $numrows = $row['numrows']; // how many pages we have when using paging? $maxPage = ceil($numrows/$rowsPerPage); // print the link to access each page $self = $_SERVER['PHP_SELF']; ?> <table align="center" border="0" cellpadding="0" cellspacing="0" width="750"> <br><tr> <td><hr noshade color="#6699FF" align="center" width="470" size="2"> </td> </tr> <tr align="center"> <td> <?php include ('paging.php'); ?> </td> </tr><tr> <td><hr noshade color="#6699FF" align="center" width="470" size="2"> </td> </tr> </table> Now save the following code as paging.php in the same directory. <?php // creating 'previous' and 'next' link // plus 'first page' and 'last page' link // print 'previous' link only if we're not // on page one if ($pageNum > 1) { $page = $pageNum - 1; $prev = " <a href=\"$self?page=$page\">[Prev]</a> "; $first = " <a href=\"$self?page=1\">[First Page]</a> "; } else { $prev = ' [Prev] '; // we're on page one, don't enable 'previous' link $first = ' [First Page] '; // nor 'first page' link } // print 'next' link only if we're not // on the last page if ($pageNum < $maxPage) { $page = $pageNum + 1; $next = " <a href=\"$self?page=$page\">[Next]</a> "; $last = " <a href=\"$self?page=$maxPage\">[Last Page]</a> "; } else { $next = ' [Next] '; // we're on the last page, don't enable 'next' link $last = ' [Last Page] '; // nor 'last page' link } // print the page navigation link //echo $first . $prev . $nav . $next . $last; echo $first . $prev . " Showing page <strong><font color='blue'>$pageNum</font></strong> of <strong><font color='blue'>$maxPage</font></strong> pages </strong></font>" . $next . $last; ?> Hope this will now wrok for u. Quote Link to comment Share on other sites More sharing options...
interpim Posted March 15, 2007 Author Share Posted March 15, 2007 Thank you for the help... with a little bit of tweaking I finally got it working Here is what I came up with in the end... <?php include('config.php'); mysql_connect($server,$username,$password); mysql_select_db($database) or die("unable to select database because ".mysql_error()); // how many rows to show per page $rowsPerPage = 30; // by default we show first page $pageNum = 1; // if $_GET['page'] defined, use it as page number if(isset($_GET['page'])) { $pageNum = $_GET['page']; } // counting the offset $offset = ($pageNum - 1) * $rowsPerPage; $query = " SELECT * FROM main WHERE level > 0 ORDER BY level LIMIT $offset, $rowsPerPage"; $result = mysql_query($query) or die('Error, first query failed'); // print the random numbers $bgcolor="#E0E0E0"; echo "<center><table border='2'><tr bgcolor='E0E0E0'><td>ID</id><td>NAME</td><td>LEVEL</td><td>WINS</td><td>LOSSES</td><td> </td></tr>"; while($row = mysql_fetch_assoc($result)) { if($row['user_name'] != $user){ if ($bgcolor == "#E0E0E0"){ $bgcolor = "#FFFFFF"; }else{ $bgcolor = "#E0E0E0"; } echo '<tr bgcolor='.$bgcolor.'><td>' . $row['id'] . '</td><td>' . $row['toon_name'] . '</td><td>' . $row['level'] . '</td><td>' . $row['win'] . '</td><td>' . $row['loss'] . '</td><td><a href="battle.php?atk=' . $row['id'] . '">ATTACK!</a></td></tr>'; } } echo "</table>"; // how many rows we have in database $query = "SELECT COUNT(level) AS numrows FROM main"; $result = mysql_query($query) or die('Error, second query failed'); $row = mysql_fetch_array($result, MYSQL_ASSOC); $numrows = $row['numrows']; // how many pages we have when using paging? $maxPage = ceil($numrows/$rowsPerPage); // print the link to access each page $self = $_SERVER['PHP_SELF']; $nav = ''; for($page = 1; $page <= $maxPage; $page++) { if ($page == $pageNum) { $nav .= " $page "; // no need to create a link to current page } else { $nav .= " <a href=\"$self?page=$page\">$page</a> "; } } // creating previous and next link // plus the link to go straight to // the first and last page if ($pageNum > 1) { $page = $pageNum - 1; $prev = " <a href=\"$self?page=$page\">[Prev]</a> "; $first = " <a href=\"$self?page=1\">[First Page]</a> "; } else { $prev = ' '; // we're on page one, don't print previous link $first = ' '; // nor the first page link } if ($pageNum < $maxPage) { $page = $pageNum + 1; $next = " <a href=\"$self?page=$page\">[Next]</a> "; $last = " <a href=\"$self?page=$maxPage\">[Last Page]</a> "; } else { $next = ' '; // we're on the last page, don't print next link $last = ' '; // nor the last page link } // print the navigation link echo $first . $prev . $nav . $next . $last; // and close the database connection mysql_close(); // ... and we're done! ?> Quote Link to comment Share on other sites More sharing options...
mmarif4u Posted March 15, 2007 Share Posted March 15, 2007 Glad to see that u sorted it out. Have a nice day. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.