Hi there,
I’m creating a site that has a searchable database.
The database is made up of companies, the certificates those companies hold and the standards that those certificates relate to.
For example:
The site then has a search form which allows users to search by either certificate number or company name and download the relevant logos for those standards.
Still with me?
The certificate number is easy as they’re unique so only give one result but the company name is proving more tricky.
The trouble is that companies can hold the same standard more than once.
So if I search for Company A in the above example the results would show the logos for Standard 1 & 3 which is fine.
But if I search for Company B in the above example the results would show Standard 3 logos twice.
Any idea how I would limit this to just one set of logos per standard?
Thanks in advance,
Tom
Hi Tom,
I would try a couple of approaches:
Restructure your channels so that you had Company, Standard and Certificate as a separate channel. That way you can assign multiple Certificates to one Standard. If your search form returns results from the Standard channel, you would have one entry per Company per Standard.
If you want to get involved with adding PHP to your template, you could order your results by Standard, and then check if the current Standard is the same as the previous one. If it is, you can skip displaying it.
I personally would go with option one as it’s the most native and makes sense from a data perspective, i.e. it is more scalable.
Kind regards,
James
Thanks for your thoughts @seavers.
I’ve got company and standards in separate channels but certificate and company are the same entry as I’m importing the database and it seems to make sense to leave it that way.
Any ideas or links on how to use option #2 you mention (PHP)?
Thanks in advance,
Tom
It all depends on how you are associating the Standard with the Certificate (maybe a grid field with a relationship field for the Standard?).
The general theory is:
// initialise variable
<? $standard_logo == null; ?>
// channel entry loop
<? if ($standard_logo != '{standard:logo}') : ?>
{standard:logo}
<? endif ?>
$standard_logo = '{standard:logo}';
// end channel entry loop
So, before the end of the channel entry loop (or search results loop), you store the standard logo url and then compare that to the one in the next row of the results.
Thank you. It’s working but I’m now getting a ‘Undefined variable: standard_logo’ error?
Any ideas?
{exp:low_search:results
query="{segment_3}"
channel="certificates"
orderby="title"
sort="asc"
}
{!-- initialise variable --}
<?php $standard_logo == null; ?>
<?php if ($standard_logo != '{iso_accreditation_standard}'): ?>
<h3>{certificate_company_name}</h3>
<b>Certificate Number:</b> {title}
<b>ISO Accreditation Standard:</b> {iso_accreditation_standard}
<?php endif; ?>
<?php $standard_logo = '{iso_accreditation_standard}'; ?>
{if no_results}
Sorry, your search returned no results.
{/if}
{/exp:low_search:results}
Nearly there, you just need to initialise the $standard_logo variable outside of the results loop, like this:
{!-- initialise variable --}
<?php $standard_logo == null; ?>
{exp:low_search:results
query="{segment_3}"
channel="certificates"
orderby="title"
sort="asc"
}
<?php if ($standard_logo != '{iso_accreditation_standard}'): ?>
<h3>{certificate_company_name}</h3>
<b>Certificate Number:</b> {title}
<b>ISO Accreditation Standard:</b> {iso_accreditation_standard}
<?php endif; ?>
<?php $standard_logo = '{iso_accreditation_standard}'; ?>
{if no_results}
Sorry, your search returned no results.
{/if}
{/exp:low_search:results}
Thanks James but I’m still getting a couple of undefined variable errors?
Any other ideas?
{if segment_3 == ''}
{!-- CODE TO GO HERE --}
{if:else}
{!-- initialise variable --}
<?php $standard_logo == null; ?>
{exp:low_search:results
query="{segment_3}"
channel="certificates"
orderby="title"
sort="asc"
}
<?php
$iso_accreditation_standard = '{iso_accreditation_standard}';
$standard1 = (str_replace(' ', '-', strtolower($iso_accreditation_standard)));
$standard2 = (str_replace(':', '', strtolower($standard1)));
?>
<?php if ($standard_logo != '{iso_accreditation_standard}'): ?>
<h3>{certificate_company_name}</h3>
<b>Certificate Number:</b> {title}
<b>ISO Accreditation Standard:</b> {iso_accreditation_standard} (<?php print ($standard2); ?>)
<?php endif; ?>
<?php $standard_logo = '{iso_accreditation_standard}'; ?>
{if no_results}
Sorry, your search returned no results.
{/if}
{/exp:low_search:results}
<a href="http://{path=/logos}">Why not try another search</a>
{/if}
Perfect, thank you James!
For anyone looking to do the same here’s the final code:
{!-- initialise variable --}
<?php $standard_logo = null; ?>
{exp:low_search:results
query="{segment_3}"
channel="certificates"
orderby="title"
sort="asc"
}
<?php if ($standard_logo != '{iso_accreditation_standard}'): ?>
<h3>{certificate_company_name}</h3>
<b>Certificate Number:</b> {title}
<b>ISO Accreditation Standard:</b> {iso_accreditation_standard}
<?php endif; ?>
<?php $standard_logo = '{iso_accreditation_standard}'; ?>
{if no_results}
Sorry, your search returned no results.
{/if}
{/exp:low_search:results}
Packet Tide owns and develops ExpressionEngine. © Packet Tide, All Rights Reserved.