I uploaded my blog using PHP and MYSQL. The link to it is http://saadi.000a.biz It's in its very raw form so I still have to work a lot on it. I require some help regarding pagination. I want to paginate my blog with 3 posts per page. The source code of my blog is as follow. <?php include("mysql_connect.php"); // File establishing connection with database mysql_select_db("db_name", $con_mysql_db); // Selecting Database $result = mysql_query("SELECT post_title, post_body From posts ORDER BY post_title DESC"); while($row = mysql_fetch_array($result)) { echo("<h3>" .$row[0] ."</h3><hr style='color: brown'><br/> .$row[1]" ."<hr style='width: 80%; align: center; color: brown'><br/><br/><br/>"); } ?> I hope everything is clear. So please tell me about the PHP code for pagination so that I do not have to amend this code greatly. I tried a code on it but it is not working. The code I used is as follow: mysql_connect("database address", "username", "password") or die(mysql_error()); mysql_select_db("databse name") or die(mysql_error()); if (!(isset($pagenum))) { $pagenum = 1; } $data = mysql_query("SELECT post_title, post_body From posts ORDER BY post_title DESC") or die(mysql_error()); $rows = mysql_num_rows($data); $page_rows = 4; $last = ceil($rows/$page_rows); if ($pagenum < 1) { $pagenum = 1; } elseif ($pagenum > $last) { $pagenum = $last; } $max = 'limit' .($pagenum -1) * $page_rows .',' .$page_rows; ?> When I run this code, A blank page is appearing, which definitely means an error in the code. So please tell me what's the point that I am missing!!!???
$row is usually contains associative arrays - the name are the name of columns. So try this: <?php include("mysql_connect.php"); // File establishing connection with database mysql_select_db("db_name", $con_mysql_db); // Selecting Database $result = mysql_query("SELECT post_title, post_body From posts ORDER BY post_title DESC"); while($row = mysql_fetch_array($result)) { $title = $row['post_title']; $body = $row['post_body']; echo("<h3>$title</h3><hr style='color: brown'><br/>$body<hr style='width: 80%; align: center; color: brown'><br/><br/><br/>"); } ?> PHP:
Yeah that's cool. But it still does not serve my purpose. I'm looking for pagination code. Anyway thanks for this code as well.
Is that all the PHP code you have? It doesn't make any sense, here's my own version, I kind of rushed doing it but you should get the general idea of if you read it over. <?php $link = mysql_connect("database address", "username", "password") or die(mysql_error()); mysqli_set_charset($link, "utf8") or die(mysql_error()); mysql_select_db("db_name", $link) or die(mysql_error()); // Selecting Database ///////// function CalculateOffset ($pgNum, $rowsPerPage) { $tempOffset = $pgNum * $rowsPerPage; return ($tempOffset > $rowsPerPage) ? ceil($tempOffset-$rowsPerPage) : 0; } $pagenum = isset($_GET['p']) ? $_GET['p'] : 1; $data = mysql_query("SELECT * From posts") or die(mysql_error()); $rows = mysql_num_rows($data); $page_rows = 4; // rows per page $offset = CalculateOffset ($pagenum, $page_rows); $data = mysql_query("SELECT * From posts ORDER BY post_title DESC LIMIT $offset, $page_rows") or die(mysql_error()); ////////////// while($row = mysql_fetch_array($result)) { $title = $row['post_title']; $body = $row['post_body']; echo("<h3>$title</h3><hr style='color: brown'><br/>$body<hr style='width: 80%; align: center; color: brown'><br/><br/><br/>"); } mysqli_close($link); ?> PHP: Visit blablabla.com/thatscript.php?p=2 .. etc
Although this may not answer the OP's initial question, I'd like to further add to Rainulf's code, and remind to intval($_GET['p']) or (int) $_GET['p'] as an attempt to ensure its numeric and to prevent potential sql injection.