Jump to content

Make the GET Automatically Decide to Use ? or &


chaseman

Recommended Posts

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&current_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&current_page=$next_page'>></a> ";
	echo " <a href='{$filename}?user=$user_name_get&current_page=$total_pages'>>></a> ";

} elseif (($filename == $index) && isset($select_category)) {
	echo " <a href='{$filename}?sort_category=$select_category&sortSubmit=submit&current_page=$next_page'>></a> ";
	echo " <a href='{$filename}?sort_category=$select_category&sortSubmit=submit&current_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?

 

Link to comment
Share on other sites

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"].'&current_page='.$next_page.'">></>';
}

?>

Link to comment
Share on other sites

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"].'&current_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?&current_page=2&current_page=3&current_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.

Link to comment
Share on other sites

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).'&current_page='.$next_page.'">></>';
}
?>

This only works with PHP5 though (because of http_build_query)

Link to comment
Share on other sites

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) . "&current_page=$next_page'>></a> ";
	echo " <a href='{$filename}?" . $http_build_query($_GET) . "&current_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)&current_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?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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