2

So, I'm trying to do something pretty simple: to check if a number equals another number - but for some reason it just doesn't want to work.

$exhibitions = "20,21,24";
$parent = "[[*parent]]";
$id = "[[*id]]";

if ($id == 5) {
    $chunk = "listExhibitions";
}
if (stripos($exhibitions, $parent) == TRUE) {
    $chunk = "Exhibitions";
}
return "[[$" . $chunk . "]]";

It's the first "if" that I'm trying to get to work. If I put an ! before the == then the page shows the "listExhibitions" chunk - but I need it to do so when the id equals five. I've tried putting ' ' around the number too. Also, when I've simpy outputted the $id, the number 5 shows up.

What am I doing wrong?

Sean Bright
  • 118,630
  • 17
  • 138
  • 146
Erica
  • 111
  • 1
  • 9
  • 16
    I'm confused - how would `"[[*id]]"` ever equal `5`? – Sean Bright Aug 09 '18 at 13:25
  • You're defining a string as id. You're comparing `[[*5]]` to `5`. – Will Aug 09 '18 at 13:28
  • It's a modx placeholder (so it outputs the current document's id - and I've checked that it outputs the number 5 when I simply returned $id). – Erica Aug 09 '18 at 13:28
  • @SeanBright i assume the op uses this "thing" as a placeholder. – Tobias F. Aug 09 '18 at 13:28
  • 5
    But since there is no replaceholdering going on between `$id = "[[*id]]";` and `if ($id == 5) {` ... this will never work. – IncredibleHat Aug 09 '18 at 13:29
  • try to put an echo inside the first *and* second if, with outputs like `First If` and `Second If`. You might have a problem because you are overwriting the `$chunk` variable. – Tobias F. Aug 09 '18 at 13:31
  • 1
    Can you link to the documentation that says you can use `[[*5]]`? – Zoe Edwards Aug 09 '18 at 13:32
  • I've tried deleting the "ifs" and just writing return $id; after the first three variables - when I run the page, the number 5 shows up. So I should be able to use it in the "if", right? – Erica Aug 09 '18 at 13:34
  • It seems like a small logical error. GO ahead an place `echo`s inside the ifs, just printing "Firt If" / "Second If" will do. – Tobias F. Aug 09 '18 at 13:35
  • 1
    @ThomasEdwards : yes, here: https://docs.modx.com/revolution/2.x/making-sites-with-modx/structuring-your-site/resources#Resources-UsingResourceFields – Erica Aug 09 '18 at 13:35
  • 1
    I am not at all familiar with modx, but I assume these placeholders are only replaced in certain contexts, not in "raw" PHP code. – Sean Bright Aug 09 '18 at 13:36
  • @EricaWyrdling In the documentation that you've posted it states: `Grabbing the Resource Fields in a Snippet is quite easy; MODx provides you with the Resource object in any Snippet, via the $modx->resource reference. ` so `$modx->resource->get('id');` – Michal Bieda Aug 09 '18 at 13:37
  • @SeanBright i think you are right, i would imagine it to work like e.g. **Smarty** or Laravels **blade** template languages. – Tobias F. Aug 09 '18 at 13:37
  • Ah I think you can only use these in the templates or content, not in PHP, as PHP will read these before MODX gets to them. In PHP land you’ll have to try and use the `$modx` class to access information, e.g. `$modx->resource->get('pagetitle')` – Zoe Edwards Aug 09 '18 at 13:38
  • @Erica, for simple debugging purposes, try do use this when you're not entirely sure what the variables are holding: echo "
    "; print_r(get_defined_vars());
    – Rafael Aug 09 '18 at 13:40
  • @TobiasF. That doesn't work - it's as if it doesn't "enter" the if statement (does that makes sense?) – Erica Aug 09 '18 at 13:40
  • 2
    @MichalBieda Okay, that worked! Guess I've misunderstood how to use the placeholders. – Erica Aug 09 '18 at 13:42

2 Answers2

5

You are referencing the ID in a way that should only be used in a view. This seems to be a controller. Try it this way:

$exhibitions = "20,21,24";
$parent = $modx->resource->get('parent');
$id = $modx->resource->get('id');

if ($id == 5) {
    $chunk = "listExhibitions";
}
if (stripos($exhibitions, $parent) == TRUE) {
    $chunk = "Exhibitions";
}
return "[[$" . $chunk . "]]";
Marvin Fischer
  • 2,552
  • 3
  • 23
  • 34
  • If she's trying to use the $exhibitions content on the return, it will still not work, can you edit the return for: `"[[${$chunk}]]"`? – Rafael Aug 09 '18 at 13:44
  • I don't see how thats different to the current code, can you make an edit suggestion? – Marvin Fischer Aug 09 '18 at 13:46
  • Thanks! The second if still doesn't work though (but when I put an ! before the == it does, so I'm guessing it can't find the parent id in the string $exhibitions) - any idea why? – Erica Aug 09 '18 at 13:48
  • return "[[$" . $chunk . "]]" will output [[$exhibitions]] as of "[[${$chunk}]]" will output the content of $exhibitions – Rafael Aug 09 '18 at 13:49
  • Output the `$parent` variable and look whats inside. Either use `var_dump` or `echo` – Marvin Fischer Aug 09 '18 at 13:49
  • @RafaelBoszko That wouldn't work within modx (a chunk is called by [[$chunkname]] ), so that part I'm fairly certain is correct. – Erica Aug 09 '18 at 13:49
  • @MarvinFischer When I return $parent the page shows the number 20 – Erica Aug 09 '18 at 13:51
  • [`stripos`](http://php.net/manual/en/function.stripos.php) returns an integer on success, `false` on failure. Comparing with `true` is meaningless. – Sean Bright Aug 09 '18 at 13:51
  • 1
    use `!== FALSE` – Marvin Fischer Aug 09 '18 at 13:51
  • Its usually best to use the absolute comparrison with stripos... like `!==false` to show that 'it exists'. – IncredibleHat Aug 09 '18 at 13:52
  • and if the result it's 0 it means it found the needle in the 0 position, if it cant find it will output specifically FALSE, so you @Erica, would need to use === – Rafael Aug 09 '18 at 13:53
  • Using: if (stripos($exhibitions, $parent) !== FALSE) { ... doesn't work either. – Erica Aug 09 '18 at 13:56
  • I have recreated that here: http://sandbox.onlinephpfunctions.com/code/b8c2470afd6b6702650701433b5a8311880b996f it should work – Marvin Fischer Aug 09 '18 at 13:57
  • @RafaelBoszko Is there a different way to check for $parent (in this case the number 20) in the string $exhibitions? – Erica Aug 09 '18 at 13:59
  • Use this Erica, see everything that's been set in the function : echo "
    "; print_r(get_defined_vars());
    – Rafael Aug 09 '18 at 14:01
  • `var_dump`, `echo` and `print_r` (also `die()` but that isn't handy) are the functions to output text to the browser, aslong as you can't use xdebug that are your options – Marvin Fischer Aug 09 '18 at 14:01
  • @MarvinFischer Hmm, when I simply put $parent = "20"; at the top, then it works, but $parent = $modx->resource->get('parent'); apparently doesn't? Even though I have: return $parent . "[[$" . $chunk . "]]"; at the bottom and the page displays the number 20 (as well as the chunk). – Erica Aug 09 '18 at 14:02
  • 2
    Lets continue this discussion here as the comment chain is getting to long: https://chat.stackoverflow.com/rooms/177740/why-does-my-simple-if-not-work-php – Marvin Fischer Aug 09 '18 at 14:04
  • @RafaelBoszko Eh, when I did that I got an insanely long list on the page - I'm guessing it outputs every modx setting/parameter/whatever... – Erica Aug 09 '18 at 14:06
2

What you are expecting to happen here is for Modx to automatically process your ID & PARENT placeholders and pass them into your snippet. Modx will not do that for you, you either have to pass them in expolicitly in the $scriptProperties array ~or~ as Marvin pointed out get those properties from the modResource object (which modx will assume to be the current resource)

To pass them explicitly, add the placeholders to your snippet call:

[[~MyCustomSnippet? &id=`[[*id]]` &parent=`[[*parent]]`]]

In that situation Modx WILL populate the placeholders when it parses your page, template or chunk (wherever you happen to have called the snippet.

If you are dealing with the ID & PARENT for the CURRENT resource; Marvin's example will work, though I do believe you have to get the current resource object first.

$resource = $modx->getObject('modResource');

you would have to check the docs on that one. (or test it)

UPDATE

The three of us worked this out in a chat & came up with the following solution:

By calling the snippet this way:

[[!MyCustomSnippet? &id=`[[*id]]`]]

The contents of the snippet:

<?php

$id = isset($scriptProperties['id']) ? $scriptProperties['id'] : FALSE; // get id passed with snippet

$exhibitions = array(20,21,24);

if(!$id){
    $id = $modx->resource->get('id'); // get the current resource id if it was not passed
}

$resource = $modx->getObject('modResource', $id); // get the resource object

$parent = $modx->resource->get('parent'); // get the parent id from the resource object

$output = '';

if ($id == 5) {
    $chunk = "listExhibitions";
}

if (in_array($parent, $exhibitions)) {
    $chunk = "Exhibitions";
}

$output = $modx->getChunk($chunk);

return $output;

That will use the ID passed in the snippet call OR assume the current resource if the id is not passed & get the parent ID from the resource object based on that.

Sean Kimball
  • 4,506
  • 9
  • 42
  • 73