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?

 

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.'">></>';
}

?>

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?

 

 

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.

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)

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?

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.