Forum

Thread tagged as: Question

Checking a specific field's true/false value in a region and displaying differen...

Not sure exactly how to explain this, but here goes. I have a multiple-item region with a simple select field with the ID of is_date_based:

<perch:content id="is_date_based" type="select" label="Tour is date-based" options="true,false" />

On the Tour Detail page, I want to be able to display a single item from that region (based on the slug), but then –– depending on the value of is_date_based, return a different page function (i.e., via an if/else in PHP).

Here's what I have:

<?php
// pull up the Tour detail region and store as variable
$tour_detail = perch_content_custom('Tours', [
         'page' => '/tours.php',
         'template' => 'tours/_tour_detail.html',
         'filter' => 'slug',
         'match' => 'eq',
         'value' => perch_get('s'),
         'count' => 1,
         'skip-template' => true,
         'return-html' => true,
]);

// echo the returned HTML
echo $tour_detail['html'];

// all good so far – here's where I need guidance
// need to check the is_date_based value and run if/else statement from there

         // if is_date_based = true
if {
         echo "Yes, this is a date-based tour";
         else {
         // if is_date_based = false
         echo "No, this is not a date-based tour";
         }
}
?>

Is this possible? Any help would be greatly appreciated. Thanks in advance!

Richard Terrick

Richard Terrick 3 points

  • 2 years ago

Richard, you can always use perch:if in your template and call a perch:layout based on the if/else result.

Hi Robert,

Normally I would go this route and use perch:if within a template, but I don't think it would work in this case – it's a bit of a weird setup.

What I am doing is using a typical list/detail setup to create content – in this case each item in the multiple item region provides the details of a Tour.

Then, also on the detail page, I am displaying related Products (in this case, tickets) using perch_shop_products. Normally I would just have a single product with options/variables (like shirt sizes/colours), rather than a content region, but for various reasons related to how these Tours are based on specific dates, and how I am limited in counting stock, it's not possible. Certainly an edge case.

It's worth noting that the "products" themselves do not have the is_date_based variable – just the Tours content region. So I need to check the value of this item via the content region – then my if/else statement would return different functions that are quite different.

I see what you're saying about using perch:if within my product template to call different layouts, I would still need to be able to pass in the is_date_based variable from the content region and go from there...

Hopefully that makes some sense – long day. Thanks for the input, it has definitely got me thinking!

Okay, think I have got it – if anyone wants to pass their eyes over this to see if it is a logical/efficient way I would appreciate it!

  $tour_detail = perch_content_custom('Tours', [
         'page' => '/tours.php',
         'template' => 'tours/_tour_detail.html',
         'filter' => 'slug',
         'match' => 'eq',
         'value' => perch_get('s'),
         'count' => 1,
         'skip-template' => true,
         'return-html' => true,
    ]);
// output the HTML
    echo $tour_detail['html'];

// store the value of the is_date_based id from the $tour_detail into a new variable
    $tour_type = $tour_detail[0]['is_date_based'];

// check if the is_date_based value matches as true, otherwise assume false
    if ($tour_type === 'true') {
      echo "This is a date-based tour";
    }
    else {
      echo "This is not a date-based tour";
    }

That's how I'd do it!

Richard Terrick said:

Hi Robert,

Normally I would go this route and use perch:if within a template, but I don't think it would work in this case – it's a bit of a weird setup. https://tgw.onl/hostgator/ https://tgw.onl/dreamhost/ https://tgw.onl/bluehost/

What I am doing is using a typical list/detail setup to create content – in this case each item in the multiple item region provides the details of a Tour.

Then, also on the detail page, I am displaying related Products (in this case, tickets) using perch_shop_products. Normally I would just have a single product with options/variables (like shirt sizes/colours), rather than a content region, but for various reasons related to how these Tours are based on specific dates, and how I am limited in counting stock, it's not possible. Certainly an edge case.

It's worth noting that the "products" themselves do not have the is_date_based variable – just the Tours content region. So I need to check the value of this item via the content region – then my if/else statement would return different functions that are quite different.

I see what you're saying about using perch:if within my product template to call different layouts, I would still need to be able to pass in the is_date_based variable from the content region and go from there...

Hopefully that makes some sense – long day. Thanks for the input, it has definitely got me thinking! Okay, think I have got it – if anyone wants to pass their eyes over this to see if it is a logical/efficient way I would appreciate it!