Show category label on page when queried in URL
6 posts by 2 authors in: Forums > CMS Builder
Last Post: October 9, 2017 (RSS)
By Mikey - October 8, 2017
Is it possible to show the label for a category queried in the URL?
"Burgers" has a category num of "16", and is selected from a field called "meal_category".
Example of a category search for meals:
When the meal_category=16 is queried in the URL, I'd like to show the label for the category on the on page - as seen below:
Burgers
Thanks, Zicky
Hi Zick,
What happens when you use the :label pseudo on the detail page?
Something like:
<?php echo $your_sectionRecord['meal_category:label'] ?>
Jerry Kornbluth
Take advantage of a free 3 month trial subscription, only for CMSB users, at: http://www.thecmsbcookbook.com/trial.php
By Mikey - October 9, 2017
Hey Jerry,
The problem with something like <?php echo $your_sectionRecord['meal_category:label'] ?> is that this is a multi-section list page of menu items. When someone clicks on a link from the menu categories page, they arrive to a meals.php file which returns a listing of all meal items listed under the category as defined by the URL meals.php?menu_category=XX . This works fine, but listed the category selected on the meals.php page based on the URL query produces results similar to this Example: Salads, Salads, Salads, Salads... and so on for every salad listed under the category "Salads". And if the user ever removed "?menu_category=XX" from the URL query of meals.php, then they would see all the menu_category listed for all meals... Example: Salads, Salads, Salads, Salads, Burgers, Burgers, Burgers, Burgers, Steaks, Steaks, Steaks, Desserts, Desserts, Desserts... and so on for every meal items listed.
Hope this all makes since.
However, thanks for the suggestion... it got me thinking about a different approach to this problem, and I wrote the solution below which is working and accomplishes what I needed.
First thing I'm doing below is checking to see if the URL is lacking "?menu_category=XX", and if it is, then I provide a default solution as defined by $menu_pageRecord["title"]
<?php $url = $_SERVER['REQUEST_URI']; ?>
<?php if ($url == '/meals.php'): ?>
<?php echo htmlspecialchars($menu_pageRecord["title"]); ?>
ELSE if "?menu_category=XX" is defined, then load the "meal_category:label" as you suggested.
I also added a counter++ to break after the first occurrence of the meal_category to eliminate the repeated meal_category instance such as Salad, Salad, Salad, so it only produces one instance of Salad.
<?php else: ?>
<?php $counter = 0; ?>
<?php foreach ($menuRecords as $record): ?>
<?php $counter++;
if($counter > 1) {break;}
?>
<?php echo $record['meal_category:label'] ?>
<?php endforeach ?>
<?php endif; ?>
Put this all together and here's the final solution.
<h2>
<?php $url = $_SERVER['REQUEST_URI']; ?>
<?php if ($url == '/meals.php'): ?>
<?php echo htmlspecialchars($menu_pageRecord["title"]); ?>
<?php else: ?>
<?php $counter = 0; ?>
<?php foreach ($menuRecords as $record): ?>
<?php $counter++;
if($counter > 1) {break;}
?>
<?php echo $record['meal_category:label'] ?>
<?php endforeach ?>
<?php endif; ?>
<h2>
Thanks, Zicky
Hey. glad it sparked a solution.
I also use a break to make sure that theere's only one category title, but forgot about that when I posted.
Jerry
BTW, got any thoughts about my ajax post?
Take advantage of a free 3 month trial subscription, only for CMSB users, at: http://www.thecmsbcookbook.com/trial.php
By Mikey - October 9, 2017
I'll check out your ajax post and see if there's anything I can suggest once I return from dinner.
Zicky
Thanks Zick
Take advantage of a free 3 month trial subscription, only for CMSB users, at: http://www.thecmsbcookbook.com/trial.php