Help with an IF statement in Company Directory

Have a question or a suggestion about a 3rd party addon module or plugin?
Let us know here.
Post Reply
CapereSpiritum
Forum Members
Forum Members
Posts: 223
Joined: Wed Dec 28, 2011 12:11 pm

Help with an IF statement in Company Directory

Post by CapereSpiritum »

Hi Folks

I'm using Company Directory for a Music Venue and Bands Artists Directory.

As CD does not support 'Pretty URLs' for some of the features.
i.e. detailtemplate, I'm having to use a single detail template for several types of details.

One of the problems I have is this.
Venues have facilities: Car Park, Pool Table, Jukebox, Baby Changing etc

Bands and Artists don't.

I need to enter an 'IF' statement that, if none of the facilities have an entry, nothing is created (that's easy, even I can do that), but...

If there is one or more of the facilities added, I want to create a div with an h4 header and with all the entries in.

What is the correct syntax for, in my case 14 possible entries?
Is there a way or adding all 14 Field Definitions to a single IF statement and if just one has an entry create the div etc.

Here's just one of the Field Definitions

Code: Select all

{if isset($entry->fields.BeerGarden) && $entry->fields.BeerGarden->value != ''}<div class="VBeerGdn row col-tweleve cf">{$entry->fields.BeerGarden->value}</div>
{/if}
I know the following is wrong, but am I way off or near the mark?

Code: Select all

{if isset($entry->fields.Jukebox,LiveSportBigScreen,PoolTable,SnookerTable,DartBoard,FruitMachine,DisabledAccessToilet,SmokingArea,BabyChanging,DogFriendly,WiFi,CarPark,DanceFloor) && $entry->fields.Jukebox,LiveSportBigScreen,PoolTable,SnookerTable,DartBoard,FruitMachine,DisabledAccessToilet,SmokingArea,BabyChanging,DogFriendly,WiFi,CarPark,DanceFloor->value != ''}{/if}
Any help appreciated for an amateur in php. I'm way better at CSS and Graphic Design ;D
CapereSpiritum
Forum Members
Forum Members
Posts: 223
Joined: Wed Dec 28, 2011 12:11 pm

Re: Help with an IF statement in Company Directory

Post by CapereSpiritum »

Ignore the request above.
I added an IF statement that relates only to one type of entry that has premises... {if $entry->address ne ''}

As Bands and Artists don't have a fixed address, I am able to 'not show' other entries based upon no address entered.

Long winded, but it works...

If anyone can show the actual answer to my request, I'd be interested anyway.

Thanks
User avatar
velden
Dev Team Member
Dev Team Member
Posts: 3497
Joined: Mon Nov 28, 2011 9:29 am

Re: Help with an IF statement in Company Directory

Post by velden »

Code: Select all

{if (isset($entry->fields.BeerGarden) && $entry->fields.BeerGarden->value != '') || (isset($entry->fields.Jukebox) && $entry->fields.Jukebox->value != '') || (...etc..)}
Show header
{/if}
That would be one solution. Just as an example.

Note the parantheses around the AND 'statements'.

IF (A AND B) OR (C AND D) OR (E AND F)
&& = AND
|| = OR

http://www.smarty.net/docs/en/language.function.if.tpl

In real life you might want to do the check per field once and safe the result in variables. But it's not very easy to do in Smarty and doubt if it will be more readable.

pseudo code:

Code: Select all

{$result=''}
{if (isset($entry->fields.BeerGarden) && $entry->fields.BeerGarden->value != '')}
{$result=$result+<div class="VBeerGdn row col-tweleve cf">{$entry->fields.BeerGarden->value}</div>} <== this won't work
{/if}
{if (isset($entry->fields.JukeBox) && $entry->fields.JukeBox->value != '')}
{$result=$result+<div class="JukeBox row col-tweleve cf">{$entry->fields.JukeBox->value}</div>} <== this won't work
{/if}
...
{if $result != ''}
  <div>
    <h1>header</h1>
    {$result}
  </div>
{/if}

calguy1000
Support Guru
Support Guru
Posts: 8169
Joined: Tue Oct 19, 2004 6:44 pm

Re: Help with an IF statement in Company Directory

Post by calguy1000 »

In real life you might want to do the check per field once and safe the result in variables. But it's not very easy to do in Smarty and doubt if it will be more readable.
Incorrect, in any situation where an expression is long and has to be repeated, you execute the expression once, and save the results. You can also store the results in an array.

Also remember the empty() function which is shorter in this case and easy to use.

{$results=[]}
{* if you have more than 3 or 4 fields here I'd use a loop, it's cleaner *}
{if !empty($entry->fields.LiveMusic->value)}{$result[]='Live Music'}{/if}
{if !empty($entry->fields.JukeBox->value)}{$result[]='Juke Box'}{/if}
{if !empty($entry->fields.BeerGarden->value)}{$result[]='Beer Garden'}{/if}
...
{if !empty($results)}
<div class="results">
<h4>Results</h4>
<ul>
{foreach $results as $item}
<ul>{$item}</ul>
{/forach}
</ul>
</div>
{/if}

This is the simplest syntax I can think of and this is ugly and not recommended. So I suggest you spend a bit of time learning a bit more about CMSMS as it will save you alot of time and headache.

You really need to learn about some smarty concepts like:
- template inheritance
- include
and some cmsms concepts like the {cms_action_hint}

IN NO WAY should your templates try to accomplish more than one goal and be filled with all kinds of silly logic statements. CMSMS is smarter than that. you just have to learn a bit.
Follow me on twitter
Please post system information from "Extensions >> System Information" (there is a bbcode option) on all posts asking for assistance.
--------------------
If you can't bother explaining your problem well, you shouldn't expect much in the way of assistance.
calguy1000
Support Guru
Support Guru
Posts: 8169
Joined: Tue Oct 19, 2004 6:44 pm

Re: Help with an IF statement in Company Directory

Post by calguy1000 »

As CD does not support 'Pretty URLs' for some of the features.
i.e. detailtemplate, I'm having to use a single detail template for several types of details.
This is where you need to learn about:
a: template inheritance
b: {cms_module_hint}

You could use a separate detail page for each category
on that detail page use {cms_action_hint module=CompanyDirectory detailtemplate='category_specific_detail_template'}

Your category_specific_detail_template could 'extend' a base category detail template that has all of the common functionality, and just changes one or two custom 'blocks'.

Easy, and clean.
Follow me on twitter
Please post system information from "Extensions >> System Information" (there is a bbcode option) on all posts asking for assistance.
--------------------
If you can't bother explaining your problem well, you shouldn't expect much in the way of assistance.
CapereSpiritum
Forum Members
Forum Members
Posts: 223
Joined: Wed Dec 28, 2011 12:11 pm

Re: Help with an IF statement in Company Directory

Post by CapereSpiritum »

Hi Calguy

Many thanks for the replies.
I hope to put them to good use.

The website is nearing completion temp address http://teessiders.artworknetwork.co.uk/

This is likely to be my last website for a client as my wife and I are taking over a pub very soon. Other than a website for the pub, there is little likelihood of my needing to create any more.

CMSMS is a brilliant CMS and I have really enjoyed using it. Even the time I have pulled my hair out ;D

I'd like to thank you and all the other people that have responded to my queries and wish you well for the future.
Best regards
Simon Fall (Lunatic Chef)
User avatar
paulbaker
Dev Team Member
Dev Team Member
Posts: 1465
Joined: Sat Apr 18, 2009 10:09 pm
Contact:

Re: Help with an IF statement in Company Directory

Post by paulbaker »

Good luck with the pub! O0
To copy System Information to the forum:
https://docs.cmsmadesimple.org/troubles ... nformation

CMS Made Simple Geekmoots attended:
Nottingham, UK 2012 | Ghent, Belgium 2015 | Leicester, UK 2016
Post Reply

Return to “Modules/Add-Ons”