RyanSF07 Posted August 30, 2008 Share Posted August 30, 2008 Hi All, I'm trying to get a pagination script to work correctly. Currently it lists content just fine, but when I click "next page" and then click on one of the listed items, the pagination defaults back to "page 1" instead of displaying the list that supposed to be on "page 2." I'm wondering what are some ideas of fixing this. As is, the content is listed by row id, Order By descending. Thus the links in the list look like this: quiz_start.php?id=$row[id] How do I add the $pagenum variable to this? Do I need to set a Session variable, like: $_SESSION[$pagenum] = $thispage; $pagenum=$thispage;[code] and then GET it with something like: [code]$_SESSION[get] = $thispage; and add that to the url with: quiz_start.php?id=$row[id]&?$thispage Then, would this work: if (!(isset($thispage))) { $pagenum = 1; } OR Would it be better to do something like: $_SESSION[$pagenum] = $_POST[pagenum]; Though this is a long post, I hope you understand what I'm trying to accomplish and may offer ideas that point me in the right direction. I'm new at this, and a little rusty, but I know I'll eventually figure it out. Thank you, Ryan[/code][/code] Quote Link to comment https://forums.phpfreaks.com/topic/121952-solved-questions-about-session-and-get/ Share on other sites More sharing options...
slapdashgrim Posted August 30, 2008 Share Posted August 30, 2008 what is the url of the link? give me the php on that page and i might beable to help more Quote Link to comment https://forums.phpfreaks.com/topic/121952-solved-questions-about-session-and-get/#findComment-629530 Share on other sites More sharing options...
kratsg Posted August 30, 2008 Share Posted August 30, 2008 If you append the page number to the end of the url, something like &page=##, use the $_GET['page'] in order to grab it correctly. What's the code for going to the next page? Let me have a look at it, and I can point out what to append. Quote Link to comment https://forums.phpfreaks.com/topic/121952-solved-questions-about-session-and-get/#findComment-629532 Share on other sites More sharing options...
RyanSF07 Posted August 30, 2008 Author Share Posted August 30, 2008 Thank you for looking at this -- here is the code. Note: this works -- yet doesn't -- as described above. <?php session_start(); include("databaseConnect.php"); require_once("classes/include.all.php"); ?> <html> <head> <link type="text/css" href="styles/rating.css" rel="stylesheet" media="all" /> <script type="text/javascript" src="scripts/prototype.js"></script> <script type="text/javascript" src="scripts/rating.js"></script> </head> <?php $sql = "SELECT video.id, title, description_text, category_text, level_text, pass_text, user_name, DATE_FORMAT(date, '%M %D, %Y') as date FROM video, registered_users WHERE video.user_id = registered_users.id AND video.category_text = 'English' AND video.level_text = 'beginning' AND video.pass_text = 'featured' ORDER BY id DESC"; $query_result = mysql_query($sql); while ($row = mysql_fetch_array($query_result)) { $videoID = $row["id"]; $ratingData = Rating::OutputRating($videoID); $content1 .= " <a class=\"bl\" href = \"quiz_begin.php?id=$row[id]\" target='_self'>$row[title]</a>$ratingData "; } $sql = "SELECT video.id, title, description_text, category_text, level_text, pass_text, user_name, DATE_FORMAT(date, '%M %D, %Y') as date FROM video, registered_users WHERE video.user_id = registered_users.id AND video.category_text = 'English' AND video.level_text = 'beginning' AND video.pass_text = 'new_quiz' ORDER BY id DESC"; $query_result = mysql_query($sql); $rows = mysql_num_rows($query_result); if (!(isset($pagenum))) { $pagenum = 1; } $page_rows = 15; $last = ceil($rows/$page_rows); if ($pagenum < 1) { $pagenum = 1; } elseif ($pagenum > $last) { $pagenum = $last; } $max = 'limit ' .($pagenum - 1) * $page_rows .',' .$page_rows; $sql_p = "SELECT video.id, title, description_text, category_text, level_text, pass_text, user_name, DATE_FORMAT(date, '%M %D, %Y') as date FROM video, registered_users WHERE video.user_id = registered_users.id AND video.category_text = 'English' AND video.level_text = 'beginning' AND video.pass_text = 'new_quiz' ORDER BY id DESC $max"; $query_result2 = mysql_query($sql_p); $rows = mysql_num_rows($query_result2); while ($row = mysql_fetch_array($query_result2)) { $videoID = $row["id"]; $ratingData = Rating::OutputRating($videoID); $content1 .= " <a class=\"bl\" href = \"quiz_begin.php?id=$row[id]\" target='_self'>$row[title]</a>$ratingData "; } $pagination1 = "<b>$pagenum</b> of <b>$last</b>"; $pagination2 = " <b> | </b> "; if ($pagenum == 1) { } else { $previous = $pagenum-1; $lside_pagination7 ="<b> <<< <a href='{$_SERVER['PHP_SELF']}?pagenum=$previous'> Prev </b></a> "; } if ($pagenum == $last) { } else { $next = $pagenum+1; $lside_pagination8 ="<a href='{$_SERVER['PHP_SELF']}?pagenum=$next'><b>Next</a> >>> </b> "; } Database::DeInitialize(); ?> Quote Link to comment https://forums.phpfreaks.com/topic/121952-solved-questions-about-session-and-get/#findComment-629603 Share on other sites More sharing options...
RyanSF07 Posted August 30, 2008 Author Share Posted August 30, 2008 I forgot to add that this file (above) is called by a template. In the template, the variables "pagination1, pagination2, content1, content2" are echoed. That's how the the list and pagination buttons are displayed. Quote Link to comment https://forums.phpfreaks.com/topic/121952-solved-questions-about-session-and-get/#findComment-629708 Share on other sites More sharing options...
compguru910 Posted August 30, 2008 Share Posted August 30, 2008 To make your life a little easier make a variable everytime the pag is called like this $next_page = $_GET['thispage']++; This will give your next page index so that you can make this for the next page link <a href=\"page.php?page={$next_page}\"> Quote Link to comment https://forums.phpfreaks.com/topic/121952-solved-questions-about-session-and-get/#findComment-629714 Share on other sites More sharing options...
RyanSF07 Posted August 30, 2008 Author Share Posted August 30, 2008 I will try it. thanks. Would this be the correct what to write this? (below) or is the & not necessary? <a href=\"content.php?id=$row[id]&?page={$next_page}\"> Quote Link to comment https://forums.phpfreaks.com/topic/121952-solved-questions-about-session-and-get/#findComment-629724 Share on other sites More sharing options...
compguru910 Posted August 30, 2008 Share Posted August 30, 2008 The & is necessary Quote Link to comment https://forums.phpfreaks.com/topic/121952-solved-questions-about-session-and-get/#findComment-629729 Share on other sites More sharing options...
RyanSF07 Posted August 30, 2008 Author Share Posted August 30, 2008 Thank you for your help Compguru910. Does this look correct: Before the isset I'd add the $_GET, like this: $pagenum = $_GET['pagenum']; if (!(isset($pagenum))) { $pagenum = 1; } I'd then change the content url from: $content1 .= " <a class=\"bl\" href = \"quiz_begin.php?id=$row[id]\" target='_self'>$row[title]</a>$ratingData "; } to: $content1 .= " <a class=\"bl\" href = \"quiz_begin.php?id=$row[id]&?page={$pagenum}\" target='_self'>$row[title]</a>$ratingData "; } if I only want the list-content to change when the user clicks Next or Previous and not the page-content, I should change this: <a href='{$_SERVER['PHP_SELF']}?pagenum=$next'><b>Next</a> >>> </b> "; to: <a href='quiz_begin.php?id=$row[id]?pagenum=$next' target='_self'><b>Next</a> >>> </b> "; Am I on the right track? Thanks again Quote Link to comment https://forums.phpfreaks.com/topic/121952-solved-questions-about-session-and-get/#findComment-629744 Share on other sites More sharing options...
compguru910 Posted August 30, 2008 Share Posted August 30, 2008 You are on the right track, but I do not see where you added the $next_page variable. You might want to try that. Quote Link to comment https://forums.phpfreaks.com/topic/121952-solved-questions-about-session-and-get/#findComment-629765 Share on other sites More sharing options...
kratsg Posted August 30, 2008 Share Posted August 30, 2008 Compguru, you cannot simply add to get the next page. You have to figure out exactly HOW many pages there are and use that to limit. Otherwise, you can use a sql injection and make matters worse. Can you show your whole script for the pagination? Not just a portion of it. Quote Link to comment https://forums.phpfreaks.com/topic/121952-solved-questions-about-session-and-get/#findComment-629785 Share on other sites More sharing options...
DarkWater Posted August 30, 2008 Share Posted August 30, 2008 Also, there should only be 1 ? in a URL, not 2. You use & to separate parameters, not &?. Quote Link to comment https://forums.phpfreaks.com/topic/121952-solved-questions-about-session-and-get/#findComment-629787 Share on other sites More sharing options...
kratsg Posted August 30, 2008 Share Posted August 30, 2008 Also, there should only be 1 ? in a URL, not 2. You use & to separate parameters, not &?. He means: http://www.somewebsite.com/somepage.php?var1=val1&var2=val2&var3=val3.... Quote Link to comment https://forums.phpfreaks.com/topic/121952-solved-questions-about-session-and-get/#findComment-629793 Share on other sites More sharing options...
RyanSF07 Posted August 30, 2008 Author Share Posted August 30, 2008 This is what I'm working with now. The names of the variables have changed. Thank you all for your help. I'm still not able to successfully "get" and "pass" the correct page number. Here is how it is currently set up (please pardon or correct me if my terminology is incorrect): Select all and run a row count: $sql = "SELECT video.id, title, description_text, category_text, level_text, pass_text, user_name, DATE_FORMAT(date, '%M %D, %Y') as date FROM video, registered_users WHERE video.user_id = registered_users.id AND video.category_text = 'English' AND video.level_text = 'beginning' AND video.pass_text = 'new_quiz' ORDER BY id DESC"; $query_result = mysql_query($sql); $rows = mysql_num_rows($query_result); (NOTE: Here is were I currently have the GET and Isset fuction. Is this in the right place?) $pagenum = $_GET['pagenum']; if (!(isset($pagenum))) { $pagenum = 1; } $page_rows = 15; $last = ceil($rows/$page_rows); if ($pagenum < 1) { $pagenum = 1; } elseif ($pagenum > $last) { $pagenum = $last; } Set a limit and run the query again: $max = 'limit ' .($pagenum - 1) * $page_rows .',' .$page_rows; $sql_p = "SELECT video.id, title, description_text, category_text, level_text, pass_text, user_name, DATE_FORMAT(date, '%M %D, %Y') as date FROM video, registered_users WHERE video.user_id = registered_users.id AND video.category_text = 'English' AND video.level_text = 'beginning' AND video.pass_text = 'new_quiz' ORDER BY id DESC $max"; $query_result2 = mysql_query($sql_p); $rows = mysql_num_rows($query_result2); while ($row = mysql_fetch_array($query_result2)) { Here is where the template grabs the content variable called $lside_content2 and displays the list of quizzes. Note the url -- I'm trying say display this quiz and also display the correct pagination info. That is, if we're on pagination 3 of 10, show the quiz, and also show that we are on pagination 3 of 10. Currently, the content is displayed, but the pagination goes back to 1 of 10. $lside_content2 .= " <a class=\"bl\" href = \"quiz_begin.php?id=$row[id]&page={$pagenum}\" target='_self'> $row[title]</a>$ratingData "; } This is where the template grabs the pagination text. I'm hoping that once the content urls are working correctly, that I can apply the same format to these Next and Previous urls (content would remain the same, but the pagination list info would change to the next or previous 15 rows in the database. $lside_pagination5 = " <b>$pagenum</b> of <b>$last</b>"; $lside_pagination6 = " <b> | </b> "; if ($pagenum == 1) { } else { $previous = $pagenum-1; $lside_pagination7 ="<b>< <a href='{$_SERVER['PHP_SELF']}?pagenum=$previous'>Prev </b></a> "; } if ($pagenum == $last) { } else { $next = $pagenum+1; $lside_pagination8 ="<a href='{$_SERVER['PHP_SELF']}?pagenum=$next'><b>Next</a> ></b> "; } Database::DeInitialize(); ?> Quote Link to comment https://forums.phpfreaks.com/topic/121952-solved-questions-about-session-and-get/#findComment-629809 Share on other sites More sharing options...
RyanSF07 Posted August 30, 2008 Author Share Posted August 30, 2008 So, I guess my question is. Do I need to set a Session variable? If so, how and where? That's another thing I need help with. This last chunk of code is the most recent. Please disregard all previous chunks as the variable names changed. Thank you again for your help with this. Quote Link to comment https://forums.phpfreaks.com/topic/121952-solved-questions-about-session-and-get/#findComment-629834 Share on other sites More sharing options...
RyanSF07 Posted August 30, 2008 Author Share Posted August 30, 2008 I tried adding this below the session_start: $_SESSION[$pagenum] = $_GET['page']; That didn't work. So I also tried: $_SESSION[$pagenum] = $_GET['pagenum']; and: $_SESSION['$pagenum'] = $_GET['page']; any thoughts on what I'm missing or doing wrong? The url again that is trying to pass the &pagenum variable is: <a class=\"bl\" href = \"quiz_begin.php?id=$row[id]&page={$pagenum}\" target='_self'>$row[title]</a>$ratingData "; } I've also tried: <a class=\"bl\" href = \"quiz_begin.php?id=$row[id]&page=$pagenum\" target='_self'> $row[title]</a>$ratingData "; } this is quite a puzzle... Quote Link to comment https://forums.phpfreaks.com/topic/121952-solved-questions-about-session-and-get/#findComment-629854 Share on other sites More sharing options...
RyanSF07 Posted August 30, 2008 Author Share Posted August 30, 2008 Success I figured it out. I deleted the Session stuff and changed the isset to: if(isset($_GET['pagenum'])) { $pagenum = $_GET['pagenum']; } else { $pagenum = 1; } and the url to: a class=\"bl\" href = \"quiz_begin.php?id=$row[id]&pagenum=$pagenum\" target='_self'> $row[title]</a>$ratingData "; } thanks for the help and ideas guys! Quote Link to comment https://forums.phpfreaks.com/topic/121952-solved-questions-about-session-and-get/#findComment-629862 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.