1) Can i change only id in url: game.php?id=1&screen=ranking.

2) With tutorial i make mod rewrite site, it make url index.php/1/edit then it check url with regex which is in array, there is also info about page as template which to load. Is thise method good, aney better? Is possible make mod rewrite which server would understand like index.php?id=1&mode=edit but browser would show index.php/1/edit?

    Yes you can change only the ID in the url. You can change anything you want in the query-string by interacting with the $_GET superglobal array.

    If you understand how mod_rewrite works, and you're comfy with php regular expressions, you can make the "router" (your index.php script) do anything you want. You could allow them to use urls like index.php/var:val/var2:value/var3:value2 or keep it as simple as your first example.

    The mod rewrite you're looking for to change index.php/1/edit into index.php?id=1&mode=edit can't be done in Apache(not without a really intricate pattern). So instead you'd use something like this:

    RewriteEngine On
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule (.*) /index.php?url=$1 [QSA,L]

    This will route any url requested for this site to index.php IF AND ONLY IF the requested file is not a directory, and the requested file is not a real file on the system (images, pdfs, documents, stylesheets, javascript, etc.). So then you can use index.php to look at the $_GET['url'] variable and see what exactly was passed to it. You can use regular expressions to parse it and even default values for things that are missing that shouldn't be.

      By changing $GET["id"] = 3; url is same but it does change id, are pages on which url is same (until open in new taw) make thise way?
      In dropdown menu should be links taken from current url + change ids(mysql), then all links in page should have this new id taken from url.
      On some links i even add some more stuff is ok thise way $
      SERVER['REQUEST_URI'] ."&some=value";.

        You can't really change the URL, only how the url is interpreted in your script. To change the URL, you'd have to build the url with items from the $GET superglobal array. For example, if you change $GET['id'] to "5", then when you go to create your links, you could do:

        $link = '/path/to/some/script.php?';
        foreach($_GET as $var => $val)
        {
            $link .= $var.'='.urlencode($val).'&';
        }
        $link = substr($link, 0, -1); // Strip the trialing '&'
        
        echo $link;

        So then you'd have the "new" id instead of the old.

          Thank you very much :p very nice code. I need your help one more time (for this example of course) i come as far as i can. Trouble is making array sign => and unknown [0] => everything else should work.

          $change = "screen=WOOOOOOORK,id=6"; //example number of parameters which change
          
          function url($change=FALSE){
          
          $url = 'http://localhost/game/game.php?';
          
          $change = str_replace("=", '" => "', $change);
          $change = str_replace(",", '","', $change);
          $change = '"'. $change .'"';// output //"screen" => "WOOOOOOORK","id" => "6"
          $change = array($change);
          print_r($change);// Array ( [0] => "screen" => "WOOOOOOORK","id" => "6" ) //too much [0] =>
          
          foreach($_GET as $var => $val){
          foreach ($change as $temp => $value){
          				//echo $temp.$value;
          				if ($var == $temp) $val = $value;
          			}
          	    $url .= $var.'='.urlencode($val).'&';
          	}
          $url = substr($url, 0, -1); // Strip the trialing '&'
          return $url;
          }

            That's because it's a string. You're just putting the string "screen" => "WOOOOORK", "id" => "6" as the first element of the array. What you need to do is to manually parse it in one of two ways.

            1. Explode on the "," so that every element is in {var}={val} format (or just a variable name).

            2. Use [man]eval/man to evaluate the code to an array.

            Since #2 uses the eval method (which in 90% of cases is the wrong way to do it), I'll give you an example of #1.

            <?php
            
            $_GET = array(
            	'screen' => 'Something Else',
            	'id' => 72,
            	'var' => 'some value',
            	'test' => "{something:'json encoded'}"
            );
            $change = "screen=WOOOOORK,id=6,var";
            
            function url($change=false)
            {
            	$url = 'http://localhost/game/game.php?';
            
            $tmp = explode(',', $change);
            
            $newChange = array();
            foreach($tmp as $item)
            {
            	$vals = explode('=', $item);
            
            	$newChange[$vals[0]] = (isset($vals[1]) ? $vals[1] : null);
            }
            
            foreach($_GET as $var=>$val)
            {
            	if(array_key_exists($var, $newChange))
            	{
            		$val = $newChange[$var];
            	}
            
            	$url .= $var .'='.urlencode($val).'&';
            }
            $url = substr($url, 0, -1); // Strip the trailing '&'
            return $url;
            }
            
            echo url($change)."\n";

            I had to manually set $_GET; however, this is just a test case. It will spit out:

            http://localhost/game/game.php?screen=WOOOOORK&id=6&var=&test=%7Bsomething%3A%27json+encoded%27%7D

            Hope that helps.

              Write a Reply...