title: PHP add value to a existing query string
body: <p>Here is the problem:</p>
  
  <p>I have a page that is all based on query strings, something like: </p>
  
  <pre><code>localhost\sandbox\hotsite.php?id=14665&amp;open=hot_promocoes&amp;titulo=127121
  </code></pre>
  
  <p>And i'm developing a pagination function to this CMS, so i want to do something that do this for me: </p>
  
  <pre><code>localhost\sandbox\hotsite.php?id=14665&amp;open=hot_promocoes&amp;titulo=127121&amp;pg=1
  </code></pre>
  
  <p>Right now, i've created a code that takes the entire URL and add the pg=1, but when I do this and click on some new page link for example page 3, the link looks like this: </p>
  
  <pre><code>localhost\sandbox\hotsite.php?id=14665&amp;open=hot_promocoes&amp;titulo=127121&amp;pg=1&amp;pg=3.
  </code></pre>
  
  <p>And it repeats forever(<code>titulo=127121&amp;pg=1&amp;pg=3&amp;pg=2&amp;pg=4&amp;pg=8&amp;pg=9&amp;pg=2&amp;pg=3</code>) as i click the pages links, and when it happens my pagination code goes crazy, if i continues to click my code just go nuts. I'm stuck at this, I just can't figure a way out of it.</p>
  
  <p>A additional info: Each page has a differente structure so</p>
  
  <ul>
  <li><p>if the content is photos the link looks like this: </p>
  
  <p>localhost\sandbox\hotsite.php?id=14665&amp;open=hot_albums&amp;titulo=127991</p></li>
  <li><p>if the content is videos the link looks like this: </p>
  
  <p>localhost\sandbox\hotsite.php?id=14665&amp;open=videos&amp;album=1348&amp;titulo=11121</p></li>
  </ul>
  
  <p>So i just cant build anything using the build string function from php, cause the only query string value that i know for sure is the id one, that will be on all the pages but the other ones are not constant.</p>
  
  <p>So, i need your help :).</p>
  
  <pre><code>        // START OF FUNCTION
          function stringadd($query_text) {
  
              $query = $query_text;
  
                  $pound = "";
                  $poundPos = -1;
  
                      //Is there a #?
                      if ( ( $poundPos = strpos( $url_atual, "#" ) ) !== false )
                          {
                              $pound = substr( $url_atual, $poundPos );
                              $url_atual = substr( $url_atual, 0, $poundPos );
                          }
  
                  $separator = (parse_url($url_atual, PHP_URL_QUERY) == NULL) ? '?' : '&amp;';
                  $url_atual .= $separator . $query . $pound;
  
            echo "".$url_atual;
          }
        // here is what i do to execute the function:
  
  
  
  &lt;?php echo stringadd('pg=').$total_paginas ;?&gt;  
  </code></pre>