Please see the attached file. Its a link class designed to do exaclty what you have asked. I created it a little while ago and it has come in very handy.
Heres an example of how it is used:
<?php
include ('link.inc');
/*
initialize the class with the link target
a new class should be created for each new target
*/
$link = new link("myScript.php");
// add some common arguments
$link->common_args = Array("imgID","root","imgNo");
// add a variable that will be prepended to each link
$link->prepend_add(Array("sesID" => "123456789"));
// add a variable that will be appended to every link
$link->append_add(Array("current" => "me"));
/*
disply the query string. The URL function must have as many
arguments as there are common arguments. In this example
imgID,root,imgNo will have the valus of the first four
arguments
*/
echo($link->URL("4","/images","66"));
echo("</br>");
// do the same but add some extra variables to the query string
echo($link->URL("10","/cars","23",Array("extra" => "234", "ingName" => "ferrari")));
echo("</br>");
// add to a link
echo("<a href=\"" . $link->URL("10","/cars","23",Array("extra" => "234", "ingName" => "ferrari")) .
"\">Test Link</a>");
?>