The $ page parameter comes fine, for the code

return $page; exit; 

no problem displays options 1, 2, 3, 4, etc.

But such a code

 <?php $output = ''; //return $page; //exit; switch ($page) { case '1': $output = $modx->getChunk('Mobile1'); break; case "2": $output = $modx->getChunk('Mobile2'); break; case 3: $output = $modx->getChunk('Mobile3'); break; case 0: $output = $modx->getChunk('Mobile4'); break; } return $output; 

in any case $ page always works only on the last line, case 0.

What is the problem? (this is ModX Revo, it is assumed that $ page comes in the $ _GET parameter)

I looked through this "1" in the hex editor. It turned out such crap:

 <20><31><0D><0A><20><20><20><20><20><20><20><20> 

space, 1, line break and a bunch of spaces.

Now trying to screw

 preg_replace('/[^0-9]/', '', $str); 

generally gives an empty string (

  • one
    It seems to me. If you remove the quotes in case 1 `` then everything will work, or not? - teran

3 answers 3

You need to call the snippet like this:

 &tplWrapper=`@CODE: [[+output]] {{!mobile?page=[[+page]]}}` 

Well, then the choice is as follows:

 <?php $output = ''; switch ((int)$page) { case 1: $output = $modx->getChunk('Mobile1'); break; case 2: $output = $modx->getChunk('Mobile2'); break; case 3: $output = $modx->getChunk('Mobile3'); break; case 0: $output = $modx->getChunk('Mobile4'); break; } return $output; 
  • Thank you, you helped me out again, everything works. If not difficult, give a link to the documentation on this issue, I would never have guessed that you need to use curly braces. Where is it at all? - Vladimir Malakhov

I would do something like that if I were you

 $page = intval(trim($page)); $m = [4,1,2,3]; // $m = $page < 4 : $page : 0; // $m = $page % 4; $output = $modx->getChunk("Mobile{$m[$page]}"); 

to demonstrate the problem of your code, and a little thought about type casting and string comparison:

 $page = " 1\r\n "; switch($page){ case '1' : echo "ooops!"; break; case "1" : echo 'oooooops!'; break; case 1 : echo 'wow!'; break; } 
  • thanks for the answer. problem: $ page = intval (trim ($ page)); again, always displays 0, respectively, always Mobile4. Can there be something else at the entrance to the $ page? how to catch? - Vladimir Malakhov
  • In general, it is transferred from the pdoPage by the parameter & tplWrapper = @CODE: [[+output]] [[!mobile?page=[[+page]]]] - Vladimir Malakhov
  • one
    @ Vladimir Malakhov you got rid of the lines in your case? What var_dump($page) say? - teran 1:01 pm
  • outputs like this: string (9) "1" string (9) "2" string (9) "3", etc. - Vladimir Malakhov
  • As a result, there was such a garbage (I found out by sending the result to a text file): $ page came in as [[+ page]] - here you have a string (9). And it was displayed on the page as normal numbers, because the conclusion was (according to the model) return [[+ page]]. @Tunker helped figure it out. Thanks for answers. - Vladimir Malakhov
 $page = 2; $output = ''; switch ($page) { case '1': $output = '$modx->getChunk(Mobile1)'; break; case "2": $output = '$modx->getChunk(Mobile2)'; break; case 3: $output = '$modx->getChunk(Mobile3)'; break; case 0: $output = '$modx->getChunk(Mobile4)'; break; } echo $output; 

the cockroach has no ears.