1

i have 6 html select boxes. Each select box contains three value (recent-media,popular-media,comments-media) and i have variables $item->content1 to $item->content6 each variable return to select box. and i create this code below by switch statement because i want use switch statement one time instead of use it 6 times.

the code work but every time return to recent-media and not return to popular and comment media. How i can fix this problem until my code correctly and return for all cases?

function YPE_bsn_show_diffcontent(&$item_output, $item) {
        switch (true) {
            case ($item->content1 == 'recent-media' OR $item->content2 == 'recent-media' OR $item->content3 == 'recent-media' OR $item->content4 == 'recent-media' OR $item->content5 == 'recent-media' OR $item->content6 == 'recent-media'):
                YPE_Bsn_Content::YPE_bsrecent_media($item_output, $item);
                break;
            case ($item->content1 == 'popular-media' OR $item->content2 == 'popular-media' OR $item->content3 == 'popular-media' OR $item->content4 == 'popular-media' OR $item->content5 == 'popular-media' OR $item->content6 == 'popular-media'):
                YPE_Bsn_Content::YPE_bspopular_media($item_output, $item);
                break;
            case ($item->content1 == 'comment-media' OR $item->content2 == 'comment-media' OR $item->content3 == 'comment-media' OR $item->content4 == 'comment-media' OR $item->content5 == 'comment-media' OR $item->content6 == 'comment-media'):
                YPE_Bsn_Content::YPE_bscomments_media($item_output, $item);
                break;
            default:
                $item_output .= 'You don\'t selected content for first column';
                break;
        }
}

1 Answer 1

1

You are not switching a variable but a constant then the result is always the same

     switch (true) {

         case 
             .....
    }

switch the proper related var

     switch ($item->content1 ) {
           case 'recent-media': 
                your code fo recent media 
                break;
           ....
           case 'popular-media': 
                your code fo popular media  
                break;
           case 'comment-media': 
                your code fo comment  media 
                break;
    }

Essentially you use the swithc in wrong way see php doc for proper use

in your case you need if (and not switch)

  function YPE_bsn_show_diffcontent(&$item_output, $item) {
        if  ($item->content1 == 'recent-media' OR 
              $item->content2 == 'recent-media' OR 
              $item->content3 == 'recent-media' OR 
              $item->content4 == 'recent-media' OR 
              $item->content5 == 'recent-media' OR 
              $item->content6 == 'recent-media')  {
                YPE_Bsn_Content::YPE_bsrecent_media($item_output, $item);
                return;
        }
        if  ($item->content1 == 'popular-media' OR 
              $item->content2 == 'popular-media' OR 
              $item->content3 == 'popular-media' OR 
              $item->content4 == 'popular-media' OR 
              $item->content5 == 'popular-media' OR 
              $item->content6 == 'popular-media') {
                YPE_Bsn_Content::YPE_bspopular_media($item_output, $item);
                return;
        }
        if  ($item->content1 == 'comment-media' OR 
              $item->content2 == 'comment-media' OR 
              $item->content3 == 'comment-media' OR 
              $item->content4 == 'comment-media' OR 
              $item->content5 == 'comment-media' OR 
              $item->content6 == 'comment-media') {
                YPE_Bsn_Content::YPE_bscomment_media($item_output, $item);
                return;
        }

       $item_output .= 'You don\'t selected content for first column';
       return ;
    }
Sign up to request clarification or add additional context in comments.

4 Comments

that means i use switch statement 6 times! before i used your way and worked for me without problem. but i search fro best and short way until i collect all $item->content1-6 in one switch statement because all $item->content1-6 have the same values. Is i can do this?
I have update the answer for the (i hope) the best solutions ,, (in this case)
also same result as in switch statement. I don't know why
Have you check the real content of $item? try with a var_dump($item) before call the function ..

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.