czukoman20 Posted December 11, 2007 Share Posted December 11, 2007 I've read a little bit on pagination, and i'm wondering deeply. Where would be the best place to find a good quick tutorial on pagination in php that uses a mysql database. Or where would be a good place to get a script already made. I've looked on google and gotten nowhere. Thanks Quote Link to comment Share on other sites More sharing options...
adam291086 Posted December 11, 2007 Share Posted December 11, 2007 There is a pagination tutorial up the tuttorial section on this site. Try that. I got it working easily and i am still learning. Quote Link to comment Share on other sites More sharing options...
phpQuestioner Posted December 11, 2007 Share Posted December 11, 2007 basic example: http://www.phpfreaks.com/tutorials/43/0.php Quote Link to comment Share on other sites More sharing options...
czukoman20 Posted December 11, 2007 Author Share Posted December 11, 2007 Ok thanks. I never thought of looking here damn lol. sorry. pretty much a wasteful topic Quote Link to comment Share on other sites More sharing options...
czukoman20 Posted December 12, 2007 Author Share Posted December 12, 2007 Ok i did the tutorial.. and im confused... i did everything that it said, with my own little add ons. but i have a serious issue. here is the link to what i havehttp://www.adworld-online.com/browse/ad2%20ptototype.php and here is the source code // The first function connects to your database, the second selects a database @mysql_connect("-----", "-----", "-----") or die("ERROR--CAN'T CONNECT TO SERVER"); @mysql_select_db("-----") or die("ERROR--CAN'T CONNECT TO DB"); /* Tip: The @ in front of these two functions will hide the username and password if there is an error. The die() function will kill the script and show an error statement if something goes wrong with the connect or select functions. A mysql_connect() error usually means your username/password are wrong, and a mysql_select_db() error usually means the database doesn't exist. */ $limit = 1; // Sets how many results shown per page $query_count = ("SELECT * FROM users WHERE userid_2 > '0' ORDER BY rand()"); $result_count = mysql_query($query_count); $totalrows = mysql_num_rows($result_count); if(empty($page)){ // Checks if the $page variable is empty (not set) $page = 1; // If it is empty, we're on page 1 } $limitvalue = $page * $limit - ($limit); // Ex: (2 * 25) - 25 = 25 <- data starts at 25 $query = ("SELECT * FROM users WHERE userid_2 > 0 LIMIT $limitvalue, $limit "); $result = mysql_query($query) or die("Error: " . mysql_error()); // Selects all the data from table. // mysql_error() will print an error if one occurs. /* Tip: The MySQL LIMIT value syntax is as follows: LIMIT $row_to_start_at, $how_many_rows_to_return */ if(mysql_num_rows($result) == 0){ echo("Nothing to Display!"); } // This reads the number of rows returned // from the result above. /* Tip: You could probably use if($totalrows == 0) for the if statement; however, reading the actual $result from the data you'll be printing to the screen is more accurate, and is a surefire way of preventing certain errors. */ $bgcolor = "#E0E0E0"; // light gray /* Sets up one of the background colors. The color stated here will be displayed second */ echo("<table>"); // Just a simple table while($row = mysql_fetch_array($result)){ /* This starts the loop (a while loop). What this does is returns a row of data to the $row array. Each time the loop restarts, the next row of data is used. So, each pass of the loop returns a different row of data. */ // Start The Background Check if($bgcolor == "#E0E0E0"){ $bgcolor = "#FFFFFF"; }else{ $bgcolor = "#E0E0E0"; } /* Tip: The color you want to appear first on the list should be where the #FFFFFF is listed. What this script does is checks to see if #E0E0E0 was used last; if it was, then it'll use #FFFFFF. All you have to do is replace the colors of your choice. */ echo("<tr bgcolor=".$bgcolor.">n<td>"); // Here we start table row & table data // Make sure your bgcolor is the $bgcolor variable echo($row["companyname"]); /* Tip: This is how we add the users field from our row. You can use any field from your row: all you do is include the field's name inbetween the array brackets. Ex: $row["field_name_here"] */ echo("</td>n<td>"); // Here we end the one section of table data, and start another. echo($row["username"]); // Prints the usersID field echo("</td>n</tr>"); // Here we end the table data & table row } /* This closes the loop. Anything after this bracket will display after the data you've pulled from the database. */ echo("</table>"); /* While we're at it, let's close the table tag--we don't want other data inside this table */ if($page != 1){ $pageprev = $page--; // Fancy way of subtracting 1 from $page echo("<a href=\"$PHP_SELF?page=$pageprev\">PREV"."</a> "); /* Tip: It is a good idea NOT to use $PHP_SELF in this link. It may work, but to be 99.9% sure that it will, be sure to use the actual name of the file this script will be running on. Also, the adds a space to the end of PREV, and gives some room between the numbers. */ }else echo("PREV"." "); // If we're on page 1, PREV is not a link $numofpages = $totalrows / $limit; /* We divide our total amount of rows (for example 102) by the limit (25). This will yield 4.08, which we can round down to 4. In the next few lines, we'll create 4 pages, and then check to see if we have extra rows remaining for a 5th page. */ for($i = 1; $i <= $numofpages; $i++){ /* This for loop will add 1 to $i at the end of each pass until $i is greater than $numofpages (4.08). */ if($i == $page){ echo($i." "); }else{ echo("<a href=\"$PHP_SELF?page=$i\">$i</a> "); } /* This if statement will not make the current page number available in link form. It will, however, make all other pages available in link form. */ } // This ends the for loop if(($totalrows % $limit) != 0){ /* The above statement is the key to knowing if there are remainders, and it's all because of the %. In PHP, C++, and other languages, the % is known as a Modulus. It returns the remainder after dividing two numbers. If there is no remainder, it returns zero. In our example, it will return 0.8 */ if($i == $page){ echo($i." "); }else{ echo("<a href=\"$PHP_SELF?page=$i\">$i</a> "); } /* This is the exact statement that turns pages into link form that is used above */ } // Ends the if statement if(($totalrows - ($limit * $page)) > 0){ /* This statement checks to see if there are more rows remaining, meaning there are pages in front of the current one. */ $pagenext = $page++; // Fancy way of adding 1 to page echo("<a href=\"$PHP_SELF?page=$pagenext\">NEXT"."</a>"); /* Since there are pages remaining, this outputs NEXT in link form. */ }else{ echo("NEXT".$limit); /* If we're on the last page possible, NEXT will NOT be displayed in link form. */ } mysql_free_result($result); /* This line is not required, since MySQL will free the result after all scripts have finished executing; however, it's a nice little backup. */ ?> 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.