Jump to content

url variables don't discard between pages


V

Recommended Posts

I'm trying to make a "sort by" script to sort posts. I'm hoping someone can help me figure out a small glitch. For some reason I'm getting multiple variables in the url as if the page doesn't reload.  :-\

 

The code is

 

$uri = $_SERVER['REQUEST_URI']; //gets url of current page

      <li><a href="<?php echo $uri; ?>&sort=New">Most Recent</a></li>
        
        <li><a href="<?php echo $uri; ?>&sort=Popular">Popular</a></li>
        
        <li><a href="<?php echo $uri; ?>&sort=Discussed">Discussed</a></li>

 

if for example I choose to sort by "Popular" the url output will be

 

http://localhost/mysite/single_cat.php?cat=57&sort=Popular

 

but then when I click let's say "Discussed" a new sort variable is added with the existing one.

 

http://localhost/mysite/single_cat.php?cat=57&sort=Popular&sort=Discussed

 

 

and below I'm querying posts based on the sort variable.. for example..

 

if ($sort == "Popular") {

      //db connection

$sql = "SELECT * FROM posts WHERE cat_id = '$cat' AND views >= 2 ORDER BY views DESC, post_date DESC";	

     //while loop, etc

}//end else if

 

 

I'm not sure what I did wrong.. How do I get just one "?sort=" url variable?

 

 

I'd first unset $_GET['sort'], then use implode() with the $_GET array to make the variable string after the ?, minus the sort. Then add it to your page URL. That is your desired link. (plus the new sort type)

 

However, if you have ?sort=a&sort=b, $_GET['sort'] should ONLY be b.

Sorry, I really appreciate your help, I know about the functions you suggested but I don't understand how to use them in this type of situation. If I first unset $sort, I get immediate undefined variable errors. Then when I click the first sorting option I won't have an array for implode because I'll have just 1 sort in the url.

 

I tried using the two but I get "forbidden" and undefined errors.

Archived

This topic is now archived and is closed to further replies.

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