We use cookies to improve your experience. No personal information is gathered and we don't serve ads. Cookies Policy.

ExpressionEngine Logo ExpressionEngine
Features Pricing Support Find A Developer
Partners Upgrades
Blog Add-Ons Learn
Docs Forums University
Log In or Sign Up
Log In Sign Up
ExpressionEngine Logo
Features Pro new Support Find A Developer
Partners Upgrades
Blog Add-Ons Learn
Docs Forums University Blog
  • Home
  • Forums

Limiting results to one entry for each custom field variant?

How Do I?

Evolve Websites's avatar
Evolve Websites
110 posts
3 years ago
Evolve Websites's avatar Evolve Websites

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:

  • Company A: Standard 1 (Certificate 123), Standard 3 (Certificate 124)
  • Company B: Standard 2 (Certificate125), Standard 3 (Certificate 126), Standard 3 (Certificate 127)

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

       
seavers's avatar
seavers
20 posts
3 years ago
seavers's avatar seavers

Hi Tom,

I would try a couple of approaches:

  1. 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.

  2. 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

? 1
       
Evolve Websites's avatar
Evolve Websites
110 posts
3 years ago
Evolve Websites's avatar Evolve Websites

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

       
seavers's avatar
seavers
20 posts
3 years ago
seavers's avatar seavers

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.

       
Evolve Websites's avatar
Evolve Websites
110 posts
3 years ago
Evolve Websites's avatar Evolve Websites

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}
       
seavers's avatar
seavers
20 posts
3 years ago
seavers's avatar seavers

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}
       
Evolve Websites's avatar
Evolve Websites
110 posts
3 years ago
Evolve Websites's avatar Evolve Websites

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}
       
seavers's avatar
seavers
20 posts
3 years ago
seavers's avatar seavers

Apologies, the initialise variable should probably be:

<?php $standard_logo = null; ?>

Note only one equals sign.

Or failing that:

<? $standard_logo = ''; ?>
       
Evolve Websites's avatar
Evolve Websites
110 posts
3 years ago
Evolve Websites's avatar Evolve Websites

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}
       

Reply

Sign In To Reply

ExpressionEngine Home Features Pro Contact Version Support
Learn Docs University Forums
Resources Support Add-Ons Partners Blog
Privacy Terms Trademark Use License

Packet Tide owns and develops ExpressionEngine. © Packet Tide, All Rights Reserved.