Forum

Thread tagged as: Question

Conditionals on perch_content_custom

Hullo,

Wondering if it is possible to have some type of if/else statements on a perch_content_custom call.

For example, I have a page that lists categories of Clearance products, with a select field in the admin to choose the category. In the HTML page, there is a perch_content_custom call for each category, like this:

<?php perch_content_custom('Clearance Items', array( 'filter'=>'p_clearance_group', 'match'=>'eq', 'value'=>'Construction' )); ?>

<?php perch_content_custom('Clearance Items', array( 'filter'=>'p_clearance_group', 'match'=>'eq', 'value'=>'Landscaping' )); ?>

<?php perch_content_custom('Clearance Items', array( 'filter'=>'p_clearance_group', 'match'=>'eq', 'value'=>'Rental Item' )); ?>

Nothing fancy – just checks for one of the three hard-coded categories and displays any listings in that group if it matches.

What I would like to do is be able to post a "No Clearance Items Available" message if there are no items in ANY of the categories. Of course, the region won't display at all if the filter/match comes up with no results, so I can't use the if/else conditionals in the template itself.

And that's about as far as I made it! Any suggestions would be helpful - thanks!

Richard Terrick

Richard Terrick 3 points

  • 7 years ago
Drew McLellan

Drew McLellan 2638 points
Perch Support

Sure, give something like this a go:

<?php 
$results_found = false;

$result = perch_content_custom('Clearance Items', array( 'filter'=>'p_clearance_group', 'match'=>'eq', 'value'=>'Construction'), true);
if ($result) {
    echo $result;
    $results_found = true;
}

$result = perch_content_custom('Clearance Items', array( 'filter'=>'p_clearance_group', 'match'=>'eq', 'value'=>'Landscaping'), true);
if ($result) {
    echo $result;
    $results_found = true;
}

$result = perch_content_custom('Clearance Items', array( 'filter'=>'p_clearance_group', 'match'=>'eq', 'value'=>'Rental Item'), true);
if ($result) {
    echo $result;
    $results_found = true;
}

if (!$results_found) {
    echo 'No Clearance Items Available';
}

?>

Sweet jebus, Drew - thanks!

Worked like a charm – clearly it's time to take some proper PHP courses so I can stop bugging you. :-)