V Posted July 27, 2010 Share Posted July 27, 2010 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? Quote Link to comment https://forums.phpfreaks.com/topic/209039-url-variables-dont-discard-between-pages/ Share on other sites More sharing options...
Jessica Posted July 27, 2010 Share Posted July 27, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/209039-url-variables-dont-discard-between-pages/#findComment-1091807 Share on other sites More sharing options...
V Posted July 27, 2010 Author Share Posted July 27, 2010 Hello jesirose, thanks for the reply! Can you please share an example? I'm not sure how to do that. Quote Link to comment https://forums.phpfreaks.com/topic/209039-url-variables-dont-discard-between-pages/#findComment-1091808 Share on other sites More sharing options...
Jessica Posted July 27, 2010 Share Posted July 27, 2010 Hello jesirose, thanks for the reply! Can you please share an example? I'm not sure how to do that. See below. Quote Link to comment https://forums.phpfreaks.com/topic/209039-url-variables-dont-discard-between-pages/#findComment-1091809 Share on other sites More sharing options...
V Posted July 27, 2010 Author Share Posted July 27, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/209039-url-variables-dont-discard-between-pages/#findComment-1091871 Share on other sites More sharing options...
Jessica Posted July 28, 2010 Share Posted July 28, 2010 So what's the next step? Quote Link to comment https://forums.phpfreaks.com/topic/209039-url-variables-dont-discard-between-pages/#findComment-1092362 Share on other sites More sharing options...
V Posted July 28, 2010 Author Share Posted July 28, 2010 I wouldn't inquire for help if I knew... I found a totally different alternative however. Quote Link to comment https://forums.phpfreaks.com/topic/209039-url-variables-dont-discard-between-pages/#findComment-1092364 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.