PHP - Having Problems With Pagination Code
Hey, I got this script off phpfreaks in the post "Basic Pagination Code", and got it to work except I'm experiencing this really tiny problem. The script displays only the first row in my table, and displays 127 times, which is how many rows I have total in the table.
This is where I have this script running at. You can go there to see what I'm talking about: http://www.djsmiley.net/guestbook.php This is the code I got and tweaked: Code: [Select] <?php //your username $username = "your username"; //your password $password = "your password"; //your mySQL server $host = "your hostname"; //The name of the database your table is in $database = "your database"; //connect, but if there is a problem, display an error message telling why there is a problem $conn = mysql_connect($host,$username,$password) or die("Error connecting to Database!<br>" . mysql_error()); //Choose the database from your mySQL server, but if there is a problem, display an error telling why $db = mysql_select_db($database) or die("Cannot select database!<br>" . mysql_error()); // find out how many rows are in the table $sql = "SELECT COUNT(*) FROM comments"; $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 * FROM comments ORDER BY commentid DESC 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 if ($name == 'DJ Smiley' && $adminkey == 'adminkey') { echo(" <div style=\"border-radius: 10px; border: solid 2px #E5E5E5; padding: 10px; width: 95%; background: #F6F6F6;\"> <p> <a name=\"$commentid\" id=\"$commentid\"></a><img src=\"http://www.djsmiley.net/images/Icons/Arrows/Right 2.png\" class=\"fltlft2\" height=\"25\" width=\"25\"><strong>$name</strong> (<a href=\"http://whois.domaintools.com/$ip\"><font size=1>$ip</font></a>) says: <br><p class=\"BottomBorder\"></p> <table><tr><td><img src=\"http://www.djsmiley.net/images/Self/Self (2).jpg\" height=\"50\" width=\"50\"></td><td>$comment</td></tr></table> <br><p class=\"BottomBorder\"></p> added on $time<p><em>$email</em></p> </p> </div><br> "); } elseif ($name == false) { echo(" <div style=\"border-radius: 10px; border: solid 2px #E5E5E5; padding: 10px; width: 95%; background: #F6F6F6;\"> <p> <a name=\"$commentid\" id=\"$commentid\"></a><img src=\"http://www.djsmiley.net/images/Icons/Arrows/Right 2.png\" class=\"fltlft2\" height=\"25\" width=\"25\"><strong>Anonymous</strong> (<a href=\"http://whois.domaintools.com/$ip\"><font size=1>$ip</font></a>) says: <br><p class=\"BottomBorder\"></p> <table><tr><td><img src=\"http://www.djsmiley.net/images/Icons/People/Anonymous 2.png\" height=\"50\" width=\"50\"></td><td>$comment</td></tr></table> <br><p class=\"BottomBorder\"></p>added on $time<p><em>$email</em></p> </p> </div><br> "); } else { echo(" <div style=\"border-radius: 10px; border: solid 2px #E5E5E5; padding: 10px; width: 95%; background: #F6F6F6;\"> <p> <a name=\"$commentid\" id=\"$commentid\"></a><img src=\"http://www.djsmiley.net/images/Icons/Arrows/Right 2.png\" class=\"fltlft2\" height=\"25\" width=\"25\"><strong>$name</strong> (<a href=\"http://whois.domaintools.com/$ip\"><font size=1>$ip</font></a>) says: <br><p class=\"BottomBorder\"></p> <table><tr><td><img src=\"http://www.djsmiley.net/images/Icons/People/Anonymous 2.png\" height=\"50\" width=\"50\"></td><td>$comment</td></tr></table> <br><p class=\"BottomBorder\"></p> added on $time<p><em>$email</em></p> </p> </div><br> "); } } // 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 " <a href='{$_SERVER['PHP_SELF']}?currentpage=1'><<</a> "; // get previous page num $prevpage = $currentpage - 1; // show < link to go back to 1 page echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$prevpage'><</a> "; } // 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 " [<b>$x</b>] "; // if not current page... } else { // make it a link echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$x'>$x</a> "; } // end else } // end if } // end for // 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 " <a href='{$_SERVER['PHP_SELF']}?currentpage=$nextpage'>></a> "; // echo forward link for lastpage echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$totalpages'>>></a> "; } // end if /****** end build pagination links ******/ ?> Similar TutorialsHey PHP Freaks, First of all I want to express my sincerest gratitude to whoever helps me solve this problem. I'm new to PHP, I started about 2 weeks ago. This is for a products page I was writing. The client updates his files which populates the SQL Database, then the PHP writes all the icons out. I thought pagination would make it look a little cleaner. But I didn't think it would be this much work. The conditional statement at the top is for when the user wants to sort the products by category (It's for a gun shop, so its categorized my centerfire, rimfire, ect...), if not it just posts everything. The error I get is... Warning: mysql_fetch_array() expects parameter 1 to be resource, string given in C:\wamp\www\Guns N' Gadgets Site\Products.php on line 151 Call Stack # Time Memory Function Location 1 0.0009 698840 {main}( ) ..\Products.php:0 2 0.0060 713416 mysql_fetch_array ( ) ..\Products.php:151 Line 151 is the while loop. Here is the code. <?php //error_reporting(0); require_once("includes/connect.php"); if(isset($_GET['category'])) { $cat = $_GET['category']; $qstuff = "SELECT guns_id,guns_brand,guns_model,guns_img,guns_price FROM tbl_guns WHERE guns_cat = '".$cat."' ORDER BY guns_id DESC"; $Fixit = mysql_query($qstuff); }else{ $qstuff = "SELECT guns_id,guns_brand,guns_model,guns_img,guns_price FROM tbl_guns ORDER BY guns_id DESC"; $Fixit = mysql_query($qstuff); } $rowsperpage = 2; if(isset($_GET['page'])) { $pagenum = $_GET['page']; }else{ $pagenum=1; } $offset = ($pagenum - 1) * $rowsperpage; $totalnum = mysql_query("SELECT guns_id FROM tbl_guns"); $total = mysql_num_rows($totalnum); $maxpage = ceil($total/$rowsperpage); $self = $_SERVER['PHP_SELF']; $qstring = $Fixit.",".$offset.",".$rowsperpage; $results = $qstring; if($total > $rowsperpage) { if ($pagenum > 1) { $page = $pagenum - 1; $prev = "<a href=\"".$self."?page=".$page."\">prev</a>"; $first = " <a href=\"".$self."?page=1\">first</a> "; }else{ $prev = " "; $first = " "; } if ($pagenum < $maxpage) { $page = $pagenum + 1; $next = "<a href=\"".$self."?page=".$page."\">next</a>"; $last = " <a href=\"".$self."?page=".$maxpage."\">last</a>"; }else{ $next = " "; $last = " "; } } ?> <div id="productsInfo"> <?php while($row = mysql_fetch_array($results)){ echo "<div class=\"productIcon\"><a href=\"productdetails.php?id=".$row['guns_id']."\"><img class=\"thumbReSize\" src=\"images/".$row['guns_img']."\" />".$row['guns_brand']."<br />".$row['guns_model']."<br />$".$row['guns_price']."</a></div>"; } ?> <div id="paginationContainer"><?php echo $first." ".$prev." Page ".$pagenum." of ".$maxpage." pages ".$next." ".$last; ?></div> Thanks again. Hi im trying to get pagination to work this is something im very new to and have tried a load f tutorial scripts but i keep getting: Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/a1581845/public_html/publist.php on line 60 here is my php code: <html> <head> <link rel="stylesheet" href="css/reset.css"> <link rel="stylesheet" href="css/style.css"> <title>E-Beer - Buying your pint has never been so easy!</title> </head> <body> <div id="head"><div id="header"> <div id="logo"></div><div id="login"> <form action="#" method="post"> <h3>Login to website:</h3> Username: <input type="text" name="name" class="1pxtotheright"><br> Password: <input type="password" name="password" class="1pxtotheright"><br> <div class="smallprint"><a href="register.php">Register Now</a> <a href="forget.php">Forgot Your Password?</a> </div> <input type="submit" value="Login"> </form> </div> </div> <div id="navbar"><a href="#">Home</a> <a href="#">Find a Pub</a> <a href="#">Register</a> <a href="#">Add your Pub</a> <a href="#">Contact Us</a> <a href="#">About Us</a></div> </div> <div id="container"> <?php include('includes/mysql.php'); $max = 1; //amount of articles per page. change to what to want $p = $_GET['p']; if(empty($p)) { $p = 1; } $limits = ($p - 1) * $max; //view the news article! if(isset($_GET['act']) && $_GET['act'] == "view") { $id = $_GET['id']; $sql = mysql_query("SELECT * FROM data WHERE id = '$id'"); while($info = mysql_fetch_array($sql)) { echo ' information'; } }else{ //view all the news articles in rows $sql = mysql_query("SELECT * FROM data LIMIT ".$limits.",$max"); //the total rows in the table $totalres = mysql_result(mysql_query("SELECT COUNT(id) AS tot FROM Pubs"),0); //the total number of pages (calculated result), math stuff... $totalpages = ceil($totalres / $max); //the table echo "<table><tr><td>Title</td><td>Author</td></tr><tr>"; while($info = mysql_fetch_array($sql)) { echo ' information } //close up the table echo " </tr></table>"; for($i = 1; $i <= $totalpages; $i++){ //this is the pagination link echo'<a href="publist.php?p='. $i .'">'. $i .'</a>'; } } ?> </div> </body> </html> If anyone could help it will be great, Thanks, Blink359 Hi, I'm very new to PHP and have been working on a small project that is a search engine connected to a mysql database. I can't seem to display the results from the search across multiple pages, when I click the Next button it just brings me to a blank page with no content except for my search form on top. I have a feeling that my problem lies in how I'm passing variables from one page to another as I realize that the URL of page 2 doesn't have the users search anymore. What do I need to change to get multiple pages working? Code: [Select] <?php if(isset($_GET['submit'])){ if(isset($_GET['name'])){ if(preg_match("/^[ a-zA-Z0-9]+/", $_GET['name'])){ $name=$_GET['name']; // Connect to database $db=mysql_connect("localhost", "root", "1234") or die ('Cannot connect: ' . mysql_error()); // Select database $mydb=mysql_select_db("mydb"); // Check page num, if no page num set to 1 if(isset($_GET['pagenum'])){ $pagenum = $_GET['pagenum']; } else{ $pagenum = 1; } // Query the database table $sql="SELECT id, ProductName, Price FROM testdb WHERE ProductName LIKE '%" . $name ."%'"; // Run query $result=mysql_query($sql); // Check to see if any results were found, if not, tell user $resultnum = mysql_num_rows($result); // Number of results per page $page_rows = 4; // Page number of previous page $last = ceil($resultnum/$page_rows); // Make sure page number isn't below 1 or above maximum if($pagenum < 1){ $pagenum = 1; } elseif($pagenum > $last){ $pagenum = $last; } // Set range to display in query $max = 'limit ' .($pagenum - 1) * $page_rows .',' .$page_rows; $sql2 = "SELECT id, ProductName, Price FROM testdb WHERE ProductName LIKE '%" . $name ."%' $max"; $data_p = mysql_query($sql2); if($resultnum == 0){ echo "<p class='p1'>No results found for query: " . $name . "</p>"; } else{ echo "<table class='sortable'>"; echo "<thead>"; echo "<tr>"; echo "<th>" . "Model Number" . "</th>"; echo "<th>" . "Product" . "</th>"; echo "<th>" . "Price" . "</th>"; echo "</tr>"; echo "</thead>"; // Loop through results $bg = '#9D9D9D'; while($row=mysql_fetch_array($data_p)){ $bg = ($bg=='#9D9D9D' ? '#C7C7C7' : '#9D9D9D'); $ProductName=$row['ProductName']; $Price=$row['Price']; $ID=$row['id']; // Display results of array echo "<tr bgcolor='" .$bg . "'>"; echo "<td width='30%', align='center'>" . " " . "</td>"; echo "<td width='60%', align='center'>" . "<a href=\"search.php?id=$ID\">" . $ProductName . "</a></td>"; echo "<td width='10%', align='center'>" . $Price . "</td>\n"; echo "</tr>"; echo "<tr><td></td></tr>"; } echo "</table>"; echo "<p>"; echo " --Page $pagenum of $last-- </p>"; if($pagenum == 1){ } else{ echo " <a href='{$_SERVER['PHP_SELF']}?pagenum=1'> <<-First</a> "; echo " "; $previous = $pagenum-1; echo " <a href='{$_SERVER['PHP_SELF']}?pagenum=$previous'> <-Previous</a> "; } if ($pagenum == $last){ } else{ $next = $pagenum+1; echo " <a href='{$_SERVER['PHP_SELF']}?pagenum=$next'>Next -></a> "; echo " "; echo " <a href='{$_SERVER['PHP_SELF']}?pagenum=$last'>Last ->></a> "; } } } else{ echo "<p class='p2'>Please enter a query.</p>"; } } } ?> The search form looks like this... Code: [Select] <form method="get" action="search.php" id="searchform"> <input type="text" name="name" size = "42"> <input type="submit" name="submit" value="Search" STYLE="font-family: Arial, Helvetica, sans-serif; font-weight: bold;"> </form> Thanks. Hello, I adapted the pagination script from the tutorial (great instruction and detail) from this site and it works perfectly. However, I added a search engine to it and it works like it should. The problem comes in with the following SQL query: Code: [Select] $sql = "SELECT COUNT(*) FROM qa"; If i modify it to fit my query, the number at the bottom of the page disappears and the > >> links go to blank pages. Also all queried rows show on one page and doesn't stop at ten. How do i modify my pagination script with a search engine so people can query their results without 'breaking' the pagination script?? Can somebody please help me sort this code out I have wirtten/got from a tutorial from phpfreaks - http://www.phpfreaks.com/tutorial/basic-pagination. The code correctly knows the number of pages needed in order to display the records from the database, but it displays all records on each page. I have 4 records and want 2 records to be shown on each page, but the code below displays all 4 records on both pages. Can someone please shed some light onto how I can display the first 2 records on the first page, and the second 2 records on the second page? code below: Code: [Select] <?php //Connect to the database $user="username"; $password="password"; $database="database"; $con = mysql_connect("localhost",$user,$password) or die ('Could not connect: ' . mysql_error()); mysql_select_db($database, $con) or die( "Unable to select database"); // Determine number of rows in database $query = mysql_query("SELECT * FROM vids"); $numrows=mysql_num_rows($query); // number of rows to show per page $rowsperpage = 2; // 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 { $currentpage = 1; } // 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; } // the offset of the list, based on current page $offset = ($currentpage - 1) * $rowsperpage; // get the info from the db $result = mysql_query("SELECT * FROM vids"); while($row = mysql_fetch_array($result)) { $id = $row['id']; $url = $row['url']; $page_url = $row['page_url'];; $title = $row['title']; $desc = $row['desc']; $date_add = $row['date_add']; $date_rec = $row['date_rec']; $place = $row['place']; $altitude = $row['altitude']; $jump_no = $row['jump_no']; //Display video echo "<div class='sky_cont'> <div class='sky_vid'><a class='video' href=\"$url\"><img src='http://www.netlinksurveyors.co.uk/test/images/lgo.jpg' alt=\"$title\" Border='0' /></a></div> <p><h4><a href=\"$page_url\" target='_blank'>$title</a></h4>$desc</p> </div>"; } /****** build the pagination links ******/ // if not on page 1, don't show back links if ($currentpage > 1) { // show << link to go back to page 1 echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=1'><<</a> "; // get previous page num $prevpage = $currentpage - 1; // show < link to go back to 1 page echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$prevpage'><</a> "; } // range of num links to show $range = 3; // 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 " [<b>$x</b>] "; // if not current page... } else { // make it a link echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$x'>$x</a> "; } } } // 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 " <a href='{$_SERVER['PHP_SELF']}?currentpage=$nextpage'>></a> "; // echo forward link for lastpage echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$totalpages'>>></a> "; } // end if /****** end build pagination links ******/ ?> This topic has been moved to PHP Applications. http://www.phpfreaks.com/forums/index.php?topic=349156.0 class curl2{ private $curl_init; private $CURLOPT_URL; public function connect(){ $this->curl_init = curl_init(); } public function debug(){ curl_setopt($this->curl_init, CURLOPT_VERBOSE, TRUE); $fp = fopen("curl2.txt", "w"); curl_setopt($this->curl_init, CURLOPT_STDERR, $fp); curl_setopt($this->curl_init, CURLOPT_RETURNTRANSFER, TRUE); } public function setUrl($url = null){ $this->CURLOPT_URL = $url; curl_setopt($this->curl_init, CURLOPT_URL, $this->CURLOPT_URL); } public function execute(){ $out = curl_exec($this->curl_init); curl_close($this->curl_init); return $out; } } $curl2 = new curl2; $curl2->connect(); $curl2->setUrl("http://www.linuxformat.co.uk"); $curl2->debug(); echo $curl2->execute(); It display a blank page like attachment result1.jpg, but if I move the $fp = fopen("curl2.txt", "w"); curl_setopt($this->curl_init, CURLOPT_STDERR, $fp); curl_setopt($this->curl_init, CURLOPT_RETURNTRANSFER, TRUE); from function debug() and join it with function execute() like this: public function execute(){ $fp = fopen("curl2.txt", "w"); curl_setopt($this->curl_init, CURLOPT_STDERR, $fp); curl_setopt($this->curl_init, CURLOPT_RETURNTRANSFER, TRUE); $out = curl_exec($this->curl_init); curl_close($this->curl_init); return $out; } it return me Linuxformat content ( expected result ) like result2.jpg below is the working code : class curl2{ private $curl_init; private $CURLOPT_URL; public function connect(){ $this->curl_init = curl_init(); } public function debug(){ curl_setopt($this->curl_init, CURLOPT_VERBOSE, TRUE); } public function setUrl($url = null){ $this->CURLOPT_URL = $url; curl_setopt($this->curl_init, CURLOPT_URL, $this->CURLOPT_URL); } public function execute(){ $fp = fopen("curl2.txt", "w"); curl_setopt($this->curl_init, CURLOPT_STDERR, $fp); curl_setopt($this->curl_init, CURLOPT_RETURNTRANSFER, TRUE); $out = curl_exec($this->curl_init); curl_close($this->curl_init); return $out; } } $curl2 = new curl2; $curl2->connect(); $curl2->setUrl("http://www.linuxformat.co.uk"); $curl2->debug(); echo $curl2->execute(); Why I couldn't split "CURLOPT_STDERR, CURLOPT_RETURNTRANSFER" with "curl_exec" can someone please tell me what is wrong with this code? <?php function documentType(){ echo <<<HEREDOC <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" ` ` "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> } HEREDOC; ?> I was wondering if someone can tell me what I have done wrong with this code: <?php foreach ($row['received'] as $data) { $data += $received; } echo $received; ?> It should be adding the number 2 three times, so it should be echoing 6 but its not. Problem 1 I have is that nothing is being added to the variable $received, and problem 2 is that I suspect $received will not become 6, but rather 222. Am I correct? I have a form on a page which is accessed when a user logs into their account. The form dynamically pulls bits of the users info from 2 tables in a mysql database, Tables (ITEMS) and Table (USERS). Table (USERS) holds the users info (NAME, EMAIL ETC) while Table (ITEMS) hold information about the users items for which they may have several of. When the form page is accessed all the users items from Table (ITEMS) are dynamically shown on the form with several radio buttons at the side of each item. The user can tick certain radio buttonss at the side of each item and click the send button. TRYING TO ACHIEVE... After the form is sent a thank you message is returned on the same page with details of the items and what radios were ticked. An Email of the same is sent to user with info of the items and what radios were ticked. An Email is also sent to me with same info of the items and what radios were ticked. WHAT I AM ACHIEVING SO FAR WITH CODE BELOW: The form page is ok showing all users items, radios, send button etc. The returned thank you page is fine showing items, what radios were checked etc. Even the email part is working sending an email to the user and myself. THE PROBLEM IS THAT A SEPERATE EMAIL IS SENT TO BOTH OF US FOR EACH ITEM THE USER HAS IN THE DATABASE. I JUST WANT ONE EMAIL SENDING TO BOTH OF US DETAILING ALL ITEMS. I realise this is because of the while statement in the call to the database which is looping the below code for each item thus sending an email for each item. What I can not suss out is how to send just the one email with all items on. Tried moving the closing loop bracket all over the place and parts of the code but to not avail. THE CODE: (some unecessary code removed from echo/thank you to simplify) <table width="100%"> <tr> <td class="bodytext"><strong>Name</strong></td> <td class="bodytext"><strong>Ref</strong></td> <td><strong>Status</strong></td> <td><strong>Type</strong></td> <td><strong>Date</strong></td> <td><strong>Extend</strong></td> <td><strong>Feature</strong></td> <td><strong>Offer</strong></td> </tr> <form action="example-this-page" method="post"> <input type="hidden" name="action" value="0"> <input type="hidden" name="id" value="<?php echo $_REQUEST["id"]; ?>"> <?php $sql = "SELECT * FROM ".$TABLES["items"]." WHERE user_id='".$_SESSION["UserAccount"]."'"; $sql_result = mysql_query ($sql, $connection ) or die ('request "Could not execute SQL query" '.$sql); while ($ITEMS = mysql_fetch_assoc($sql_result)) { $sql = "SELECT * FROM ".$TABLES["users"]." WHERE id='".$ITEMS["user_id"]."'"; $sql_resultT = mysql_query ($sql, $connection ) or die ('request "Could not execute SQL query" '.$sql); $USER=mysql_fetch_assoc($sql_resultT); ?> <? if (isset($action)) { $to = $USER["email"]; $mailheader = "From: $email\r\n"; $mailheader .= "Reply-To: $email\r\n"; $mailheader .= "Content-type: text/html; charset=iso-8859-1\r\n"; $msg = "Dear ".stripslashes($USER["name"])."<br><br> <table><tr> <td><strong>Name</strong></td> <td><strong>Ref No</strong></td> <td><strong>Status</strong></td> <td><strong>Type</strong></td> <td><strong>Date</strong></td> <td><strong>Extend</strong></td> <td><strong>Feature</strong></td> <td><strong>Offer</strong></td> </tr> <tr> <td>".stripslashes($ITEMS["name"])."</td> <td>".stripslashes($ITEMS["ref"])."</td> <td>".stripslashes($ITEMS["status"])."</td> <td>".stripslashes($ITEMS["type"])."</td> <td>".stripslashes($ITEMS["date"])."</td> <td>".$_POST['extend']."</td> <td>".$_POST['feature']."</td> <td>".$_POST['offer']."</td> </tr> </table> "; mail($to, "subject", $msg, $mailheader) or die ("Failure"); mail("me@me.co.uk", "subject", $msg, $mailheader) or die ("Failure"); echo "<br /><br /><font size=2> <strong><center>Thank you!</center><br /> Your details of your request has been sent:</center><br /> "; }; ?> <tr> <td><?php echo stripslashes($ITEMS["name"]); ?></td> <td><?php echo stripslashes($ITEMS["ref"]); ?></td> <td><?php echo stripslashes($ITEMS["status"]); ?></td> <td><?php echo stripslashes($ITEMS["type"]); ?></td> <td><?php echo stripslashes($ITEMS["date"]); ?></td> <td><input type="radio" name="extend<?php echo stripslashes($ITEMS["id"]); ?>" value="No"> No <input type="radio" name="extend<?php echo stripslashes($ITEMS["id"]); ?>" value="Yes" /> Yes</td> <td><input type="radio" name="featured<?php echo stripslashes($ITEMS["id"]); ?>" value="No"> No <input type="radio" name="featured<?php echo stripslashes($ITEMS["id"]); ?>" value="Yes" /> Yes</td> <td><input type="radio" name="latedeal<?php echo stripslashes($ITEMS["id"]); ?>" value="No"> No <input type="radio" name="latedeal<?php echo stripslashes($ITEMS["id"]); ?>" value="Yes" /> Yes</td> </tr> <?php } ?> <tr> <td> <input type="submit" name="Submit" value="Send"></td> </tr></table> </form> </table> Hello Please can someone help me, I currently have all working on my friends online shop other than the pagination. When clicking a topic say category - products matching that category show, which is fine only if there is more than say 12 records i need to split it between php pages. Again works fine on the first page but when I click previous or last it ignores the id (category) and lists all the products not only under 'Black' I am really new to PHP so if you can help please could you show me on my code below so I can make sense of it. here's my could for the category :- <a href='shop_online2.php?id=<?php echo($row['id']) ?>'> <?php echo($row['description']) ?></a> <?php } while ($row_cat = mysql_fetch_assoc($cat)); ?> here's my code for the next button :- <? if (!($start>=$record_count-$per_page)) echo " <a href='shop_online2.php?start= $next '>Next</a> "; ?> Hopefully I will only have to add a variable in the last statement, if so could you please show me the correct formatting for this. Hope this makes sense ?? Thanks Marc <?php require_once('upper.php'); // Connects to your Database require_once('database.php'); //This checks to see if there is a page number. If not, it will set it to page 1 if (!(isset($pagenum))) { $pagenum = 1; } //Here we count the number of results //Edit $data to be your query $query = "SELECT * FROM events"; $result = mysqli_query($dbc,$query) or die('Not'); $rows = mysqli_num_rows($result); //This is the number of results displayed per page $page_rows = 2; //This tells us the page number of our last page $last = ceil($rows/$page_rows); //this makes sure the page number isn't below one, or more than our maximum pages if ($pagenum < 1) { $pagenum = 1; } elseif ($pagenum > $last) { $pagenum = $last; } //This sets range that we will display in our query $max = 'limit ' .($pagenum - 1) * $page_rows .',' .$page_rows; //This is your query again, the same one... the only difference is we add $max into it $result = mysqli_query($dbc,"SELECT * FROM events $max") or die('Not'); //This is where you display your query results while($row = mysqli_fetch_array( $result)) { $Title=$row['Title']; $City=$row['City']; $Content=$row['Content']; //$Photo=$row['Photo']; $Date=$row['Date']; $EventId=$row['EventId']; $Photo=$row['Photo']; //echo $Photo; echo "<div><table border='0' cellspacing='0' cellpadding='0' width='389'> <tr> <td><img src='images/events_2.png' width='389' height='10'></td> </tr> <tr> <td background='images/events_2_bg.png'> <table border='0' cellspacing='0' width='359'> <tr> <td> <tr> <table width='100%' border='0' cellspacing='0' > <tr> <td rowspan='3'><img src='$Photo' width='80' height='60'></td> <td align='left' valign='top' width='180'>$City</td> <td align='left' valign='top'>$Date</td> </tr> <tr> <td colspan='3' align='left' valign='top'>$Title</td> </tr> <tr> <td colspan='2'><a href='KnowMore.php?EventId=".$row['EventId']."'>Know more </a> / <a href='EventParticipator.php?EventId=".$row['EventId']."'>click here to participate</a></td> </tr> </table> </tr> </tr> </table> </td> </tr> <tr> <td><img src='images/events_2_bottom.png' width='389' height='10' ></td> </tr> </table></div>"; } echo "<p>"; // This shows the user what page they are on, and the total number of pages echo " --Page $pagenum of $last-- <p>"; // First we check if we are on page one. If we are then we don't need a link to the previous page or the first page so we do nothing. If we aren't then we generate links to the first page, and to the previous page. if ($pagenum == 1) { } else { echo " <a href='{$_SERVER['pagination.php']}?pagenum=1'> <<-First</a> "; echo " "; $previous = $pagenum-1; echo " <a href='{$_SERVER['page_test.php']}?pagenum=$previous'> <-Previous</a> "; } echo $PHP_SELF; //just a spacer echo " ---- "; //This does the same as above, only checking if we are on the last page, and then generating the Next and Last links if ($pagenum == $last) { } else { $next = $pagenum+1; echo " <a href='{$_SERVER['page_test.php']}?pagenum=$next'>Next -></a> "; echo " "; echo " <a href='{$_SERVER['page_test.php']}?pagenum=$last'>Last ->></a> "; } require_once('lower.php'); ?> Quote Hi friends............ I want to paginate my page but it not works properly. It displays first 2 rows but on clicking "Next" nothing happens. It displays error also "Notice: Undefined variable: PHP_SELF in C:\wamp\www\NGOProject\Elite Brigade\pagination.php on line 112" and "Notice: Undefined index: page_test.php in C:\wamp\www\NGOProject\Elite Brigade\pagination.php on line 124"......... I don't know what to do?????? please help me anyone??????? I am trying to code a php, mysql and jquery pagination much like the way twitter's pagination works. Page 1 i have Code: [Select] <div id="load_more"> <?php $query = mysql_query("SELECT * FROM comments ORDER BY id DESC LIMIT 5"); while($row = mysql_fetch_assoc($query)){ $id = $row['id']; // show comments code } ?> </div> <script type="text/javascript"> $(function() { $('.showmore').live("click",function() { var ID = $(this).attr("id"); if(ID) { $("#more"+ID).html('<img src="ajax-loader.gif" />'); $.ajax({ type: "POST", url: "comments_more.php", data: "lastmsg="+ ID, cache: false, success: function(html){ $("#load_more").append(html); $("#more"+ID).remove(); // removing old more button } }); } else { $(".morebox").html('No more comments.');// no results } return false; }); }); </script> <div id="more<?php echo $id; ?>" class="morebox"> <a href="javascript:;" style="text-decoration: none; font-size: 12px; color: #ffffff; font-weight: bold; " class="showmore" id="<?php echo $id; ?>">Load More Comments</a> </div> So i am only showing 5 rows untill the link is clicked and then i want it to load more comments from comments_more.php which is the same page as above just it checks for the lastmsg id and alters the query to WHERE id < '$lastmsg'. Everything works ok for clicking the link and showing more results but it only works once. even if there is more than 10 rows it will show as "no more comments" after 10. Any help will be appreciated thanks. Sorry to bug yalls with yet another problem, but I tried applying pagination to the page I use to display the news on my website, which is stored within mysql, and I'm having major issues. Here's the code below for both the pagination and getting the news out of the database to display. Not sure what's going on here. I tested the script and it does everything fine except it doesn't display the information stored within the database. Go here to see for yourself: http://www.djsmiley.net/index.php Code: [Select] <?php /* Place code to connect to your DB here. */ include('cms/news/dbconnect.php'); // include your code to connect to DB. $tbl_name="mynews"; //your table name // How many adjacent pages should be shown on each side? $adjacents = 3; /* First get total number of rows in data table. If you have a WHERE clause in your query, make sure you mirror it here. */ $query = "SELECT COUNT(*) as num FROM $tbl_name"; $total_pages = mysql_fetch_array(mysql_query($query)); $total_pages = $total_pages[num]; /* Setup vars for query. */ $targetpage = "index.php"; //your file name (the name of this file) $limit = 1; //how many items to show per page $page = $_GET['page']; if($page) $start = ($page - 1) * $limit; //first item to display on this page else $start = 0; //if no page var is given, set start to 0 /* Get data. */ $sql = "SELECT 10 FROM $tbl_name LIMIT $start, $limit"; $result = mysql_query($sql); /* Setup page vars for display. */ if ($page == 0) $page = 1; //if no page var is given, default to 1. $prev = $page - 1; //previous page is page - 1 $next = $page + 1; //next page is page + 1 $lastpage = ceil($total_pages/$limit); //lastpage is = total pages / items per page, rounded up. $lpm1 = $lastpage - 1; //last page minus 1 /* Now we apply our rules and draw the pagination object. We're actually saving the code to a variable in case we want to draw it more than once. */ $pagination = ""; if($lastpage > 1) { $pagination .= "<div class=\"pagination\">"; //previous button if ($page > 1) $pagination.= "<a href=\"$targetpage?page=$prev\"><img class=\"fltlft2\" src=\"http://www.djsmiley.net/images/Icons/Arrows/Previous.png\" height=\"25\" width=\"25\"></a>"; else $pagination.= "<span class=\"disabled\"><img class=\"fltlft2\" src=\"http://www.djsmiley.net/images/Icons/Arrows/Previous.png\" height=\"25\" width=\"25\"></a></span>"; //pages if ($lastpage < 7 + ($adjacents * 2)) //not enough pages to bother breaking it up { for ($counter = 1; $counter <= $lastpage; $counter++) { if ($counter == $page) $pagination.= "<span class=\"current\">$counter</span>"; else $pagination.= "<a href=\"$targetpage?page=$counter\">$counter</a>"; } } elseif($lastpage > 5 + ($adjacents * 2)) //enough pages to hide some { //close to beginning; only hide later pages if($page < 1 + ($adjacents * 2)) { for ($counter = 1; $counter < 4 + ($adjacents * 2); $counter++) { if ($counter == $page) $pagination.= "<span class=\"current\">$counter</span>"; else $pagination.= "<a href=\"$targetpage?page=$counter\">$counter</a>"; } $pagination.= "..."; $pagination.= "<a href=\"$targetpage?page=$lpm1\">$lpm1</a>"; $pagination.= "<a href=\"$targetpage?page=$lastpage\">$lastpage</a>"; } //in middle; hide some front and some back elseif($lastpage - ($adjacents * 2) > $page && $page > ($adjacents * 2)) { $pagination.= "<a href=\"$targetpage?page=1\">1</a>"; $pagination.= "<a href=\"$targetpage?page=2\">2</a>"; $pagination.= "..."; for ($counter = $page - $adjacents; $counter <= $page + $adjacents; $counter++) { if ($counter == $page) $pagination.= "<span class=\"current\">$counter</span>"; else $pagination.= "<a href=\"$targetpage?page=$counter\">$counter</a>"; } $pagination.= "..."; $pagination.= "<a href=\"$targetpage?page=$lpm1\">$lpm1</a>"; $pagination.= "<a href=\"$targetpage?page=$lastpage\">$lastpage</a>"; } //close to end; only hide early pages else { $pagination.= "<a href=\"$targetpage?page=1\">1</a>"; $pagination.= "<a href=\"$targetpage?page=2\">2</a>"; $pagination.= "..."; for ($counter = $lastpage - (2 + ($adjacents * 2)); $counter <= $lastpage; $counter++) { if ($counter == $page) $pagination.= "<span class=\"current\">$counter</span>"; else $pagination.= "<a href=\"$targetpage?page=$counter\">$counter</a>"; } } } //next button if ($page < $counter - 1) $pagination.= "<a href=\"$targetpage?page=$next\"><img src=\"http://www.djsmiley.net/images/Icons/Arrows/Next.png\" height=\"25\" width=\"25\"></a>"; else $pagination.= "<span class=\"disabled\"><img src=\"http://www.djsmiley.net/images/Icons/Arrows/Next.png\" height=\"25\" width=\"25\"></span>"; $pagination.= "</div>\n"; } ?> <?php while($row = mysql_fetch_array($result)) { echo("<p class=NewsContainer><span class=NewsID>$type</span> <font size=5>$title</font><br><br> <em>posted by <strong>$user</strong> | added on $time</em><br><br> $message<br><br> <label class=fltlft2><img src=../images/Icons/Arrows/Right.png width=20 height=20/></label><a href=$url>Read more - $url</a> <div class=newsLikeShareRate> <table width=100% border=0> <tr> <td width=3% height=21><script src=http://connect.facebook.net/en_US/all.js#xfbml=1></script> <fb:like href='$url' show_faces=true width=450 font=arial></fb:like> </td> <td width=65%><a name=fb_share id=fb_share4 type=icon_link share_url=$url>Share</a> <script src=http://static.ak.fbcdn.net/connect.php/js/FB.Share type=text/javascript></script></td> <td width=32%>Rate this article: </td> </tr> </table> </div></p><br><br>");// Your while loop here } ?> <?=$pagination?> i have some code which checks all of the posts in the database with the topic_id of $topic_id. what i want to do is like on many forums. On the topic list next to the name just show the pages like: Code: [Select] 1-2-3-4 and when there are more than 4 or so Code: [Select] 1-2....8-9 here is the code which gets the number of pages: $data = $db->query("SELECT * FROM ".DB_PREFIX."posts WHERE topic_id = '$topic_info->topic_id'"); $rows = mysql_num_rows($data); $page_rows = $posts_per_page; if ($rows > $page_rows) { $rowsArray = array($rows); } as you can see i have added it into an array but i dont know what to do from there. I guessed it would be something like a for statement but im not sure. Can anyone help? I'm working on a flat file database that I can search. I got most of it done, but wondering how I can paginate the results.. <?php ini_set('display_errors', "On"); $count = 0; $time_start = microtime(true); $handle = fopen('/var/txtdb/links.txt', "r"); if ($handle) { while (!feof($handle) && $count < 15) { $line = fgets($handle, "4096"); $pos = strpos($line, "keyword"); if ($pos == true) { preg_match('~\[desc = ([^\]]*)\]~is', $line, $desc); echo "$desc[1]<br>"; $count++; } } fclose($handle); } $time_end = microtime(true); $time = $time_end - $time_start; echo '<br>Script took '.$time.' seconds to execute'; ?> So I want to display 15 results per page.. I know I could just loop through all the results each page just start from the last result +15 more. but.. that would take too long considering the amount of data I have. So is there a way to start the loop at a specific line? That way I won't have to loop through them all and waste time and slow things down.. My php/mysql book doesn't cover pagination so I've been searching the web for a simple pagination script that I can learn from. I got the following one working at http://www.phpsimplicity.com/tips.php?id=1 but it's just showing the next and prev links... no results on the pages and you can click on the next link forever when there's only 11 records in the table I queried. How can I display results on each page and have the info append to the next page? This is the second example I've tried and none seem to explain this part? Hello friends. I have a pagination script and there are 3 issues I cant seem to figure out. Here is the code: Code: [Select] <?php // page_name.php $page = $_GET['page']; //some code $your_db = @ new mysqli("hostname", "username", "password"); if (mysqli_connect_errno()) { echo 'ERROR!<br />'.mysqli_connect_errno() .' - Not connected : '.mysqli_connect_error().'<br />'; die; } else { $db_connect = $your_db->select_db("database"); if (!$db_connect) { echo 'ERROR CONNECT DATA BASE'; die; } } $query = "select fldIconMedium, fldTitle, fldLink from games"; //$result = $db_connect->query($query); $result = $your_db->query($query); $number_of_records = $result->num_rows; $num_pages = $number_of_records / 16; if (($number_of_records % 16) > 0 ) { $num_pages++; } if (strlen($page) == 0) { $page = 0; } else { $page = $page * 16; } $br = 1; $result->data_seek($page); for ($i = $page; $i < $number_of_records; $i++) { //$row = $result->fetch_array(); $row = $result->fetch_assoc(); if ($br <= 16) { echo '<b>'.stripslashes($row['fldIconMedium']).' </b>'; echo stripslashes($row['fldTitle']).' '; echo '<b>'.stripslashes($row['fldLink']).'</b><br />'; $br++; } else { break; } } for ($j = 0; $j < $num_pages; $j++) { $page_link = $j + 1; echo '<a href="games2.php?page='.$j.'">'.$page_link.'</a>'; } echo ' '.$number_of_records; ?> Here is a database I am using: |fldID| |fldTitle| | fldCategory | fldIconMedium | | fldLink | 2415 Krusty Fun House Skills krusty.jpg games.php?id=2415 2415 Krusty Fun House Skills krusty.jpg games.php?id=2415 2415 Krusty Fun House Skills krusty.jpg games.php?id=2415 2415 Krusty Fun House Adventure krusty.jpg games.php?id=2415 2415 Krusty Fun House Adventure krusty.jpg games.php?id=2415 2415 Krusty Fun House Adventure krusty.jpg games.php?id=2415 2415 Krusty Fun House Action krusty.jpg games.php?id=2415 2415 Krusty Fun House Action krusty.jpg games.php?id=2415 2415 Krusty Fun House Action krusty.jpg games.php?id=2415 I have a couple of issues as I am still a noob learning PHP. ===================================== 1. I have no idea how to add a 'Previous' and 'Next' button to the pagination number bar. 2. My results are displayed vertical and I can't make my results display in a horizontal view. 5 results going from left to right and results from top to bottom Basically 5 x 7 table 3. I have an issue with categorizing my games in my pagination table. I have multiple categories like Action, Skills, Arcade, Skills, Mind, Shooter in my database. For example, the pagination script above creates the link to be like this '.php?page=1', when in fact I want the script to search for categories games, example '.php?page=skills', '.php?page=action', '.php?page=adventure' etc..., linking them to the 'fldCategory' column which has the list of the category names of games, and then fetching all the rows of that particular game category and be displayed in the 5 x 7 table. For each result, it would have the Image Icon (fldIconMedium), the Title (fldTitle) under it, and both of them hyper linked to their links from the 'fldLinks' column. I would really appreciate the help. It has been 2 weeks and I'm very frustrated that I can't figure the 3 points out. hi ive simple code. but i would need help how to do pagination. ive followed one tutorial and ive code.but i really dont know how to connect these.or if its even possible to connect. heres the index.php file Code: [Select] <?php include '_class/cms_class.php'; $obj = new modernCMS(); $obj->host = 'localhost'; $obj->username = 'root'; $obj->password = 'root'; $obj->db = 'modernCMS'; $obj->connect(); ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <title>Untitled Document</title> <link rel="stylesheet" href="style.css" type="text/css" media="screen" title="no title" charset="utf-8"> </head> <body> <div id="page-wrap"> <?php if(isset($_GET['id'])): $obj->get_content($_GET['id']); else: $obj->get_content(); endif; ?> </div> </body> </html> cms_class.php Code: [Select] <?php class modernCMS { var $host; var $username; var $password; var $db; function connect() { $con = mysql_connect($this->host, $this->username, $this->password) or die(mysql_error()); mysql_select_db($this->db, $con) or die(mysql_error()); } function get_content($id = '') { if($id != ""): $id = mysql_real_escape_string($id); $sql = "SELECT * FROM cms_content WHERE id = '$id'"; else: $sql = "SELECT * FROM cms_content ORDER BY id DESC"; endif; $res = mysql_query($sql) or die(mysql_error()); while($row = mysql_fetch_assoc($res)) { echo '<h1><a href="index.php?id=' . $row['id'] . '">' . $row['title'] . '</a></h1>'; echo '<p>' . $row['body'] . '</p>'; } } } ?> Heres the pagination code ihave Code: [Select] <?php include 'db.inc.php'; $per_page = 2; $pages_query = mysql_query("SELECT COUNT(`id`) FROM `cms_content`"); $pages = ceil(mysql_result($pages_query, 0) / $per_page); $page = (isset($_GET['page'])) ? (int)$_GET['page'] : 1; $start = ($page - 1) * $per_page; $query = mysql_query("SELECT * FROM `cms_content` LIMIT $start, $per_page"); while ($query_row = mysql_fetch_assoc($query)) { echo '<p>', $query_row['title'] ,'</p>'; echo '<p>', $query_row['body'] ,'</p>'; } if ($pages >=1 && $page <= $pages) { for ($x=1; $x<=$pages; $x++) { echo ($x == $page) ? '<strong><a href="?page=' .$x. '">' .$x. '</a></strong> ' : '<a href="?page=' .$x. '">' .$x. '</a> '; } } ?> thanks. |