Variable reset between two php pages
Posted: Wed Jun 20, 2007 10:26 am
Everah | Please use
Here's the code for the second page:
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";
?>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]