chaseman Posted February 27, 2011 Share Posted February 27, 2011 This is an example of the forwarding section of the pagination code: if (($filename == $profile) && isset($select_category)) { echo " <a href='{$filename}?user=$user_name_get&sort_category=$select_category&sortSubmit=submit¤t_page=$next_page'>></a> "; echo " <a href='{$filename}?user=$user_name_get&sort_category=$select_category&sortSubmit=submitcurrent_page=$total_pages'>>></a> "; } elseif ($filename == $profile) { echo " <a href='{$filename}?user=$user_name_get¤t_page=$next_page'>></a> "; echo " <a href='{$filename}?user=$user_name_get¤t_page=$total_pages'>>></a> "; } elseif (($filename == $index) && isset($select_category)) { echo " <a href='{$filename}?sort_category=$select_category&sortSubmit=submit¤t_page=$next_page'>></a> "; echo " <a href='{$filename}?sort_category=$select_category&sortSubmit=submit¤t_page=$total_pages'>>></a> "; } else { echo " <a href='{$filename}?current_page=$next_page'>></a> "; echo " <a href='{$filename}?current_page=$total_pages'>>></a> "; } $filename = the opened filename + extension $profile = profile.php $index = 01.php I'm using several if and elseif statements to not have the pagination clash with the other GET data. I'd like to have the pagination simply append at the end of the URL with an ampersand if there is GET data already. If there's no GET data at all I'd like to have it append with a question mark. The problem with the solution I have now is, if I use more GET data in the future I'll have to use more elseif statements it will get out of hand. I've noticed that if you send GET data over a form that has method on GET, then the GET data will automatically decide if to add a question mark or an ampersand. In that sense my question would be, is there a way to make the pagination decide automatically to use a question mark or an ampersand? Quote Link to comment Share on other sites More sharing options...
flolam Posted February 27, 2011 Share Posted February 27, 2011 Hi you could use $_SERVER["QUERY_STRING"] <?php if ($_SERVER["QUERY_STRING"] == "") { echo '<a href="'.$filename.'?current_page='.$next_page.'">></a>'; } else { echo '<a href="'.$filename.'?'.$_SERVER["QUERY_STRING"].'¤t_page='.$next_page.'">></>'; } ?> Quote Link to comment Share on other sites More sharing options...
chaseman Posted February 27, 2011 Author Share Posted February 27, 2011 when I echo out $_SERVER['QUERY_STRING'] I don't get anything, what does it do? Btw, where can I find all the $_SERVER super global variants, I can't find them in the PHP manual? Quote Link to comment Share on other sites More sharing options...
Jessica Posted February 27, 2011 Share Posted February 27, 2011 when I echo out $_SERVER['QUERY_STRING'] I don't get anything, what does it do? Btw, where can I find all the $_SERVER super global variants, I can't find them in the PHP manual? Is there a query string in your url? Quote Link to comment Share on other sites More sharing options...
flolam Posted February 27, 2011 Share Posted February 27, 2011 The php manual lists them (http://php.net/manual/en/reserved.variables.server.php) but I find it easier to do print_r($_SERVER) this also shows you the values. Quote Link to comment Share on other sites More sharing options...
chaseman Posted February 27, 2011 Author Share Posted February 27, 2011 Hi you could use $_SERVER["QUERY_STRING"] <?php if ($_SERVER["QUERY_STRING"] == "") { echo '<a href="'.$filename.'?current_page='.$next_page.'">></a>'; } else { echo '<a href="'.$filename.'?'.$_SERVER["QUERY_STRING"].'¤t_page='.$next_page.'">></>'; } ?> Ok I tried your suggestion and it works so far without problem, the only thing I'm wondering is if I click FORWARD I get this: 01.php?¤t_page=2¤t_page=3¤t_page=4 It will simply append on what is already there. And I'm wondering if this is common practice? I'm thinking if the user may scroll through 20 or 30 pages, the URL may become way too long, could that cause a problem? Thanks for the tip though. Quote Link to comment Share on other sites More sharing options...
flolam Posted February 27, 2011 Share Posted February 27, 2011 Sorry I didn't think of that. How about <?php unset($_GET["current_page"]); if (count($_GET) == 0) { echo '<a href="'.$filename.'?current_page='.$next_page.'">></a>'; } else { echo '<a href="'.$filename.'?'.http_build_query($_GET).'¤t_page='.$next_page.'">></>'; } ?> This only works with PHP5 though (because of http_build_query) Quote Link to comment Share on other sites More sharing options...
chaseman Posted February 27, 2011 Author Share Posted February 27, 2011 Thanks that one works great, I can tell you're a skilled programmer Quote Link to comment Share on other sites More sharing options...
chaseman Posted February 27, 2011 Author Share Posted February 27, 2011 I noticed a small problem though. I'm getting following error: Fatal error: Function name must be a string if (count($_GET) == 0) { echo " <a href='{$filename}?current_page=$next_page'>></a> "; echo " <a href='{$filename}?current_page=$total_pages'>>></a> "; } else { echo " <a href='{$filename}?" . $http_build_query($_GET) . "¤t_page=$next_page'>></a> "; echo " <a href='{$filename}?" . $http_build_query($_GET) . "¤t_page=$total_pages'>>></a> "; } The error message is referencing to the $http_build_query part. If I click a link on the pagination then the URL will look like this: profile.php?(Array)¤t_page=2 I have the code inside function, though it should work nevertheless since it's over GET. And I do have PHP 5 btw. Any idea why I'm encountering this problem? Quote Link to comment Share on other sites More sharing options...
flolam Posted February 27, 2011 Share Posted February 27, 2011 no $ in front of http_build_query, it's a function, not a variable Quote Link to comment Share on other sites More sharing options...
chaseman Posted February 27, 2011 Author Share Posted February 27, 2011 Oh damn I didn't even notice that (I was thinking of $_SERVER), that was stupid me. Thanks for the help man, you rescued my day! Quote Link to comment 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.