www.webdeveloper.com
Results 1 to 9 of 9

Thread: Structure problem: trying to clean my code with a switch statement...

  1. #1
    Join Date
    Aug 2004
    Location
    Switzerland
    Posts
    1,101

    Structure problem: trying to clean my code with a switch statement...

    Hi!
    I'm working on a little thing for my website (modifying an existing wordpress plugin, so that I can style the comments from the authors even if they post on facebook on the comment in the posted on wordpress via the plugin).

    My code (working) is awful :
    PHP Code:
        function Comment_class($classes) {
                global 
    $comment;
                if (!empty(
    $comment) && $comment->comment_agent == 'AL2FB')
                    
    $classes[] = 'facebook-comment';
                            if (!empty(
    $comment) && $comment->comment_agent == 'AL2FB' && esc_attr($comment->comment_author_url) == 'http://www.facebook.com/profile.php?id=randomnumber1')
                    
    $classes[] = 'comment-author-ec';
                            if (!empty(
    $comment) && $comment->comment_agent == 'AL2FB' && esc_attr($comment->comment_author_url) == 'http://www.facebook.com/profile.php?id=randomenumber2')
                    
    $classes[] = 'comment-author-ness';
                            if (!empty(
    $comment) && $comment->comment_agent == 'AL2FB' && esc_attr($comment->comment_author_url) == 'http://www.facebook.com/profile.php?id=randomnumber3')
                    
    $classes[] = 'comment-author-alice';
                            if (!empty(
    $comment) && $comment->comment_agent == 'AL2FB' && esc_attr($comment->comment_author_url) == 'http://www.facebook.com/profile.php?id=randomnumber4' || !empty($comment) && $comment->comment_agent == 'AL2FB' && esc_attr($comment->comment_author_url) == 'http://www.facebook.com/profile.php?id=randomnumber5')
                    
    $classes[] = 'comment-author-joannv';
                            if (!empty(
    $comment) && $comment->comment_agent == 'AL2FB' && esc_attr($comment->comment_author_url) == 'http://www.facebook.com/profile.php?id=randomnumber6')
                    
    $classes[] = 'comment-author-kanata';
                return 
    $classes;
            } 

    Here is my attempt :

    PHP Code:
    function Comment_class($classes) {
                global 
    $comment;
                            if (!empty(
    $comment) && $comment->comment_agent == 'AL2FB' && esc_attr($comment->comment_author_url) == $urlfacebook)
                    
    $classes[] = $facebookclasshere;
                            switch (
    $urlfacebook) {
                
                    case 
    'http://www.facebook.com/profile.php?id=randomnumber1':            
                    
    $facebookclasshere 'comment-author-ec';
                    break;
                    
                    case 
    'http://www.facebook.com/profile.php?id=randomnumber2':
                    
    $facebookclasshere 'comment-author-ness';
                    break;
                    
                    case 
    'http://www.facebook.com/profile.php?id=randomnumber3':
                    
    $facebookclasshere 'comment-author-alice';
                    break;
                    
                    case 
    'http://www.facebook.com/profile.php?id=randomnumber4':                
                    case 
    'http://www.facebook.com/profile.php?id=randomnumber5':
                    
    $facebookclasshere 'comment-author-joannv';
                    break;
                    
                    case 
    'http://www.facebook.com/profile.php?id=randomnumber6':
                    
    $facebookclasshere 'comment-author-kanata';
                    break;
                    }
                return 
    $classes;
        } 
    No idea why it's not working Somebody could give me a hint? Thanks a lot !

  2. #2
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,633
    Main problem I see is that you try to assign $facebookclasshere to your array before it ($facebookclasshere, that is) gets defined/assigned in the following switch() block. Looks like that assignment should happen after the switch() finishes its work. I suspect this is your show-stopper issue -- the following are probably not crticial, but would add robustness and maintainability:

    For more robustness, you may want/need a "default" case to do whatever seems appropriate if you fall through all the other cases without a match (even if it's just logging something to the error_log).

    Lastly, "global" should be avoided except as a last resort. It's better to pass it in as another function parameter if possible, passing it by reference if you need to modify it as part of the function.
    Last edited by NogDog; 04-03-2013 at 08:22 PM.
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  3. #3
    Join Date
    Aug 2004
    Location
    Switzerland
    Posts
    1,101
    Thanks!
    Yeah, it's not my function, I'm just adding the switch. The global and all are part of the plugin, so I'd rather not touch it.
    I reordered it and put the

    PHP Code:
    $classes[] = $facebookclasshere
    after my switch, but it doesn't work either
    At the moment, my ugly code is working, but I don't like when it's a mess like that...

  4. #4
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,633
    Upon further review, I think maybe something like this is what you're after?
    PHP Code:
    function Comment_class($classes) {
        global 
    $comment// Yuk! ;-)
        
    if (!empty($comment) && $comment->comment_agent == 'AL2FB') {
            switch (
    esc_attr($comment->comment_author_url)) {
                case 
    'http://www.facebook.com/profile.php?id=randomnumber1':            
                    
    $facebookclasshere 'comment-author-ec';
                    break;
                case 
    'http://www.facebook.com/profile.php?id=randomnumber2':
                    
    $facebookclasshere 'comment-author-ness';
                    break;
                case 
    'http://www.facebook.com/profile.php?id=randomnumber3':
                    
    $facebookclasshere 'comment-author-alice';
                    break;
                case 
    'http://www.facebook.com/profile.php?id=randomnumber4':                
                case 
    'http://www.facebook.com/profile.php?id=randomnumber5':
                    
    $facebookclasshere 'comment-author-joannv';
                    break;
                case 
    'http://www.facebook.com/profile.php?id=randomnumber6':
                    
    $facebookclasshere 'comment-author-kanata';
                    break;
            }
        }
        
    $classes[] = $facebookclasshere;
        return 
    $classes;

    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  5. #5
    Join Date
    Aug 2004
    Location
    Switzerland
    Posts
    1,101
    Oh, awesome, perfectly working, thanks a lot!

  6. #6
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,633
    PS: This would be less likely to generate undefined variable warnings:
    PHP Code:
    function Comment_class($classes) {
        global 
    $comment// Yuk! ;-)
        
    if (!empty($comment) && $comment->comment_agent == 'AL2FB') {
            switch (
    esc_attr($comment->comment_author_url)) {
                case 
    'http://www.facebook.com/profile.php?id=randomnumber1':            
                    
    $classes[] = 'comment-author-ec';
                    break;
                case 
    'http://www.facebook.com/profile.php?id=randomnumber2':
                    
    $classes[] = 'comment-author-ness';
                    break;
                case 
    'http://www.facebook.com/profile.php?id=randomnumber3':
                    
    $classes[] = 'comment-author-alice';
                    break;
                case 
    'http://www.facebook.com/profile.php?id=randomnumber4':                
                case 
    'http://www.facebook.com/profile.php?id=randomnumber5':
                    
    $classes[] = 'comment-author-joannv';
                    break;
                case 
    'http://www.facebook.com/profile.php?id=randomnumber6':
                    
    $classes[] = 'comment-author-kanata';
                    break;
            }
        }
        return 
    $classes;

    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  7. #7
    Join Date
    Aug 2004
    Location
    Switzerland
    Posts
    1,101
    Thanks!
    The other one seems to be working perfectly fine, I added a default case so that the variable is defined anyway.
    Why would it generate undefined variables warnings?
    (I'm sorry, now I'm working with php like once a year, so I forgot a lot about it...)

  8. #8
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,633
    The prior version only set $facebookclasshere within the if() block, but then used it after that if() block. Therefore, if at any time the if() condition did not trigger, after that block you would then be trying to use $facebookclasshere in an undefined state.
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  9. #9
    Join Date
    Aug 2004
    Location
    Switzerland
    Posts
    1,101
    Ok, thanks for the explanation!

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles