homepage Welcome to WebmasterWorld Guest from 184.72.82.126
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
Dynamic GET Problems
NoLimits




msg:4165002
 12:34 am on Jul 6, 2010 (gmt 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".

 

mack




msg:4165003
 12:40 am on Jul 6, 2010 (gmt 0)

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.

NoLimits




msg:4165008
 12:57 am on Jul 6, 2010 (gmt 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.

mack




msg:4165027
 3:30 am on Jul 6, 2010 (gmt 0)

might be easier to have more than one variable?

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

$animal = $_GET['animal'];
$type = $_GET['type'];

dreamcatcher




msg:4165079
 6:11 am on Jul 6, 2010 (gmt 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

NoLimits




msg:4165189
 1:53 pm on Jul 6, 2010 (gmt 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

Matthew1980




msg:4165204
 2:26 pm on Jul 6, 2010 (gmt 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

NoLimits




msg:4165227
 3:23 pm on Jul 6, 2010 (gmt 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!

NoLimits




msg:4165289
 5:05 pm on Jul 6, 2010 (gmt 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.

dreamcatcher




msg:4165319
 5:33 pm on Jul 6, 2010 (gmt 0)

Try changing:

$svar = $row{sourcevar1};

to

$svar = $row['sourcevar1'];

dc

NoLimits




msg:4165326
 5:54 pm on Jul 6, 2010 (gmt 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!

Matthew1980




msg:4165357
 6:52 pm on Jul 6, 2010 (gmt 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

NoLimits




msg:4165367
 7:08 pm on Jul 6, 2010 (gmt 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 :

Matthew1980




msg:4165384
 7:38 pm on Jul 6, 2010 (gmt 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

NoLimits




msg:4165389
 7:55 pm on Jul 6, 2010 (gmt 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.

Matthew1980




msg:4165393
 8:07 pm on Jul 6, 2010 (gmt 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

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved