PHP - Previous/next Pagination Links
Hi all,
Anyone give me a few hints how I can add a next and previous button to my pagination links? I currently have this: <?php } if($total_records > 5) ?> <div id="pg"> <?php { for ($i = 1; $i <= $total_pages; $i++) { echo "<a title='page $i' class='current' href='?catid=" . $catid ."&"; if($subid > 0) { echo "subid="; echo $subid . "&"; } echo "p= $i'>$i</a>"; } } ?> </div> Which displays them nicely, but I would like a next and previous button. Cheers Similar TutorialsI messed around with a simple pagination script I found online. I'm still diving in and breaking it down to try and understand it better but I also was planning to have a "next" and "previous" link instead of just echoing out page numbers. I know I'll have to come up with some $pg+1 or $pg-1 function but how do I find out what $pg is currently on? function showSubmissions(){ $max = 9; // number of news entries per page $pg = $_GET['pg']; if(empty($pg)) { $pg = 1; } $limits = ($pg - 1) * $max; //view all the news articles in rows $q = mysql_query("SELECT * FROM submissions WHERE approved='YES' ORDER BY postdate DESC LIMIT " . $limits . ", $max") or die(mysql_error()); //the total rows in the table $totalres = mysql_result(mysql_query("SELECT COUNT(id) AS tot FROM submissions"), 0); //the total number of pages (calculated result), math stuff... $totalpages = ceil($totalres / $max); // BEGIN PAGINATION print ' <div class="paginationList"> <ul class="pgList"> <li><img src="images/icon_prevresult.png" title="" alt="" /></li>'; for($i = 1; $i <= $totalpages; $i++) { //this is the pagination link print '<li><a href="submissions.php?pg=' . $i . '">' . $i . ' </a></li>'; } print ' <li><img src="images/icon_nextresult.png" title="" alt="" /></li> </ul> </div>'; // END PAGINATION // PRINT RESULTS while($row = mysql_fetch_array($q)) { $id = $row['id']; $name = stripslashes($row['name']); $email = $row['email']; print ' <div class="sub-thumb-wrapper"> <div class="sub-thumb-user"> <div class="ps-name"><p class="sub-label">NAME:</p><a href="mailto:' . $email . '">' . $name . '</a></div> </div> </div>' . "\n"; } // END PRINT RESULTS } I am trying to create a gallery page, one photo on the page with Next/Previous links either above or below but am having a little local difficulty. This is what I have:- Code: [Select] //>>> Set up arrays and variables <<< $piccies = array(); $exif = array(); $exifdata = array(); $pic = 0; $img =''; $date = ''; $time = ''; $caption = ''; $copyright =''; $imageid = 1; $first = 1; $last = 5; $self = $_SERVER['PHP_SELF'] . "?imageid="; //>>> Read images from folder and assign to the array $piccies[] <<< $piccies = read_folder(); //>>> Find image and extract the Exif info <<< $exif = read_exif_info($piccies, $pic); //>> Populate with required info from $exifdata with required info from $exif $exifdata = populate_exif_data($exif, $date, $time); $img = substr_replace($img, "", 0, 2); ?> <div id="pict"> <img src="<?php echo $img; ?>" /> <?php echo "<p>" . $caption . "</p>"; if ($imageid < $last) { echo "<a href=" . $self . ($imageid + 1) . "><h2>Next</h2></a>"; } if ($imageid > $first) { echo "<a href=" . $self . ($imageid - 1) . "><h2>Previous</h2></a>"; } ?> </div> It puts the 'Next' link in fine, but when clicked the link goes to the Parent directory:- Index of /php/gallery [ICO] Name Last modified Size Description [DIR] Parent Directory - [ ] functions.php 27-Oct-2011 09:25 2.1K [ ] gallery index.php 27-Oct-2011 09:45 1.8K [DIR] images/ 26-Oct-2011 16:00 - and when I look at the source I see that the link has not been constructed as I expected. This is the source:- Code: [Select] <div id="pict"> <img src="images/blue sky.jpg" /> <p>Blue Sky over Auld Reekie</p><a href=/php/gallery test.php?imageid=2><h2>Next</h2></a></div> where '/php/gallery test.php' is showing I expected 'images/filename.jpg' etc. and I just can't figureout what I have done wrong... I've got a page with pagination set up, but I can't figure out where I went wrong with the links at the bottom. When I do a search, it generates the proper number of links, but clicking on the links goes to a page with a whole row of 20 links at the bottom and no results on the page. (my search should show 3 pages with 2 records on each page) I've looked through the tutorial here and one in a book on pagination. Not seeing what's wrong. I'd narrow this down if I knew where it was causing the problem. pagination links are at the very bottom of the code. Code: [Select] <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> <html><head> <meta content="text/html; charset=ISO-8859-1" http-equiv="content-type"><title>search.html</title></head> <body> <center> <p>The results of your search:</p> <br> </center> require ('databaseconnect.php'); $select = mysql_select_db($db, $con); if(!$select){ die(mysql_error()); } $display = 2; if (isset($_GET['np'])) { $num_pages = $_GET['np']; } else { $data = "SELECT COUNT(*) FROM descriptors LEFT JOIN plantae ON (descriptors.plant_id = plantae.plant_name) WHERE `leaf_shape` LIKE '%$select1%' AND `leaf_venation` LIKE '%$select3%' AND `leaf_margin` LIKE '%$select4%'"; $result = mysql_query ($data); if (!$result) { die("Oops, my query failed. The query is: <br>$data<br>The error is:<br>".mysql_error()); } $row = mysql_fetch_array($result, MYSQL_NUM); // row 41 $num_records = $row[0]; if ($num_records > $display) { $num_pages = ceil ($num_records/$display); } else { $num_pages = 1; } } if (isset($_GET['s'])) { $start = $_GET['s']; } else { $start = 0; } if(isset($_POST[submitted])) { // Now collect all info into $item variable $shape = $_POST['select1']; $color = $_POST['select2']; $vein = $_POST['select3']; $margin = $_POST['select4']; // This will take all info from database where row tutorial is $item and collects it into $data variable row 55 $data = mysql_query("SELECT `descriptors`.* ,`plantae`.* FROM `descriptors` LEFT JOIN `plantae` ON (`descriptors`.`plant_id` = `plantae`.`plant_name`) WHERE `leaf_shape` LIKE '%$select1%' AND `leaf_venation` LIKE '%$select3%' AND `leaf_margin` LIKE '%$select4%' ORDER BY `plantae`.`scientific_name` ASC LIMIT $start, $display"); //chs added this in... row 72 echo '<table align="center" cellspacing="0" cellpading-"5"> <tr> <td align="left"><b></b></td> <td align="left"><b></b></td> <td align="left"><b>Leaf margin</b></td> <td align="left"><b>Leaf venation</b></td> </tr> '; // This creates a loop which will repeat itself until there are no more rows to select from the database. We getting the field names and storing them in the $row variable. This makes it easier to echo each field. while($row = mysql_fetch_array($data)){ echo '<tr> <td align="left"> <a href="link.php">View plant</a> </td> <td align="left"> <a href="link.php">unknown link</a> </td> <td align="left">' . $row['scientific_name'] . '</td> <td align="left">' . $row['common_name'] . '</td> <td align="left">' . $row['leaf_shape'] . '</td> </tr>'; } echo '</table>'; } if ($num_pages > 1) { echo '<br /><p>'; $current_page = ($start/$display) + 1; // row 100 if ($current_page != 1) { echo '<a href="leafsearch2a.php?s=' . ($start - $display) . '&np=;' . $num_pages . '">Previous</a> '; } for ($i = 1; $i <= $num_pages; $i++) { if($i != $current_page) { echo '<a href="leafsearch2a.php?s=' . (($display * ($i - 1))) . '$np=' . $num_pages . '">' . $i . '</a>'; } else { echo $i . ' '; } } if ($current_page != $num_pages) { echo '<a href="leafsearch2a.php?s=' . ($start + $display) . '$np=' . " " . $num_pages . '"> Next</a>'; } } ?> </body></html> I'm trying to create a function where you specify the $points it will then do some mathematical stuff with the $points (explain later) an return an array containing the $rank and $level. The mathematical stuff: It starts at 99 and when it reaches +199 it changes the key of the array. I'm not sure how to describe this theirfore i'll just provide examples: Example 1: If $points <= 99 It will return an array containing $ranks[0] and $levels[0], would be the following: return array('Peon', 0); Example 2: If $points >= 100 && $points <= 299 It will return an array containing $ranks[1] and $levels[1], would be the following: return array('Grunt', 1); Example 3: If $points >= 300 && $points <= 499 It will return an array containing $ranks[2] and $levels[2], would be the following: return array('Berserker', 2); etc.. until it reaches the 7th key of the $ranks/$levels array Heres my code: <?php function rank($points){ //their are 0-7 levels $levels = range(0, 7); //7 ranks $ranks = array('Peon', 'Grunt', 'Berserker', 'Tauren', 'Spirit Walker', 'Wind Rider'); for(...) { return array($rank, $level); } } ?> Hi all, I have a fairly simple problem, but i cannot get to it. I have a page, that displays one (1) record at the time, and I want to have forward and back buttons (links) at the top and bottom of the page. Can anyone help me with that??? Here is a part of my code. Code: [Select] <form method="get"> <table border="0" width="90%" cellpadding="2" cellspacing="2" align="center"> <tr><td align="center"> Select first letter or kind of the story: <input type="hidden" name="link" value="stories.php"> <input type="hidden" name="Pripadnost" value="3"> <select name="letter" onchange="this.form.submit()"> <?PHP $letter = trim($_REQUEST["letter"]); ?> <option value="Sve" <?PHP echo $letter == "All" ? " selected " : "" ; ?>>All</option> <?PHP include_once ("/includes/databaseMySQL.php"); mysql_set_charset('utf8'); $SQL = "Select distinct ucase(left(title,1)) as sl FROM tblStoryes order by sl"; $rs_data = mysql_query($SQL) or die(mysql_error()); while ($row_data = mysql_fetch_assoc($rs_data)) { ?> <option value="<?PHP echo $row_data["sl"]; ?>"<?PHP echo $letter == $row_data["sl"] ? " selected " : "" ; echo ">".$row_data["sl"] ;?></option> <?PHP // ****************** Listed all the first letters of the titles into a drop box. *********************** // } ?> </select> Bosnian <input type="checkbox" name="Bosnian " <?PHP echo isset($_REQUEST["Bosnian "]) ? "checked=checked" : "" ;?> onclick="this.form.submit()" /> Turkish <input type="checkbox" name="Turkish " <?PHP echo isset($_REQUEST["Turkish "]) ? "checked=checked" : "" ;?> onclick="this.form.submit()" /> Arabian <input type="checkbox" name="Arabian " <?PHP echo isset($_REQUEST["Arabian "]) ? "checked=checked" : "" ;?> onclick="this.form.submit()" /> </td></tr> </table> <table border="0" width="90%" cellpadding="2" cellspacing="2" align="center"> <?PHP $id = -1; $rs_data = mysql_query("Select max(id) as maxid from tblStories WHERE aktivno = True") or die(mysql_error()); $row_data = mysql_fetch_assoc($rs_data); $maxid = (int) $row_data["maxid"]; // ****************** Found the largest ID from the table. *********************** // $SQL = "SELECT * FROM tblStories WHERE aktivno = True "; if ( strlen($letter) < 3 && strlen($letter) > 0 ) { $SQL = $SQL . " and ucase(left(`title`, 1)) = '" . $Letter . "'"; } $lang = 0; $lang = $lang + isset($_REQUEST["Bosnian"]) * 4; $lang = $lang + isset($_REQUEST["Turkish"] ) * 2; $lang = $lang + isset($_REQUEST["Arabian"] ); switch ($lang) { case 6: // bos i tur $SQL = $SQL . " and ( Bosanski = 1 or Turski = 1 ) "; break; case 5: // bos i ar $SQL = $SQL . " and ( Bosanski = 1 or Arapski = 1) "; break; case 4: // bos $SQL = $SQL . " and Bosanski = 1 "; break; case 3: // tur i ar $SQL = $SQL . " and ( Turski = 1 or Arapski =1) "; break; case 2: // tur $SQL = $SQL . " and Turski = 1 "; break; case 1: // ar $SQL = $SQL . " and Arapski =1 "; break; } if (trim($_REQUEST["DBid"]) <> "" or $_REQUEST["DBid"] > 0) $SQL = $SQL . " and id > " . trim($_REQUEST["DBid"]) ; $SQL = $SQL . " LIMIT 1"; //order by naslov $rs_data = mysql_query($SQL) or die(mysql_error()); echo "<!--" . $SQL . " : Jezik je:" . $lang . "-->"; while ($row_data = mysql_fetch_assoc($rs_data)) { $id = (int) $row_data["ID"]; ?> <tr> <td align="center" colspan=3> <h3><?PHP echo $row_data["Title"];?> </h3> </td> </tr> <tr> <td width=20%> </td> <td align="justify" width=60%> <?PHP echo $row_data["tekst"]; // tekst = text, but not keyword ?> </td> <td width=20%> </td> </tr> <?PHP } ?> </table> <p> <?PHP // ****************************** HEEEEELLLLPPPPPP ******************************* // ?> <table border="0" width="90%" cellpadding="1" align="center"> <tr> <td align="center"><a href="?DBid=1<?PHP foreach($_REQUEST as $arr => $elem){ if ( $arr != "DBid") echo "&" . $arr . "=" . $elem; } ?>"> <<</a></td> <td align="center"><a href="?DBid=<?PHP echo $_REQUEST["id"]; foreach($_REQUEST as $arr => $elem){ if ( $arr != "DBid") echo "&" . $arr . "=" . $elem; } ?>" ><</a></td> <?PHP if ( $maxid <= $id ) { ?> <td align="center">></td> <td align="center">>></td> <?PHP } else {?> <td align="center"><a href="?DBid=<?PHP echo $id ; foreach($_REQUEST as $arr => $elem){ if ($arr != "DBid") echo "&" . $arr . "=" . $elem; } ?>">></a></td> <?PHP if ( ($id + 9) < $maxid ) { ?> <td align="center"><a href="?DBid=<?PHP echo $id+10 ; foreach($_REQUEST as $arr => $elem){ if ($arr != "DBid") echo "&" . $arr . "=" . $elem; } ?>">>></a></td> <?PHP } else {?> <td align="center">>></td> <?PHP }?> <?PHP }?> </tr> </table> </form> I have rows being generated in a foreach loop. I need to find the time in the row before it (or maybe after it if my logic is screwed up) to see if the times are equal. If they are equal, I need to see if the next row after that is equal. If it is not equal then stop. mysql user id time action 1 5 bought 1 5 purchased 1 5 sold 1 4 created Code: [Select] foreach($newsfeed as $k => $news) { if ($news['time'] == $news['time'] of $k--) { echo " you bought purchased and created"; } else { echo $news['action']; } } How can I get the next four and previous four rows? Here is what I have and it isn't working at at all. $q1 = Mysql::query("SELECT id, file, dir FROM photos WHERE id > $id AND album = '$al' ORDER BY id ASC LIMIT 4"); $q2 = Mysql::query("SELECT id, file, dir FROM photos WHERE id < $id AND album = '$al' ORDER BY id DESC LIMIT 4"); Hi , i read many articles and tried many things before asking but i came to a dead end. My problem is very simple (or at least i believe so).
In the main directory \httpdocs there is this file we call "header.php" and another file we call "article.php".
So the code of the article is very simple:
<?php include("header.php"); echo "bla bla bla\n"; ?> SQL is made up below, but I have something similar sorted by dates, and NOT by ID. Currently I get this done by getting all the ID's in an array ordered by date and gleaning the two record ID's from that array. if the table is huge, that might not be the best way. Just seems like there should be an easier way to do it. Any ideas? Select startrek.ID, optc.opt_value as trk_series_id, optc.ID as trk_series_idID, startrek.trk_title, startrek.trk_episode, startrek.trk_season, startrek.trk_airdate, startrek.trk_stardate FROM startrek JOIN opt optc ON optc.ID = startrek.trk_series_id ORDER BY trk_airdate ASC
Dear All I am seriously stuck up I am displaying a products for shoppoing cart (using MYSQL and PHP) then clicking on a product displaying thir more images and allow to specify qty rquired (for Shoppng cart) How can dislay prevous and next record of the product ( i believe i ned to remember query defied for previous page but i ma not clear how to display current record and previous record and next record ... I want to display previous and next based on query reun
I want to list the previous (minus) 10 records. echo $rs['tree'];
} Thanks... <html> <?php $id = $_GET['id']; $dbusername="web148-matt"; $dbpassword="matt"; $dbdatabase="web148-matt"; mysql_connect(localhost,$dbusername,$dbpassword); @mysql_select_db($dbdatabase) or die( "Unable to select database"); mysql_query("UPDATE count SET clicks=clicks+1 WHERE id='$id'"); $sql = mysql_query("SELECT link FROM count WHERE id='$id'"); $fetch = mysql_fetch_row($sql); $result = mysql_query("SELECT * FROM count"); while($row = mysql_fetch_array($result)) { echo "<a href=" .$row['link']. ">Link</a>"; } ?> <a href='http://www.google.com'>Google</a> <a href='/index.php?id=2'>link2</a> </html> Hi guyz, I have created an event calender with PHP for customer appointments (calender.php). It is very basic and simple were all the days in the month are clickable; click on the day and you are redirected to diary page (diary.php) to view the appointments for that day. At the moment after the user is done looking at appointment and want to view another, they have to go back to the calender.php page for the next/previous day. I would like a previous and next button (Like the calender on MS Outlook) on the (diary.php page) so I don't have to keep going back to the calender.php page to go to the next or previous day. Please guys help me out, I realise it is quite complicated so any suggestions, ideas, or even if you have a similar script to see how you did it would be much appreciated and very helpful. Thanks guys! Hi guys, Is it possible to make a page automatically redirect to the previous page a user was in once they submit a form? Thanks Hey freaks! I have run into a bit of a snag and need a little guidance from the wise ones. I have a login script that users are redirected to if they land on restricted pages of my site. Initially, I used javascript (go back (-1) to get the user back to the page they were previously on after logging in - which worked fine...but only if they got their login credentials right the first time (which will probably never happen that often). The login form processes itself, so if they get it wrong, it comes back to login.php. I noticed that when I test logging in and purposely enter the wrong information, when I do enter the right info the login page comes back up because it was the previous url. I am not a big fan of JS and I am still fairly new to PHP, so I am not sure how to do this. I tried setting a session for the previous url using http referer (if the url did not equal login.php), but it did not like that at all! Undefined index, headers already sent, you name it...I got the error... I can post my code if need be, but it is just a basic login script (with a lot of Dreamweaver mumbo jumbo) that checks the username and pass in mysql. Any suggestions? Should I use a session for this? Or is it probably one of those switch statements that I am not remotely close in knowledge to writing myself...lol. Or even scarier...a custom function? Any help would be appreciated. Thanks! I'm not sure if this is a pure mySQL question or if it's more PHP but I thought this seemed like a good place to ask it.
I have a table which has data stored into it twice daily. The data stored is the timestamp, an account_ID, and the number of followers.
I am trying to add another field of "gains_losses". This should look at the previous entry for that account_ID, find out the followers data, and then find out the difference between the data being submitted today.
How would you go about doing this? I'm a bit stuck because I can't do it based on the date previously, and the times can change so I can't do a search for "Where time = this minus one day".
Here's a screenie of the table, you can see the account_ID repeats often as these are entries for each of these accounts.
I've got a page that displays all my blog entries on a single page. When you click on a specific entry, it pulls the post ID and loads the page specifically. From there, I wanted to have the option to click either onto the next or previous post by finding the next or previous ID in the database.. however, I know that sometimes posts are deleted, etc. so the ID's won't exactly be in order. What direction should I take with this? I'm trying to figure out how to filter the results of a database query. For example somebody uses a php/javascript form to search for ford cars. When they see 100 results, they then narrow the results by model and/or color. Am I right in thinking that the way to do this is by creating a temporary table of the results? What is the best approach to what I'm trying to do? This topic has been moved to JavaScript Help. http://www.phpfreaks.com/forums/index.php?topic=321154.0 |