Welcome to WebmasterWorld Guest from 54.147.158.215

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

Dynamic GET Problems

     
12:34 am on Jul 6, 2010 (gmt 0)

Preferred Member

10+ Year Member

joined:Aug 2, 2005
posts:501
votes: 0


I don't understand why this code does not work. Any suggestions?

assume the url is www.site.com/?dog=poodle


$svar = dog;
$tkw = $_GET[$svar];


$tkw is returning nothing, and I thought that it would return "poodle".
12:40 am on July 6, 2010 (gmt 0)

Moderator from GB 

WebmasterWorld Administrator mack is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:June 15, 2001
posts:7581
votes: 10


in your example the variable is dog the value is poodle. We need to first get the value of dog.

$var = $_GET['dog'];
echo"$var";

will result in poodle


Mack.
12:57 am on July 6, 2010 (gmt 0)

Preferred Member

10+ Year Member

joined:Aug 2, 2005
posts:501
votes: 0


Well, the problem is that the variable will not always be dog. It might be cat sometimes, or mouse - and that info is being pulled from a sql database based on other GET variables.

I guess i just need to know if there is a way to have a variable, variable.
3:30 am on July 6, 2010 (gmt 0)

Moderator from GB 

WebmasterWorld Administrator mack is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:June 15, 2001
posts:7581
votes: 10


might be easier to have more than one variable?

example.com/?animal=dog&type=poodle

$animal = $_GET['animal'];
$type = $_GET['type'];
6:11 am on July 6, 2010 (gmt 0)

Senior Member

WebmasterWorld Senior Member dreamcatcher is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:Mar 30, 2003
posts:3719
votes: 0


The problem is because you are using dog without quotes and the system thinks its a constant. If you use error reporting you`ll see a notice for an undefined constant.

Try it in quotes:

$svar = "dog";
$tkw = $_GET[$svar];

dc
1:53 pm on July 6, 2010 (gmt 0)

Preferred Member

10+ Year Member

joined:Aug 2, 2005
posts:501
votes: 0


Awesome thank you. It works with the quotes.

Now suppose dog is instead a $row variable. I can't seem to get it to work. Tried different quoting...stumped
2:26 pm on July 6, 2010 (gmt 0)

Senior Member

WebmasterWorld Senior Member 5+ Year Member

joined:Feb 22, 2009
posts:1396
votes: 0


Hi there NoLimits,

>>Now suppose dog is instead a $row variable. I can't seem to get it to work. Tried different quoting...stumped

Post the offening code, then we can assist you in getting it working :) The quoting isn't that difficult once you get used to using it, ie when to use, when not to etc..

Cheers,
MRb
3:23 pm on July 6, 2010 (gmt 0)

Preferred Member

10+ Year Member

joined:Aug 2, 2005
posts:501
votes: 0


Here is the offending code:

$subid = mysql_query("SELECT sourcevar1 FROM sources WHERE id='$tsid'");
$row = mysql_fetch_array($subid);
$svar = ".$row{'sourcevar1'}.";
$tkw = $_GET[$svar];

If I manually set $svar = "dog" it works fine, but when I let $svar = the $row variable it returns null.

Thank you gents for all your help!
5:05 pm on July 6, 2010 (gmt 0)

Preferred Member

10+ Year Member

joined:Aug 2, 2005
posts:501
votes: 0


It won't let me edit the above post, but i see that I had an error in it - my problem persists however, the updated code is below:

$subid = mysql_query("SELECT sourcevar1 FROM sources WHERE id='$tsid'");
$row = mysql_fetch_array($subid);
$svar = $row{sourcevar1};
$svar2 = "dog";
$tvar = $_GET[$svar];
$tvar2 = $_GET[$svar2];
print "$tvar";
print "$tvar2";

When I manually define $svar it works, but when I use the $row{sourcevar1} it does not work. I have confirmed that the values are one and the same by printing $row{'surcevar1'}

The example I have setup above illustrates the problem. $tvar is null and $tvar2 prints out the value of $_GET['dog'] from the URL, as it should.
5:33 pm on July 6, 2010 (gmt 0)

Senior Member

WebmasterWorld Senior Member dreamcatcher is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:Mar 30, 2003
posts:3719
votes: 0


Try changing:

$svar = $row{sourcevar1};

to

$svar = $row['sourcevar1'];

dc
5:54 pm on July 6, 2010 (gmt 0)

Preferred Member

10+ Year Member

joined:Aug 2, 2005
posts:501
votes: 0


I amended the code based on your suggestion:

$subid = mysql_query("SELECT sourcevar1 FROM sources WHERE id='$tsid'");
$row = mysql_fetch_array($subid);
$svar = $row['sourcevar1'];
$tvar = $_GET[$svar];
print "$svar - $tvar";

The result was the same. Thanks for your continued help though, I really appreciate it!
6:52 pm on July 6, 2010 (gmt 0)

Senior Member

WebmasterWorld Senior Member 5+ Year Member

joined:Feb 22, 2009
posts:1396
votes: 0


Hi there nolimits,

Firstly, I assume as there is more than one row in the table being searched - but is there only one row going to be returned from this query? if so, you can just place a LIMIT 1 clause on the end of the query - that's just my preference, you don't need to do that, but I find that if the query being sent should only return one value (one row) just make certain by putting the LIMIT 1 clause there.

Secondly,if there is a likelihood of there being more than one result, you'll need to loop through the array returned, else you will only ever get the last result returned in the array - ie the last entry that matches whatever is stored in $tsid, whether it's the right one or not..

Thirdly, I'm guessing that both of these values are going to be int's purely because of the calculation you are carrying out.. So I would do this:-


$GetQuery = "SELECT `sourcevar1` FROM `sources` WHERE `id` ='".$tsid."' LIMIT 1";
$subid = mysql_query($GetQuery) or die(mysql_error());//use this for handling errors from mysql remove when going live ;)
$row = mysql_fetch_array($subid) or die(mysql_error());
//I have put these three lines in just so that you can see exactly what's coming from the query
//remove them if the data is correct :)
echo "<pre>";
print_r($row);
echo "</pre>";
$svar = (int)$row['sourcevar1'];//just typecasting to ensure that it's an int - just remove (int) if that's not needed ;)
$tvar = $_GET[$svar];
echo $svar - $tvar;//this will echo the result (hopefully!)

Sorry to witter on, but this should be a simple extract and calculate, Other than that, I'm stumped.

Cheers,
MRb
7:08 pm on July 6, 2010 (gmt 0)

Preferred Member

10+ Year Member

joined:Aug 2, 2005
posts:501
votes: 0


Okay, I've altered the code to read:

$tsid = $_GET['tsid'];

$GetQuery = "SELECT `sourcevar1` FROM `sources` WHERE `id` ='".$tsid."' LIMIT 1";
$subid = mysql_query($GetQuery) or die(mysql_error());//use this for handling errors from mysql remove when going live ;)
$row = mysql_fetch_array($subid) or die(mysql_error());
//I have put these three lines in just so that you can see exactly what's coming from the query
//remove them if the data is correct :)
echo "<pre>";
print_r($row);
echo "</pre>";
$svar = $row['sourcevar1'];//just typecasting to ensure that it's an int - just remove (int) if that's not needed ;)
$tvar = $_GET[$svar];
echo "$svar : $tvar";

I removed the int on $row['sourcevar1'] as it is not an int. I changed the minus sign to a colon to eliminate confusion. The results are coming out the same, printing the following:

Array
(
[0] => dog
[sourcevar1] => dog
)

dog :
7:38 pm on July 6, 2010 (gmt 0)

Senior Member

WebmasterWorld Senior Member 5+ Year Member

joined:Feb 22, 2009
posts:1396
votes: 0


Hi there nolimits,

Ok, my misunderstanding then, I thought as you were subtracting one from the other. Doh.

Right, at least you know there are values within the query array, now what you need to do is this:-

if(isset($_GET) && !empty($_GET)){
echo "<pre>";
print_r($_GET);
echo "</pre>";
}
else{
echo "no $_GET's are set :(";
}

That will print out whatever is held in $_GET when the script is run, but I'm sure you already know that, I'm getting the feeling that there is a key naming mismatch somewhere.

BUT, personally (though your example is valid syntax) I would do this for the echo:-

echo $svar.":".$tvar;

Reading your earlier posts again, you say that entering the key value manually makes it work.

I have to ask if you have error_reporting(E_ALL); on in this script, OTT maybe, but at least you have it there.

Cheers,
MRb
7:55 pm on July 6, 2010 (gmt 0)

Preferred Member

10+ Year Member

joined:Aug 2, 2005
posts:501
votes: 0


oh lord - the problem was resolved moments ago.

There was a typo in the mysql database that I missed over and over again. Thank you all for your help.
8:07 pm on July 6, 2010 (gmt 0)

Senior Member

WebmasterWorld Senior Member 5+ Year Member

joined:Feb 22, 2009
posts:1396
votes: 0


Hi there nolimits,

>>I'm getting the feeling that there is a key naming mismatch somewhere.

Well at least I wasn't far off then ;)

Glad your all sorted now.

Have fun with the rest of your project..

Cheers,
MRb
 

Join The Conversation

Moderators and Top Contributors

Hot Threads This Week

Featured Threads

Free SEO Tools

Hire Expert Members