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
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
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
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
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
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
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
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
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:
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.
The result was the same. Thanks for your continued help though, I really appreciate it!
Matthew1980
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
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
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
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
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.