Forum

Thread tagged as: Question, Problem, Error

Using PHP in options-tag

I'm a frontend developer and have a bit struggle to implement my dynamically created opening hours in a radio-button template:

<perch:content id="course_time" type="radio" label="Do we have open today?" options="dynamically|<?php echo $cocktailbar; ?>, open|yes we have open, closed|no, we are closed" />

I have to print these times when the owner of the site choose the radio-button labeled with "dynamically":

<?php
    $date = date('l');

    switch ($date) {
        case 'Sunday':
            $cocktailbar = "Sonntag 18:00 – 22:30";
            break;
        case 'Monday':
            $cocktailbar = "Heute ist die Bar geschlossen";
            break;
        case 'Tuesday':
            $cocktailbar = "Heute ist die Bar geschlossen";
            break;
        case 'Wednesday':
            $cocktailbar = "Heute ist die Bar geschlossen";
            break;
        case 'Thursday':
            $cocktailbar = "Donnerstag 18:00 – 24:00";
            break;
        case 'Friday':
            $cocktailbar = "Freitag 18:00 – 02:00";
            break;
        case 'Saturday':
            $cocktailbar = "Samstag 18:00 – 02:00";
            break;
    }
?>

Is there a way to get this running?

Andreas Gücklhorn

Andreas Gücklhorn 0 points

  • 6 years ago

If you wondering:

The restaurant owner need these options on holidays or other non regular days.

Rachel Andrew

Rachel Andrew 394 points
Perch Support

You cannot use PHP in a Perch template.

I'm not really sure what it is you are trying to achieve to be able to suggest a solution.

Hi Rachel :)

I got these website: https://www.seehofleineck.de/

On the upper left tile and the lower right tile there are dynamically rendered opening hours.

Now, I would like to use a perch radio button template with the following options available:

1: I will use the dynamically rendered opening hours from my php

2: Today is closed (because of an holiday or something else)

3: Today is open (because of a big party or whatever)

Drew McLellan

Drew McLellan 2638 points
Perch Support

In your template:

<perch:content id="course_time" type="radio" label="Do we have open today?" options="dynamically, open, closed" suppress="true" />

<perch:if id="course_time" value="dynamically">
    <perch:content id="opening_times" type="hidden" />
</perch:>

<perch:if id="course_time" value="open">
    Yes, we are open.
</perch:>

<perch:if id="course_time" value="closed">
    Sorry, we are closed.
</perch:>

Then in your page:

<?php
    $date = date('l');

    switch ($date) {
        case 'Sunday':
            $cocktailbar = "Sonntag 18:00 – 22:30";
            break;
        case 'Monday':
            $cocktailbar = "Heute ist die Bar geschlossen";
            break;
        case 'Tuesday':
            $cocktailbar = "Heute ist die Bar geschlossen";
            break;
        case 'Wednesday':
            $cocktailbar = "Heute ist die Bar geschlossen";
            break;
        case 'Thursday':
            $cocktailbar = "Donnerstag 18:00 – 24:00";
            break;
        case 'Friday':
            $cocktailbar = "Freitag 18:00 – 02:00";
            break;
        case 'Saturday':
            $cocktailbar = "Samstag 18:00 – 02:00";
            break;
    }

    PerchSystem::set_var('opening_times', $cocktailbar);
    perch_content_custom('Your region', [
         'template' => 'your_template.html',
    ]);
?>

Hello Drew,

thank you so much for that awesome support!

Unfortunately, it doesn't work as expected. All three options are displayed right now:

1 2 3

Drew McLellan

Drew McLellan 2638 points
Perch Support

You can remove <?php perch_content('Foo'); ?> as you've replace it with the perch_content_custom() call.

What does your template look like?

I removed the perch_content('Foo'). But all three options are still visible:

alt text for image

alt text for image

alt text for image

Drew McLellan

Drew McLellan 2638 points
Perch Support

Replace

</perch:>

with

</perch:if>

That was my late night typo that I then copied and pasted twice. Sorry!

Now it works like expected :)

Great product and great support, thanks!