...

View Full Version : Function not working - can someone please look at code and tell me what's wrong?



ShootingBlanks
11-14-2007, 06:23 PM
I made this function:

if (isset($_GET['orderType'])) {
switch($_GET['orderType']) {
case "ASC":
$orderType = "ASC";
break;
case "DESC":
$orderType = "DESC";
break;
default:
$orderType = "ASC";
}
} else {
$orderType = "ASC";
}

function switchOrder($orderType) {
if ($orderType == "ASC") {
$returnOrder = "DESC";
} else {
$returnOrder = "ASC";
}
return $returnOrder;
}

Then I have this link in the body of my document:

<a href="index.php?orderBy=leader&orderType="<?php echo switchOrder($orderType); ?>>Leader</a>

All that's supposed to happen is to have the "orderType" variable in the URL alternate between "ASC" and "DESC" everytime it's clicked. However, all that comes up when it's clicked is:

/index.php?orderBy=leader&orderType=

So, it's like it's not getting the $returnOrder variable from my function. What am I doing wrong here? I am pretty novice with PHP, so it may be a simple error. Thanks!...

CFMaBiSmAd
11-14-2007, 06:37 PM
The second double-quote in your link is in the wrong place -

<a href="index.php?orderBy=leader&orderType="<?php echo switchOrder($orderType); ?>>Leader</a>should be -

<a href="index.php?orderBy=leader&orderType=<?php echo switchOrder($orderType); ?>">Leader</a>The second quote indicates the end of the href="..." parameter.

ShootingBlanks
11-14-2007, 06:42 PM
D'oh! - I knew it would be a stupid error...

...THANKS!!! :thumbsup:

aedrin
11-14-2007, 07:06 PM
It's good to use clean, self describing code. But I think this is overdoing it.



if (isset($_GET['orderType'])) {
switch($_GET['orderType']) {
case "ASC":
$orderType = "ASC";
break;
case "DESC":
$orderType = "DESC";
break;
default:
$orderType = "ASC";
}
} else {
$orderType = "ASC";
}




$orderType = isset($_GET['orderType']) ? $_GET['orderType'] : 'ASC';

ShootingBlanks
11-14-2007, 10:05 PM
Good call....thanks!



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum