Page 1 of 1

Variable reset between two php pages

Posted: Wed Jun 20, 2007 10:26 am
by yoda69
Everah | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


hey,

I'm trying to pass 1 variable to another page ($id) in order to display more information from a database. The variable passes great and the 2'nd page works fine.

However, the second page have link in the table header, allowing the user to order the results (asc or desc). when they press this link (which leads them to the same page) the $id variable losses it value and basically they get zero fields......

I don't really know what should be the best way to overcome this. I thought maybe about using session varibles, but I don't have so much experince with this and I was hoping you could point me in this direction or any other solutions you might find appropriate.

Here's the code for the first page:

Code: Select all

<?PHP
$db = mysql_connect("localhost","root","test") or die("Problem connecting");
mysql_select_db("education") or die("Problem selecting database");
$query = "SELECT * From teachers";
$result = mysql_query($query) or die ("Query failed");
//let's get the number of rows in our result so we can use it in a for loop
$numofrows = mysql_num_rows($result);

echo "<TABLE class=\"maintable\" BORDER=1 frame=box width=100% CELLPADDING=0 CELLSPACING=1 >\n";
echo "<TR bgcolor=\"lightblue\">
<TD>Teacher</TD>
</TR>\n";
for($i = 0; $i < $numofrows; $i++) {
$row = mysql_fetch_array($result); //get a row from our result set
if($i % 2) { //this means if there is a remainder
echo "<TR bgcolor=\"#CCFFCC\">\n";
} else { //if there isn't a remainder we will do the else
echo "<TR bgcolor=\"#BFD8BC\">\n";
}
echo"
<TD><a href='bymaster.php?id={$row[id]}'>".stripslashes($ row[teacher_name])."</a></TD>
\n";
echo "</TR>\n";
}
//now let's close the table and be done with it
echo "</TABLE>\n";
?>
Here's the code for the second page:

Code: Select all

<?PHP


$db = mysql_connect("localhost","root","test") or die("Problem connecting");
mysql_select_db("education") or die("Problem selecting database");

//set a direction for the search. If none is set, we'll choose ASC
if(!isset($_GET['direction'])){
$direction = 'ASC';
}else{
$direction = $_GET['direction'];
}
//set the newdirection as the opposite to the old
if($direction=="ASC"){
$newdirection= "DESC";
}else{
$newdirection = "ASC";
}
if(isset($_GET['orderby'])){
$query = "SELECT * FROM classes, teachers, types WHERE (classes.teacher_id=teachers.id AND classes.discipline_id=classes.id AND teachers.id=$id) ORDER BY '$_GET[orderby]' $direction";
}else{
$query = "SELECT * FROM classes, teachers, types WHERE (classes.teacher_id=teachers.id AND classes.discipline_id=classes.id AND teachers.id=$id)";
}

$result = mysql_query($query) or die ("Main Query failed");
//let's get the number of rows in our result so we can use it in a for loop
$numofrows = mysql_num_rows($result);

echo "<TABLE class=\"maintable\" BORDER=1 frame=box width=100% CELLPADDING=0 CELLSPACING=1 >\n";
echo "<TR bgcolor=\"lightblue\">
<TD><a href='".$_SERVER["PHP_SELF"]."?orderby=start_date& direction=".($newdirection)."'>Start Date</a></TD>
<TD><a href='".$_SERVER["PHP_SELF"]."?orderby=title&direc tion=".($newdirection)."'>Title</a></TD>
<TD>Type</TD>
<TD>Teacher</TD>
</TR>\n";
for($i = 0; $i < $numofrows; $i++) {
$row = mysql_fetch_array($result); //get a row from our result set
if($i % 2) { //this means if there is a remainder
echo "<TR bgcolor=\"#CCFFCC\">\n";
} else { //if there isn't a remainder we will do the else
echo "<TR bgcolor=\"#BFD8BC\">\n";
}
echo"
<TD>".$row['start_date']."</TD>
<TD><a href='/showall.php?primary_index={$row[primary_ind ex]}'>".stripslashes($row[title])."</a></TD>
<TD>".$row['type_name']."</TD>
<TD>".$row['teacher_name']."</TD>
\n";
echo "</TR>\n";
}
//now let's close the table and be done with it
echo "</TABLE>\n";
mysql_free_result($result);
?>

Everah | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]

Re: Variable reset between two php pages

Posted: Wed Jun 20, 2007 10:33 am
by volka
here
yoda69 wrote:<TD><a href='bymaster.php?id={$row[id]}'>".stripslashes($ row[teacher_name])."</a></TD>
you're passing the id through the url.
Here
yoda69 wrote:<TD><a href='".$_SERVER["PHP_SELF"]."?orderby=start_date& direction=".($newdirection)."'>Start Date</a></TD>
<TD><a href='".$_SERVER["PHP_SELF"]."?orderby=title&direc tion=".($newdirection)."'>Title</a></TD>
you don't

Posted: Wed Jun 20, 2007 11:11 am
by yoda69
tnx for your quick reply.

Problem is that even if i add this variable to the second link, hence:

<TD><a href='".$_SERVER["PHP_SELF"]."?id={$row[id]}&orderby=start_date&direction=".stripslashes($newdirection)."'>Start Date</a>

i get a no results in the query.
upon using the "print" $query i get:

SELECT * FROM classes, teachers, types WHERE (classes.teacher_id=teachers.id AND classes.discipline_id=classes.id AND teachers.id='') ORDER BY 'start_date' DESC


from some reason the $id variable that was passed before is empty now...
any idea?

Posted: Wed Jun 20, 2007 11:22 am
by Begby
try $_GET['id'] instead of $row['id']

Posted: Wed Jun 20, 2007 11:26 am
by arturm
instead of

Code: Select all

<TD><a href='".$_SERVER["PHP_SELF"]."?id={$row[id]}&orderby=start_date&direction=".stripslashes($newdirection)."'>Start Date</a>
try

Code: Select all

<TD><a href='".$_SERVER["PHP_SELF"]."?id=".$_GET['id']."&orderby=start_date&direction=".stripslashes($newdirection)."'>Start Date</a>
on a second page id is no longer inside $row array but in $_GET

Posted: Wed Jun 20, 2007 11:30 am
by yoda69
it works!!!!

thanks a million.

any idea why it works now? i mean what is actually different between the two methods?
would like to know that for my own understanding of php.

Posted: Wed Jun 20, 2007 11:50 am
by arturm
$row was an array you got from your mysql query
$_GET is an array of variables passed in url

Posted: Wed Jun 20, 2007 11:58 am
by yoda69
Got it.

thanks again for your support guys, been really really helpful

:D

Posted: Wed Jun 20, 2007 11:20 pm
by feyd
You can leave out PHP_SELF in the case artum posted above. Likely, it isn't needed in all cases.

If you need the URL of the current page, for some reason, it needs to be generated as the data in PHP_SELF is susceptible to injection attacks.