Page 1 of 1

How to read the data after the "?" (e.g. index.php?x=off) ?

Posted: Tue Jan 06, 2009 5:53 pm
by helioslive
I've got a very simple script to chage a value on a XML file, that will be later read by a Visual Basic program. I got everything working, but i want to optimize.

Let's give an example of the code:

Code: Select all

<?php
if (file_exists('1.xml')) {
    $xml = simplexml_load_file('1.xml');
 
} else {
    exit('Error al abrir test.xml.');
}
 
[b]  [size=200]$xml->z[x] = 'y'; [/size][/b]
 
  echo $xml->asXML(); 
 
 $xml->saveXML('1.xml'); // save as file 
?>
Let´s say i want those variables: "X,Y,Z" to change based on what you see after the "?" before the webpage url. (e.g. http://server.com/thing.php?x=0&y=Off&z=state)

How is it done?
Any ideas? examples?

Thanks! =D

PS.- Sorry! I didn't noticed the PHP - Code Section... xD

Re: How to read the data after the "?" (e.g. index.php?x=off) ?

Posted: Tue Jan 06, 2009 6:05 pm
by Syntac

Re: How to read the data after the "?" (e.g. index.php?x=off) ?

Posted: Tue Jan 06, 2009 6:18 pm
by Apollo
For some reason you can't do this directly. But with a function using byref syntax, it works fine.

For example, call this script with bla.php?x=test&y=bye&z=myArray

Code: Select all

<?php
 
function ChangeArray( &$arr, $key, $value )
{
    $arr[$key] = $value;
}
 
$myArray = array('test'=>'hello');
 
print_r($myArray); // output: Array ( [test] => hello ) 
 
$x = $_GET['x'];
$y = $_GET['y'];
$z = $_GET['z'];
 
// $$z[$x] = $y; // for some reason, this doesn't work (at least not with my PHP 5.2.4 installation)
ChangeArray($$z,$x,$y); // but this does the job - note the double $
 
print_r($myArray); // output: Array ( [test] => bye )
 
?>

Re: How to read the data after the "?" (e.g. index.php?x=off) ?

Posted: Tue Jan 06, 2009 9:04 pm
by requinix
Apollo wrote:For some reason you can't do this directly. But with a function using byref syntax, it works fine.
Try

Code: Select all

${$z}[$x] = $y;

Re: How to read the data after the "?" (e.g. index.php?x=off) ?

Posted: Wed Jan 07, 2009 2:46 am
by Apollo
tasairis wrote:Try

Code: Select all

${$z}[$x] = $y;
Nice. I thought the { } notation only applied inside strings.
Tnx! (also on behalf of the TS, I guess ;))

Re: How to read the data after the "?" (e.g. index.php?x=off) ?

Posted: Wed Jan 07, 2009 10:07 am
by helioslive
Yeah! Thank you both!

I think i did it now...
xD

I think i will use this forum more often.. xD :D