Jump to content

Databases: Detecting if the category has featured records


Meddysong
 Share

Recommended Posts

I'm building a template for my database and am currently customising the listing template. I want it to be similar to the groups directory, in that I would like the list of records to show the featured ones in one table, rather than list them with the other records.

I know how to identify whether my category has any rows:

{{if count($rows)}}

With that, I would normally call the recordRow template.

I need to keep that as it is but have another line which means "if there are any rows which are featured". That's where the problem lies. If I could work that out, I'd run a foreach loop for those records to generate a list of them.

This leads me on to a second problem. Since the featured records will already listed on the same page but in another table, I don't need them duplicating in the normal table. I would need to rewrite 

{{if count($rows)}}

to be "if there are any rows which aren't featured" and then adjust recordRow so that only unfeatured records are run for the foreach loop.

Does anybody know what syntax I need to pull this off?

Link to comment
Share on other sites

If you're not going to query for them separately, you'll need to loop twice... pull out your featured rows, and unset them from $rows.

foreach $rows as $id => $recordRows
{
  if($recordRows->featured)
  {
  $featured[] = $recordRows;
  unset($rows[$id]);
  }
}

// Then loop through $rows again, you'll only have unfeatured

// Loop through $featured, it'll have all your featured rows

pseudo code of an option.  There is likely a more efficient way to do this outside of the template, but I've not worked with Pages 4.x much over the last year to be able to point you to it.

Link to comment
Share on other sites

Thank you - that's provided me with a good starting point :)

The syntax needs to be a bit different and I'm not versed in PHP, so experimentally I've started with this:

{{foreach $rows as $id => $recordRows}}
	<p>Record code will be added later</p>
{{endforeach}}

and that's correctly printing the HTML four times, reflecting the four records in the category. But if I try the following to see whether I can get it to narrow down to the two featured ones

{{foreach $rows as $id => $recordRows}}
	{{if ($recordRows->featured)}}
		<p>Record code will be added later</p>
	{{endif}}
{{endforeach}}

I'm not getting any returns at all.

{{if ($recordRows->featured)}}

doesn't seem to ever evaluate to TRUE. Have I set that out incorrectly?

(I know I've ignored the principle you kindly set out about unsetting the featured rows from the group. I need to work out what the correct syntax looks like and then confirm the results with a basic example like this before I dare venture to something slightly more above basic.)

Link to comment
Share on other sites

This is proving a little problematic, no doubt owing to my own inexperience.

If there are any featured records, then I need to create a table. Within that table I can run a foreach loop to add the relevant records.

My problem is knowing how to check for the existence of featured records without first using a foreach loop. I tried

{{if count($rows->mapped('featured'))}}

and that produced an error, so I've learned that this isn't the approach to take.

I already know that I can do this

{{foreach $rows as $id => $recordRows}}    	
	{{$featured = $recordRows->mapped('featured');}}

to allow me to check for the existence of $featured:

{{if ! empty($featured)}}

The problem is that this last line is only evaluating to TRUE if I use it within the foreach loop. I need to use it outside of a loop because it's going to be the start of a table. And if I do that, I'm not getting anything back.

(A side thing that is confusing me is that {{if ! empty($featured)}} is producing two rows (correct), whilst {{if count($featured)}} produces five (the number of records in the database but not the number of featured ones).)

So:

- I need to establish whether there are any featured records
- I can do this within a foreach loop
- I need to do it outside a foreach loop because I'm only using this as a check and don't want any looping
- I don't seem able to do this outside a foreach loop.

So, long story short: given that I have $rows and that it's possible for a row to be featured ($row->mapped('featured')), how do I establish the presence of a featured row without running the check in a foreach loop?  

Link to comment
Share on other sites

You can’t do it outside a loop, because it is not stored as a separate entry outside the loop. You have to look at each entry and check them individually.

Aiwa’s code was to split records into featured records and those which are not. If you only want a check, just store a result instead in the loop.  

{{if $recordRows->mapped('featured')}}
  {{$there_are_featured_records=1;}}
{{endif}}

You can then check that variable later. 

Edited by opentype
Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...