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

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
helioslive
Forum Newbie
Posts: 4
Joined: Tue Jan 06, 2009 5:35 pm
Location: Coding somewhere

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

Post 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
User avatar
Syntac
Forum Contributor
Posts: 327
Joined: Sun Sep 14, 2008 7:59 pm

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

Post by Syntac »

User avatar
Apollo
Forum Regular
Posts: 794
Joined: Wed Apr 30, 2008 2:34 am

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

Post 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 )
 
?>
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

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

Post 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;
User avatar
Apollo
Forum Regular
Posts: 794
Joined: Wed Apr 30, 2008 2:34 am

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

Post 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 ;))
User avatar
helioslive
Forum Newbie
Posts: 4
Joined: Tue Jan 06, 2009 5:35 pm
Location: Coding somewhere

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

Post by helioslive »

Yeah! Thank you both!

I think i did it now...
xD

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