Erica
Erica

Reputation: 111

Why does my simple "if" not work? PHP

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?

Upvotes: 2

Views: 200

Answers (2)

Sean Kimball
Sean Kimball

Reputation: 4494

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.

Upvotes: 2

Marvin Fischer
Marvin Fischer

Reputation: 2572

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 . "]]";

Upvotes: 5

Related Questions