ShootingBlanks Posted November 14, 2007 Share Posted November 14, 2007 Hello. The point of what I'm doing in the code below is to have it so that when a user clicks a link, the $_GET['orderType'] variable in the URL will alternate between "ASC" and "DESC" everytime the link is clicked, and the page will reload. If there is no "orderType" in the $_GET array upon entering the page, then it should default to "ASC". Here is the code, and below it is the error that I get: 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() { if ($orderType == "ASC") { $returnOrder = "DESC"; } else { $returnOrder = "DESC"; } return $returnOrder; } THEN IN THE BODY OF THE PAGE: <a href="index.php?orderBy=leader&orderType="<?php echo switchOrder(); ?>>Leader</a> Here is the error that I'm getting (the error is posted within the "Leader" link itself): Notice: Undefined variable: orderType in C:\htdocs\_PHP-SITES\ProjectBoard\index.php on line 125 DESC>Leader Line 125 in my code is the line just under "function switchOrder() {". Any ideas??? Thanks! Link to comment https://forums.phpfreaks.com/topic/77333-solved-function-not-working-can-someone-please-look-at-code-and-tell-me-whats-wrong/ Share on other sites More sharing options...
kenrbnsn Posted November 14, 2007 Share Posted November 14, 2007 The variable $orderType is not global in scope, either pass it into your function or declare it as a global in your function. The former method is preferred. Ken Link to comment https://forums.phpfreaks.com/topic/77333-solved-function-not-working-can-someone-please-look-at-code-and-tell-me-whats-wrong/#findComment-391517 Share on other sites More sharing options...
ShootingBlanks Posted November 14, 2007 Author Share Posted November 14, 2007 The variable $orderType is not global in scope, either pass it into your function or declare it as a global in your function. The former method is preferred. Ken How would I pass it to my function, based on the switch/case statement above the function? Like, I need it to be declared first in the switch/case statement, so then AFTER that, how do I get that into the "switchOrder" function? I guess I need to know how to reference a variable inside a function that is located outside of the function beforehand. I hope what I'm asking made sense. If not, it's just because I'm new at this! Link to comment https://forums.phpfreaks.com/topic/77333-solved-function-not-working-can-someone-please-look-at-code-and-tell-me-whats-wrong/#findComment-391518 Share on other sites More sharing options...
ShootingBlanks Posted November 14, 2007 Author Share Posted November 14, 2007 Okay - I changed the "switchOrder" function to this: function switchOrder($orderType) { if ($orderType == "ASC") { $returnOrder = "DESC"; } else { $returnOrder = "ASC"; } return $returnOrder; } And then I amended my link's code to this: <a href="index.php?orderBy=leader&orderType="<?php echo switchOrder($orderType); ?>>Leader</a> That got rid of any errors, but now my URL reads this anytime it's clicked: /index.php?orderBy=leader&orderType= So, the "orderType" is not being added to the URL. Am I doing that "return" stuff wrong or something??? Link to comment https://forums.phpfreaks.com/topic/77333-solved-function-not-working-can-someone-please-look-at-code-and-tell-me-whats-wrong/#findComment-391526 Share on other sites More sharing options...
revraz Posted November 14, 2007 Share Posted November 14, 2007 Try echo "switchOrder($orderType)"; Link to comment https://forums.phpfreaks.com/topic/77333-solved-function-not-working-can-someone-please-look-at-code-and-tell-me-whats-wrong/#findComment-391528 Share on other sites More sharing options...
ShootingBlanks Posted November 14, 2007 Author Share Posted November 14, 2007 Try echo "switchOrder($orderType)"; Nope - same results. Any other ideas??? Link to comment https://forums.phpfreaks.com/topic/77333-solved-function-not-working-can-someone-please-look-at-code-and-tell-me-whats-wrong/#findComment-391530 Share on other sites More sharing options...
ShootingBlanks Posted November 14, 2007 Author Share Posted November 14, 2007 Nevermind - stupid error. The second quotation mark in my link was in the wrong place. It should've gone AFTER the PHP code, and not before it. Thanks! Link to comment https://forums.phpfreaks.com/topic/77333-solved-function-not-working-can-someone-please-look-at-code-and-tell-me-whats-wrong/#findComment-391545 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.