PDA

View Full Version : Variables in functions?



Decode
03-07-2008, 04:25 PM
If i had a function


function tom() {
if ( $_GET['name'] == "Tom" ) {
$var = "Tom";
}
else {
$var = "Your name is not Tom";
}
}

Can i echo the var out side of the function, becuase at the moment im having trouble doing it, but im not sure if its a problem with my sql code or that lol.

Jme
03-07-2008, 04:31 PM
function tom() {
if ( $_GET['name'] == "Tom" ) {
$var = "Tom";
}
else {
$var = "Your name is not Tom";
}
return $var;
}


That might work? When you want to show the variable, just call the function..

Dentafrice
03-07-2008, 05:44 PM
function foo ()
{
$name = $_GET["name"];
if ( $name == "Tom" ) {
$variable = $name;
}
else {
$variable = "Hey! Your name is not Tom!";
}
return $variable;
}

$returnVar = foo();

echo $returnVar;

Decode
03-07-2008, 06:03 PM
function foo ()
{
$name = $_GET["name"];
if ( $name == "Tom" ) {
$variable = $name;
}
else {
$variable = "Hey! Your name is not Tom!";
}
return $variable;
}

$returnVar = foo();

echo $returnVar;

Hmm its still not working, maybe its a problem with my sql.

im using the function to set the sql code;



function sqlby($cat) {
if ( $_GET['order'] == "desc" ) {
echo "selected";
if ( $cat == "all" ) {
$query ="SELECT * FROM games ORDER BY `games`.`dateday` DESC, `datemonth` DESC , `dateyear` DESC LIMIT " . $pagedatastart . " , " . $pagedataend;
}
else {
$query = "SELECT * FROM games WHERE cat = '" . $cat . "' ORDER BY `games`.`dateday` DESC, `datemonth` DESC , `dateyear` DESC LIMIT " . $pagedatastart . " , " . $pagedataend;
}
}
if ( $_GET['order'] == "asc" ) {
echo "selected";
if ( $cat == "all" ) {
$query = "SELECT * FROM games ORDER BY `games`.`dateday` ASC, `datemonth` ASC , `dateyear` ASC LIMIT " . $pagedatastart . " , " . $pagedataend;
}
else {
$query = "SELECT * FROM games WHERE cat = '" . $cat . "' ORDER BY `games`.`dateday` ASC, `datemonth` ASC , `dateyear` ASC LIMIT " . $pagedatastart . " , " . $pagedataend;
}
}
return $query;
}


then im using this to get the $query


$query = sqlby("action");
mysql_query($query) or die(mysql_error());


And i get this mysql error;

Query was empty

+rep if anyone knows whats wrong with it.

Independent
03-07-2008, 06:18 PM
I can't remember if you need to connect functions to the db, try that.

Dentafrice
03-07-2008, 06:20 PM
Do you have desc or asc in the URL? ?order=bla

Decode
03-07-2008, 06:45 PM
Do you have desc or asc in the URL? ?order=bla
LOL, thanks. That was the problem.

I would give u a +rep but it says i need to spread. :(

Want to hide these adverts? Register an account for free!