Forum
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!
Using
perch_content
in your PHP page might not be the best idea. It's probably better to useperch_content_create
(passing in the template option to define the template) and follow that withperch_content_custom
(passing in theskip-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!
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:
but I'm not sure how to actually put the perch_content_custom into the script to replace:
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:
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.
Hi David,
you're not going to get something that matches this:
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.
Untested (just off the top of my head) but something like:
Drew that's done it! thank you!! :) :) :)