// Database connection info $ Conn = mysql_connect ('localhost', 'dbusername', 'dbpass') or trigger_error ("SQL", E_USER_ERROR ); $ Db = mysql_select_db ('dbname', $ conn) or trigger_error ("SQL", E_USER_ERROR ); // Find out how many rows are in the table $ SQL = "SELECT COUNT (*) FROM numbers "; $ Result = mysql_query ($ SQL, $ conn) or trigger_error ("SQL", E_USER_ERROR ); $ R = mysql_fetch_row ($ result ); $ Numrows = $ r [0]; // Number of rows to show per page $ Rowsperpage = 10; // Find out total pages $ Totalpages = ceil ($ numrows/$ rowsperpage ); // Get the current page or set a default If (isset ($ _ GET ['currentpage']) & is_numeric ($ _ GET ['currentpage']) { // Cast var as int $ Currentpage = (int) $ _ GET ['currentpage']; } Else { // Default page num $ Currentpage = 1; } // End if // If current page is greater than total pages... If ($ currentpage> $ totalpages ){ // Set current page to last page $ Currentpage = $ totalpages; } // End if // If current page is less than first page... If ($ currentpage <1 ){ // Set current page to first page $ Currentpage = 1; } // End if // The offset of the list, based on current page $ Offset = ($ currentpage-1) * $ rowsperpage; // Get the info from the db $ SQL = "SELECT id, number FROM numbers LIMIT $ offset, $ rowsperpage "; $ Result = mysql_query ($ SQL, $ conn) or trigger_error ("SQL", E_USER_ERROR ); // While there are rows to be fetched... While ($ list = mysql_fetch_assoc ($ result )){ // Echo data Echo $ list ['id']. ":". $ list ['Number']." "; } // End while /****** Build the pagination links ******/ // Range of num links to show $ Range = 3; // If not on page 1, don't show back links If ($ currentpage> 1 ){ // Show <link to go back to page 1 Echo "<"; // Get previous page num $ Prevpage = $ currentpage-1; // Show <link to go back to 1 page Echo "<"; } // End if // Loop to show links to range of pages around current page For ($ x = ($ currentpage-$ range); $ x <($ currentpage + $ range) + 1); $ x ++ ){ // If it's a valid page number... If ($ x> 0) & ($ x <= $ totalpages )){ // If we're on current page... If ($ x = $ currentpage ){ // 'Highlight' it but don't make a link Echo "[$ X] "; // If not current page... } Else { // Make it a link Echo "$ x "; } // End else } // End if } // End // If not on last page, show forward and last page links If ($ currentpage! = $ Totalpages ){ // Get next page $ Nextpage = $ currentpage + 1; // Echo forward link for next page Echo "> "; // Echo forward link for lastpage Echo "> "; } // End if /****** End build pagination links ******/ ?> |