Jump to content

[SOLVED] simple for you .. difficult for me


n8w

Recommended Posts

okay .. I am doing pagination .. but I am adding a &page=$current_page to the query string so I am getting urls that keep adding more and more pages

 

http://www.illustrationmundo.com/audio_index.php?&page=1&page=2&page=3

 

How can I change my code so it just replaces the page vs. adding it?

 

Thanks a ton

 

$q=$_SERVER['QUERY_STRING'];
$q =str_replace(array('&', '&'), array('&', '&'),$q);

$counter = 1;
while ($counter <= $num_pages) {
?>
    <? if(($counter-1)==$page ){?>
(<?= $counter ?>) | 
    <? }else {?>
<a href="http://www.illustrationmundo.com/articles_index.php?<?= $q ?>&page=<?= $counter-1 ?>"> <?= $counter ?></a> | 
    <? }?>
    <?

$counter++;
}
}

 

 

Link to comment
https://forums.phpfreaks.com/topic/83022-solved-simple-for-you-difficult-for-me/
Share on other sites

just a quick note if you are keeping the link

 

http://www.illustrationmundo.com/audio_index.php?&page=1&page=2&page=3

 

that will not work it should be

 

http://www.illustrationmundo.com/audio_index.php?page=1&page=2&page=3

 

the first $_GET value is just put after the question mark. However all URL passed information after that is "added" to the list with the use of &

 

for example

 

www.domain.com/index.php?test=1&test=2

You're capturing the current query string in $q and adding it to the URL, as well as adding &page=$current_page.

 

So if $q already has page in it you'll just be adding it again, make sense?

 

Hey thanks for replying

 

.. yes that does make sense .. but I don't know how to fix it because I need the query string for the other parameters .. but I need to replace the page variable with then new one ...

 

how would you recomment doing that .. say I have the query string ..but I need to tell it .. its now page 7

 

thanks

You could just rebuild it from all the $_GET values:

 

$newQueryString = Array();
foreach($_GET as $key => $val){
  if($key == 'page'){
    $val = $newPage;
  }
  $newQueryString[] = $key . '=' . $val;
}
$newQueryString = implode('&', $newQueryString);
echo $newQueryString;

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.