Page 5 of 6

Posted: Mon Jun 07, 2004 9:45 am
by magicrobotmonkey
No, because of the design of session variables, you only need the sort to be set to main in the url once and it will remain that way until it is set to something else.

as for your errors, session_start() needs to be at the very beginning of your script. If this script is in an include, put it at the top of whatever script is including it. It can have no whitespace before it. if this is part of an html file part way down, open your php tags at the very beginning, session_start() and close them. http://us3.php.net/manual/en/function.session-start.php

also, additional code changes:

Code: Select all

<?php
$query = "SELECT Name,Phone,Email FROM contact "; 
    
   session_start();//move to beginning of code!!
    
   $_SESSION['sortMe']="name";     

   if(isset($_GET['sort']))    { 
     $_SESSION['sortMe']=$_GET['sort']; 
     $query.="ORDER BY $_SESSION[sortMe]"; //changed to sortMe
    }  

?>

Posted: Mon Jun 07, 2004 10:20 am
by Kingo
I dont get any errors if i put the session_start(); at the beginning of my code. But, in the second page the sort order is not maintained.

Posted: Mon Jun 07, 2004 10:24 am
by magicrobotmonkey
oh yea i see the mistake in the logic:

Code: Select all

<?php
   $_SESSION['sortMe']="name"; // default
    
   if(isset($_GET['sort'])) { 
      $_SESSION['sortMe']=$_GET['sort']; 
    }  
  
   $query.="ORDER BY $_SESSION[sortMe]"; //moved from if statement

?>
Now, it will default to sorting by whatever you set it to up at the top there but it will continue to hold the changes now. I think you should have been able to figure this out. It was only adding the ORDER BY clause to the query when get was set so by moving it outside of the if it will always be added.

Posted: Mon Jun 07, 2004 12:43 pm
by Kingo
I was able to sort it on all pages withgoit using the SESSIONS.
I did it in this way

Code: Select all

if ($screen > 0) { 
	$url = "view.php?sort=".$sort."&screen=" . ($screen - 1); 
	echo "<a href="$url"><font size="2" face="Arial, Helvetica, sans-serif">Previous</font></a>\n"; 
	} 
	// page numbering links now 
	for ($i = 0; $i < $pages; $i++) { 
	$url = "view.php?sort=".$sort."&screen=" . $i; 
	echo " | <a href="$url"><font size="2" face="Arial, Helvetica, sans-serif">$i</font></a> |"; 
	} 
	
	if ((($screen+1)*$rows_per_page)< $total_records)
	{ 
	  if ($screen < $pages)
	   { 
		$url = "view.php?sort=".$sort."&screen=" .($screen + 1); 
		echo "<a href="$url"><font size="2" face="Arial, Helvetica, sans-serif">Next</font></a>\n"; 
	   } 
	}
But I'm not able to retain the selected item. I mean when I select the "Name" from the drop down list. I should get the RELOADED Page with the Name option Selected.
I guess I should write some IF conditions in this code. But Donot know how to do it.
<form name="frm1" action="POST">
<select name="select" size="1" OnChange="location.href=this.options[this.selectedIndex].value" style="font-family: Arial, Helvetica, sans-serif; font-size:11px">

<option selected >---------</option>
<option value="view.php?sort=name">Name</option>
<option value="view.php?sort=phone">Phone</option>
<option value="view.php?sort=email">Email</option>
</select></form>

Posted: Mon Jun 07, 2004 1:03 pm
by magicrobotmonkey
that's what I was recommending the use of sessions for - to retain the selected items. Its a perfect use of sessions and IMHO will keep your code much more simple!

Posted: Mon Jun 07, 2004 1:07 pm
by Kingo
Your code uisng the sessions is working only for the "Name", How do i use it for Phone and email.

Posted: Mon Jun 07, 2004 1:46 pm
by magicrobotmonkey
name was the default. keep your dropdown how it is and if it passes sort=whatev in the url it will change what it's sorting to be that until you pass it again.

Posted: Mon Jun 07, 2004 2:20 pm
by Kingo
Only the "Name" is getting sorted on all pages.
Email and Phone are getting sorted only on the first page and the rest of the pages for phone and email are not sorted. And the STATE is not retaining, I mean when I select "Name", on the reloaded page i dont see "Name" as selected.

Posted: Tue Jun 08, 2004 6:21 am
by magicrobotmonkey
oh, yea! Sh*T i made a mistake! But its something else you should have caught! READ OVER THE CODE DON"T JUST POST RIGHT AWAY WHEN IT DOESN"T WORK!

Code: Select all

<?php
   //$_SESSION['sortMe']="name"; drop this line    

   if(isset($_GET['sort'])) { 
      $_SESSION['sortMe']=$_GET['sort']; 
    }
  
  if(isset($_SESSION['sortMe'])){
    $query.="ORDER BY $_SESSION[sortMe]";
  }

?>

Thanx

Posted: Tue Jun 08, 2004 2:37 pm
by Kingo
Hello,
thanx . I got it. But inspite of using the sessions , I'm not able to retain the state in the LIST box. I mean , when i click Name, the reloaded page does not have Name as selected.
Thanx

Posted: Tue Jun 08, 2004 2:46 pm
by feyd
you need to add "selected" to the drop down when a sorting method is used if you want that.

Posted: Tue Jun 08, 2004 2:49 pm
by Kingo
I used the following code
<form name="frm1" method="POST" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<select name="select" size="1" OnChange="location.href=this.options[this.selectedIndex].value" style="font-family: Arial, Helvetica, sans-serif; font-size:11px">
<option selected >---------</option>
<option value="view3.php?sort=name">Name</option>
<option value="view3.php?sort=phone">Phone</option>
<option value="view3.php?sort=email">Email</option>
</select></form>

For default i used selected for ----------

Posted: Tue Jun 08, 2004 2:55 pm
by feyd
you'll need something similar to:

Code: Select all

<form name="frm1" method="POST" action="<?php echo $_SERVER&#1111;'PHP_SELF']; ?>"> 
<select name="select" size="1" OnChange="location.href=this.options&#1111;this.selectedIndex].value" style="font-family: Arial, Helvetica, sans-serif; font-size:11px"> 
<option<?php if(!isset($_GET&#1111;'sort'])) echo ' selected '; ?>>---------</option> 
<option value="view3.php?sort=name"<?php if(isset($_GET&#1111;'sort']) && $_GET&#1111;'sort'] == 'name') echo ' selected '; ?>>Name</option> 
<option value="view3.php?sort=phone"<?php if(isset($_GET&#1111;'sort']) && $_GET&#1111;'sort'] == 'phone') echo ' selected '; ?>>Phone</option> 
<option value="view3.php?sort=email"<?php if(isset($_GET&#1111;'sort']) && $_GET&#1111;'sort'] == 'email') echo ' selected '; ?>>Email</option> 
</select></form>

Posted: Tue Jun 08, 2004 4:02 pm
by Kingo
Thanx very much.It works. But there is a problem. On the second page the state is not maintained. I mean When i select Name, it is selected only on the first page when i click the Next link, i still see the "-------" as selected. Your help is really appreciated.

Posted: Tue Jun 08, 2004 4:06 pm
by feyd
are you passing the sort parameter to the second page?