Forum

Thread tagged as: Question, Problem, Field-Types

Perch content within PHP script

Hi Guys.

I'm trying to make parts of a PHP script editable within Perch. I've included the code below. I've been trying to include the storeSchedule part within Perch so that a client can set the opening hours of the store.

I've used the select field type with 9:00 AM, 10:00 AM, etc and created a template which started at $storeSchedule and ended at the bottom of the opening times and added this into the script with a perch content tag set with the "true" option at the end but it's not working at all.

Working PHP Script:

<?php

$storeSchedule = [
    'Sun' => ['11:00 AM' => '9:00 PM'],
    'Mon' => ['11:00 AM' => '9:00 PM'],
    'Tue' => ['11:00 AM' => '9:00 PM'],
    'Wed' => ['11:00 AM' => '9:00 PM'],
    'Thu' => ['11:00 AM' => '9:00 PM'],
    'Fri' => ['11:00 AM' => '9:30 PM'],
    'Sat' => ['11:00 AM' => '9:30 PM']
];

$timestamp = time();

$status = '<span class="txt red">closed</span>';

$currentTime = (new DateTime())->setTimestamp($timestamp);

foreach ($storeSchedule[date('D', $timestamp)] as $startTime => $endTime) {

    $startTime = DateTime::createFromFormat('h:i A', $startTime);
    $endTime   = DateTime::createFromFormat('h:i A', $endTime);

    if (($startTime < $currentTime) && ($currentTime < $endTime)) {
        $status = '<span class="txt green">open</span>';
        break;
    }
}

echo "$status";

?>

Script with the content tags:

<?php

perch_content('Opening Times', true);

$timestamp = time();

$status = '<span class="txt red">closed</span>';

$currentTime = (new DateTime())->setTimestamp($timestamp);

foreach ($storeSchedule[date('D', $timestamp)] as $startTime => $endTime) {

    $startTime = DateTime::createFromFormat('h:i A', $startTime);
    $endTime   = DateTime::createFromFormat('h:i A', $endTime);

    if (($startTime < $currentTime) && ($currentTime < $endTime)) {
        $status = '<span class="txt green">open</span>';
        break;
    }
}

echo "$status";

?>

My template (I've only included Saturday as the full template it quite big and just repeats the rest of the days as above.

$storeSchedule = [
    'Sat' => ['<perch:content id="saturday-open" type="select" label="Saturday Open"
options="12:00 AM, 12:30 AM, 1:00 AM, 1:30 AM, 2:00 AM, 2:30 AM, 3:00 AM, 3:30 AM, 4:00 AM, 4:30 AM, 5:00 AM, 5:30 AM, 6:00 AM, 6:30 AM, 7:00 AM, 7:30 AM, 8:00 AM, 8:30 AM, 9:00 AM, 9:30 AM, 10:00 AM, 10:30 AM, 11:00 AM, 11:30 AM, 12:00 PM, 12:30 PM, 1:00 PM, 1:30 PM, 2:00 PM, 2:30 PM, 3:00 PM, 3:30 PM, 4:00 PM, 4:30 PM, 5:00 PM, 5:30 PM, 6:00 PM, 6:30 PM, 7:00 PM, 7:30 PM, 8:00 PM, 8:30 PM, 9:00 PM, 9:30 PM, 10:00 PM, 10:30 PM, 11:00 PM, 11:30 PM" allowempty="false" required="true" divider-before="Saturday Opening Hours" />' => '<perch:content id="saturday-closed" type="select" label="Saturday Close"
options="12:00 AM, 12:30 AM, 1:00 AM, 1:30 AM, 2:00 AM, 2:30 AM, 3:00 AM, 3:30 AM, 4:00 AM, 4:30 AM, 5:00 AM, 5:30 AM, 6:00 AM, 6:30 AM, 7:00 AM, 7:30 AM, 8:00 AM, 8:30 AM, 9:00 AM, 9:30 AM, 10:00 AM, 10:30 AM, 11:00 AM, 11:30 AM, 12:00 PM, 12:30 PM, 1:00 PM, 1:30 PM, 2:00 PM, 2:30 PM, 3:00 PM, 3:30 PM, 4:00 PM, 4:30 PM, 5:00 PM, 5:30 PM, 6:00 PM, 6:30 PM, 7:00 PM, 7:30 PM, 8:00 PM, 8:30 PM, 9:00 PM, 9:30 PM, 10:00 PM, 10:30 PM, 11:00 PM, 11:30 PM" allowempty="false" required="true" />']
];

If I remove the True bit from the content tag it just outputs that section of the code onto the page, which does seem to be the correct format at least but with a " " around it. I'm probably going about this in completely the wrong way so any help or advice would be great!

David Clarke

David Clarke 0 points

  • 4 years ago
Duncan Revell

Duncan Revell 78 points
Registered Developer

Using perch_content in your PHP page might not be the best idea. It's probably better to use perch_content_create (passing in the template option to define the template) and follow that with perch_content_custom (passing in the skip-template option).

This will allow you to essentially pass the data from the database into a PHP variable to then use in the rest of your script.

The template is still needed to define the data you want to collect, but don't put any PHP output into your template. Just think of it as getting the raw data into the database.

Also, to keep your template DRY, you could consider using repeaters.

There's other ways of doing this, of course!

Drew McLellan

Drew McLellan 2638 points
Perch Support

As Duncan says, you'll need to use skip-template to get the data. What it looks like you're doing at the moment is writing out PHP code which then ends up as a useless string - not code.

Get the data back into an array, and then loop through it to find what you want and build up your $storeSchedule array.

Thanks for the info guys. I've only really used Perch before to output html directly onto the page rather than into a script, so used to just including markup within the template file and doing it that way.

I've removed the php from the template now so I just have an open and close time for each day of the week: 9:00 AM 9:00 PM 9:00 AM 9:00 PM 9:00 AM 9:00 PM 9:00 AM 9:00 PM 9:00 AM 9:00 PM 9:00 AM 9:00 PM 9:00 AM 9:00 PM

How would I go about using perch_content_custom to put this into a format so that it matches the script above? I've used skip-template:

    <?php
      $content = perch_content_custom('Opening Times', array(
            'skip-template'=>'true'
    )); ?>

but I'm not sure how to actually put the perch_content_custom into the script to replace:

$storeSchedule = [
    'Sun' => ['11:00 AM' => '9:00 PM'],
    'Mon' => ['11:00 AM' => '9:00 PM'],
    'Tue' => ['11:00 AM' => '9:00 PM'],
    'Wed' => ['11:00 AM' => '9:00 PM'],
    'Thu' => ['11:00 AM' => '9:00 PM'],
    'Fri' => ['11:00 AM' => '9:30 PM'],
    'Sat' => ['11:00 AM' => '9:30 PM']
];
Drew McLellan

Drew McLellan 2638 points
Perch Support

I'd start with a print_r($content); and look to see if everything I need is available.

Hi Drew, thanks again for the reply. I just get this:

Array ( [0] => Array ( [_id] => 17 [sunday-open] => 10:00 AM [sunday-closed] => 9:00 PM [monday-open] => 9:00 AM [monday-closed] => 9:00 PM [tuesday-open] => 9:00 AM [tuesday-closed] => 9:00 PM [wednesday-open] => 9:00 AM [wednesday-closed] => 9:00 PM [thursday-open] => 9:00 AM [thursday-closed] => 9:00 PM [friday-open] => 9:00 AM [friday-closed] => 9:00 PM [saturday-open] => 9:00 AM [saturday-closed] => 9:00 PM [_page] => /php/openclosedtest.php [_pageID] => 5 [_sortvalue] => 1000 ) )

I'm not a php developer (as I'm sure you've guessed!) but I suppose it's sort of there? I can change sunday-open and sunday-closed to Sun in the template but how would I structure the perch template to have two select boxes (the opening and closing times) for one content id? or can I combine them within perch_content_custom.

Duncan Revell

Duncan Revell 78 points
Registered Developer

Hi David,

you're not going to get something that matches this:

'Sun' => ['11:00 AM' => '9:00 PM'], 
'Mon' => ['11:00 AM' => '9:00 PM'], 
'Tue' => ['11:00 AM' => '9:00 PM'], 
'Wed' => ['11:00 AM' => '9:00 PM'], 
'Thu' => ['11:00 AM' => '9:00 PM'], 
'Fri' => ['11:00 AM' => '9:30 PM'], 
'Sat' => ['11:00 AM' => '9:30 PM']

out of the database (have a read about PHP arrays and key => value)

However, if you experiment with a repeater in your template, you may get something that is closer.

What you have currently is one array with all the values you need in it - which will make it a bit more complicated to break up into each day.

If your repeater had fields for day, start-time, end-time and you repeated it seven times, you may get a result from skip-template that makes your life easier.

Drew McLellan

Drew McLellan 2638 points
Perch Support

Untested (just off the top of my head) but something like:

$tt = $content[0];
$storeSchedule = [ 
    'Sun' => [$tt['sunday-open']   => $tt['sunday-closed']],
    'Mon' => [$tt['monday-open']   => $tt['monday-closed']],
    'Tue' => [$tt['tuesday-open']  => $tt['tuesday-closed']],
    'Wed' => [$tt['wednesday-open']=> $tt['wednesday-closed']],
    'Thu' => [$tt['thursday-open'] => $tt['thursday-closed']],
    'Fri' => [$tt['friday-open']   => $tt['friday-closed']],
    'Sat' => [$tt['saturday-open'] => $tt['saturday-closed']] 
];

Drew that's done it! thank you!! :) :) :)