Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    Regular Coder
    Join Date
    Sep 2007
    Posts
    125
    Thanks
    30
    Thanked 0 Times in 0 Posts

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

    I made this function:
    Code:
    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:
    Code:
    <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!...
    Last edited by ShootingBlanks; 11-14-2007 at 05:33 PM.

  • #2
    Senior Coder CFMaBiSmAd's Avatar
    Join Date
    Oct 2006
    Location
    Denver, Colorado USA
    Posts
    2,960
    Thanks
    2
    Thanked 304 Times in 296 Posts
    The second double-quote in your link is in the wrong place -
    Code:
    <a href="index.php?orderBy=leader&orderType="<?php echo switchOrder($orderType); ?>>Leader</a>
    should be -
    Code:
    <a href="index.php?orderBy=leader&orderType=<?php echo switchOrder($orderType); ?>">Leader</a>
    The second quote indicates the end of the href="..." parameter.
    If you are learning PHP, developing PHP code, or debugging PHP code, do yourself a favor and check your web server log for errors and/or turn on full PHP error reporting in php.ini or in a .htaccess file to get PHP to help you.

  • Users who have thanked CFMaBiSmAd for this post:

    ShootingBlanks (11-14-2007)

  • #3
    Regular Coder
    Join Date
    Sep 2007
    Posts
    125
    Thanks
    30
    Thanked 0 Times in 0 Posts
    D'oh! - I knew it would be a stupid error...

    ...THANKS!!!

  • #4
    Senior Coder
    Join Date
    Jan 2007
    Posts
    1,648
    Thanks
    1
    Thanked 58 Times in 54 Posts
    It's good to use clean, self describing code. But I think this is overdoing it.

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

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

  • Users who have thanked aedrin for this post:

    ShootingBlanks (11-14-2007)

  • #5
    Regular Coder
    Join Date
    Sep 2007
    Posts
    125
    Thanks
    30
    Thanked 0 Times in 0 Posts
    Good call....thanks!


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •