Jump to content

[SOLVED] Questions about session and get


RyanSF07

Recommended Posts

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]

Link to comment
Share on other sites

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();
?>

 

Link to comment
Share on other sites

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

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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();
?>

Link to comment
Share on other sites

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.

 

 

Link to comment
Share on other sites

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...

Link to comment
Share on other sites

Success  ;D  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!

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.