2 of 2
2
displaying subcats and grouping by custom field
Posted: 19 January 2008 12:44 AM   [ Ignore ]   [ # 19 ]  
Research Assistant
Avatar
RankRankRank
Total Posts:  671
Joined  06-29-2005

This loop may show promise, giving us all the state, county, city and entry info in a loop that we can perhaps filter with conditionals next. It employs two {exp:weblog:entries} tags nested within one another to pull it off.

<h2 class="sidetitle"><b>Restaurants</b></h2>

{exp:weblog:entries weblog="entries" category_group="12" limit="15" dynamic="off"}
{categories}
<h1>{category_name}</h1>

<
p>{ccf_county} County</p>
<
p>State of {ccf_state}</p>


{exp:weblog:entries weblog="entries" orderby="date" sort="asc" limit="1" dynamic="off"}


{title}{cf_rating}
{cf_address}
{cf_phone}
{cf_directions}
{cf_review}

<p></p>
{/categories}
{
/exp:weblog:entries}
{
/exp:weblog:entries}

I’ll leave a temp link here for you to see what we’ve got so far. the ccf_county and ccf_state fields are custom category fields (ccf), while the cf_rating, etc. are normal custom fields in the publish/edit page and weblog. As you can see, the city/state/county entries keep repeating presently with each restaurant/establishment entry. That is the first place I’ll filter.

On to phase two! Filtering!

Terry

 Signature 

Terry Leigh Britton
http://twitter.com/TerryBritton
http://twitter.com/Terr_ee

Search the web using Terry Leigh Britton to find all kinds of stuff I’m into!
Or use Yasni for Terry Leigh Britton

Profile
 
 
Posted: 19 January 2008 02:53 AM   [ Ignore ]   [ # 20 ]  
Research Assistant
Avatar
RankRankRank
Total Posts:  671
Joined  06-29-2005

I’ve got interesting things going on now, but absolutely had to use an embedded template to get it to work on one page. Still, this may prove instructive.

I used the count variable to eliminate the state and county headings on subsequent iterations of towns in the same county, but that won’t work on a page showing more than one city yet.

Here’s the index2 page code (link to that demo page here) - ugly but we’re going for function now!
index2 template

{embed="weblog_globals/_masthead"
section_name="entries"
page_title="entries"
}
{assign_variable
:master_weblog_name="entries"}

<div class="spacer"></div>

<
h2 class="sidetitle"><b>Restaurants</b></h2>
{exp:weblog:categories weblog="entries"}
<a href="{path=entries/index2}">City of {category_name}...{ccf_county} County, {ccf_state}</a>
{/exp:weblog:categories}


{embed
="entries/_listings" my_cat="{current_category}" my_county="{exp:weblog:category_heading}{ccf_county}{/exp:weblog:category_heading}"}
{
!-- my_county for later testing against county, perhaps. Not actually used yet. --}


</div>


<
br class="spacer" />


</
body>
</
html>

And here is the functional embedded template that displays the cities (once) and their restaurants:
_listings embed template:

{exp:weblog:entries weblog="entries" category_group="12" limit="15" category="{embed:my_cat}" dynamic="off"}

{categories}

{if count
== "1"}

<h1>{category_name}</h1>
<
p>State of {ccf_state}</p>
<
h3>County</h3>
<
p>{ccf_county} County</p>
{/if}


{exp
:weblog:entries weblog="entries" orderby="date" sort="asc" limit="1" dynamic="off"}


<h2><u>{title}</u></h2> Our Rating: {cf_rating}
<h3>Address</h3>
{cf_address}
<h3>Phone</h3>
{cf_phone}
<h3>Directions</h3>
{cf_directions}
<h3>Our Review:</h3>
{cf_review}

<p></p>
{/categories}
{
/exp:weblog:entries}
{if category_request}
{
/exp:weblog:entries}
{
/if}

Needed that last bit {if category_request} to hide the /exp:weblog:entries tag that kept showingupon landing with no link selected!

Hope this helps some. Styling and such needed, of course.

Terry

 Signature 

Terry Leigh Britton
http://twitter.com/TerryBritton
http://twitter.com/Terr_ee

Search the web using Terry Leigh Britton to find all kinds of stuff I’m into!
Or use Yasni for Terry Leigh Britton

Profile
 
 
Posted: 21 January 2008 10:23 AM   [ Ignore ]   [ # 21 ]  
Grad Student
Rank
Total Posts:  72
Joined  05-29-2007

hi terry,

back at it after a weekend away. i’ll spend a little time with you’re solution to understand….again, you’re going to have to show me where the clients will be able to add the name of the city, state - why am i so dense about this!!

til a little later,
v.

Profile
 
 
Posted: 21 January 2008 02:37 PM   [ Ignore ]   [ # 22 ]  
Grad Student
Rank
Total Posts:  72
Joined  05-29-2007

hi terry,

ok. beginning to see what you’re doing. going through mike boyink’s tutorial on the custom category fields and beginning to understand this http://www.boyink.com/splaat/comments/building-an-expressionengine-site-chapter-10/

and i’m starting to play a bit to see if i can dup some of what you’re doing. i’m simplifying the number of ccfs, but as i come up with something i’ll add it.

v.

Profile
 
 
Posted: 21 January 2008 04:40 PM   [ Ignore ]   [ # 23 ]  
Research Assistant
Avatar
RankRankRank
Total Posts:  671
Joined  06-29-2005

Sure - it is something to get used to, the custom category fields. The client will see them after you’ve created them every time they create a new category (city) in the locations category group, so it is easy for them to add cities, and I set the custom fields of county and state as required fields so they can’t leave them blank if they tried! The establishments (restaurants, etc.), of course, are actual weblog entries to which the city category is assigned.

Oh, and in case this wasn’t clear, the client can access the Edit Categories section of your control panel directly via the Categories tab of the Publish/Edit page - the link is below the list of categories they select from.


Let me know if you get hung up anywhere!

Terry

PS: One thing that is not made clear in Michael’s tutorial is that the “{category_image} URL” field could really contain any text - it itself is merely a custom category text field added in by default. You would still have to actually display the image whose path was in there with an <img class=“codeblock”> <img class="category_image" src="{category_image}" alt="{category_name}" title="{category_name}" height="112" width="150" /> </div>

As I was remarking on another thread, when I started out with EE I was afraid of “breaking something” if I started adding “custom” fields - or “custom” anythings! Well, all the default fields except “Title” are custom fields, whether we’re talking weblog publish page “custom fields” or “category custom fields”. We can add our own and feel quite unabashed about it without feelings of impropriety or undue risk!

Image Attachments
publish_cats.png
Click thumbnail to see full-size image
 Signature 

Terry Leigh Britton
http://twitter.com/TerryBritton
http://twitter.com/Terr_ee

Search the web using Terry Leigh Britton to find all kinds of stuff I’m into!
Or use Yasni for Terry Leigh Britton

Profile
 
 
Posted: 21 January 2008 06:33 PM   [ Ignore ]   [ # 24 ]  
Grad Student
Rank
Total Posts:  72
Joined  05-29-2007

well, i’m tryin’ but i’m not quite getting there! perhaps tomorrow!

i’ll keep you posted…

v.

Profile
 
 
Posted: 23 January 2008 02:22 AM   [ Ignore ]   [ # 25 ]  
Research Assistant
Avatar
RankRankRank
Total Posts:  671
Joined  06-29-2005

I am finally satisfied - We’ve accomplished much - sorting by county, then by city, with headings in the right places and not repeated for every entry. Now I will sleep.

This has one custom category field, called {ccf_county} which is a required field when the customer wishes to add a new category (a city name).

There are only four counties covered, so testing in this case can be hard-wired into the code by testing for the actual name. Should probably include a check for upper/lower case first letter.

The city is the actual category name, so it needs only to be isolated by county. Then keeping the city establishments all together and not merely presented in the order the entries (restaurants) were added is needed. And to cap things off, the city name now sits on top of all its restaurants.

See the Sample page (adapted from content on the actual website where this will be used) to see this short bit in action.

An additional bonus is using the {categories show=”{embed:my_cat}”} parameter to handle cases where there are two city/county locations of the same restaurant, keeping the two in their respective cities and counties and not showing atop one another twice! I figure the client will select both cities if the restaurant exists in two (or more) and so had to cover for that.

The variable {current_category} is a “Fresh Variable” made with that module. It has these settings:

current_category = {exp:weblog:entries limit=“1” dynamic=“off”}{categories}{category_id}{/categories}{/exp:weblog:entries}

These {current_category} Fresh variables could be replaced in the following code by simply entering in longhand in their place:

{exp:weblog:entries limit="1" dynamic="off"}{categories}{category_id}{/categories}{/exp:weblog:entries}

but this looks much, much neater!

Main snippet:

<div id="content">
<
h1>Restaurants</h1>

<
h2 class="listings">Butner County</h2>

{!-- sweep through categories to sift desired ones out --}
     {exp
:weblog:categories weblog="restaurants" style="linear" show_empty="no"}

{
!-- screen out county name from category custom field --}
     {if ccf_county
== "Butner"}

{
!-- display the city name --}
     
<h2>{category_name}</h2>   

{!-- workhorse -- nested category/entries sifting loop. This shows the restaurants by city. --}
     { embed
="entries/_listings4" my_cat="{category_id}" }

{
/if}
{
/exp:weblog:categories}

{
!-- the rest share the same code with county name change hardwired --}

<h2 class="listings">Orange County</h2>

{exp:weblog:categories weblog="restaurants" style="linear" show_empty="no"}
{if ccf_county
== "Orange"}
<h2>{category_name}</h2>
{ embed="entries/_listings4" my_cat="{category_id}" }
{
/if}
{
/exp:weblog:categories}


<h2 class="listings">Durham County</h2>

{exp:weblog:categories weblog="restaurants" style="linear" show_empty="no"}
{if ccf_county
== "Durham"}
<h2>{category_name}</h2>
{ embed="entries/_listings4" my_cat="{category_id}" }
{
/if}
{
/exp:weblog:categories}


<h2 class="listings">Forsyth County</h2>
{exp:weblog:categories weblog="restaurants" style="linear" show_empty="no"}

{if ccf_county
== "Forsyth"}
<h2>{category_name}</h2>
{ embed="entries/_listings4" my_cat="{category_id}" }
{
/if}
{
/exp:weblog:categories}

</div>


Embedded template (called “entries/_listings4” above)

{!-- treat cities within a county one at a time --}

     {exp
:weblog:entries weblog="restaurants" category="{embed:my_cat}" dynamic="off" orderby="title" sort="asc"}

{
!-- display restaurant name --}
     
<h3><u>{title}</u></h3>

{!-- display restaurant body text --}
     {cf_entry}

<p></p>

{/exp:weblog:entries}

Small code is elegant code. This is another very elegant example of how embeds can allow tasks not able to be accomplished except by filtering data and isolating ID variables, and then passing those to subroutines in another template. Very beautiful!

Enjoy!

Terry

[Edit:] I’ve created a wiki article out of this post, as I think this technique could enjoy many applications. If you do use it, please add a link there displaying where and how you did!

[Edit2:] I’ve updated this post to match the Wiki entry mentioned above.

 Signature 

Terry Leigh Britton
http://twitter.com/TerryBritton
http://twitter.com/Terr_ee

Search the web using Terry Leigh Britton to find all kinds of stuff I’m into!
Or use Yasni for Terry Leigh Britton

Profile
 
 
Posted: 09 February 2008 11:54 AM   [ Ignore ]   [ # 26 ]  
Summer Student
Avatar
Total Posts:  24
Joined  05-24-2003

Thanks for all this - I have been trying to get my head around how to accomplish something similar, a way to do a navigational listing which is grouped by multiple factors - categories and custom fields.

I was started to believe I would have to change all the custom fields to categories to get a grouping, or go the custom query route (which is not very future proof) but your approach gives me a start using standard functionality. cheese

I just have too many values to hard code them in the template like you did, so I will have to add another loop either from weblog or a custom query

I see how you used the “embed” option with variables to pass information from one weblog loop to another, so you can show variables from an outer loop in an inner loop, or use them for comparison. I should have thought of that!

The big question will remain as to how the performance is with so many queries and the amount of data I have - and this poor site I work on has been dragged from pmachine to ee beta, back to pmachine and now again in ee…

So thanks A LOT for posting this!!!

 Signature 

—-
err, what happened to all the posts I did back in 2002/2004? I want my fancier status back smile

http://www.iphi.net
Iphigenie in twitter / blipfoto / google / pownce / diigo / profilactic / mybloglog / and loads more
Superiphi in yahoo / flickr / lastfm / digg / technorati / upcoming and loads more

Profile
 
 
Posted: 09 February 2008 01:28 PM   [ Ignore ]   [ # 27 ]  
Research Assistant
Avatar
RankRankRank
Total Posts:  671
Joined  06-29-2005

Heh - it was a pleasure! I’ve updated the post to match the Wiki entry, as there was an extra step that was unnecessary as it turned out.

There are many things that require an outer loop placed inside an inner loop, to get around the dynamic quality of many tags, for instance, when between their own weblog:entries or weblog:categories tags (just discovered the path= tag in the query module is one of those). So, embedded templates really take care of those issues!

Enjoy!

Terry

 Signature 

Terry Leigh Britton
http://twitter.com/TerryBritton
http://twitter.com/Terr_ee

Search the web using Terry Leigh Britton to find all kinds of stuff I’m into!
Or use Yasni for Terry Leigh Britton

Profile
 
 
Posted: 09 February 2008 08:39 PM   [ Ignore ]   [ # 28 ]  
Summer Student
Avatar
Total Posts:  24
Joined  05-24-2003

Yes - I think this really will work for the kind of thing I am trying to do which is of the kind

status 1, genre 1
  cat1: entry - entry - entry
  cat2: entry - entry - entry
status 1, genre 2
  cat1: entry - entry - entry
  cat2: entry - entry - entry
status 2, genre 1
  cat1: entry - entry - entry
  cat2: entry - entry - entry
etc.
My challenge is simply that i have too many “genres” to reasonably do the hard coding you did, so I might have to do another {weblog} loop around it or a query, then use that parameter to generate the next step etc. One step at a time.

It’s a shame the weblog:category_archive option does not support many filtering options that exist for some of the other pairs (doesnt offer the search option, for example), since the inner loop in my example above would be done without work

 Signature 

—-
err, what happened to all the posts I did back in 2002/2004? I want my fancier status back smile

http://www.iphi.net
Iphigenie in twitter / blipfoto / google / pownce / diigo / profilactic / mybloglog / and loads more
Superiphi in yahoo / flickr / lastfm / digg / technorati / upcoming and loads more

Profile
 
 
   
2 of 2
2
 
Post Marker Legend
New Topic New posts Hot Topic Hot Topic with new posts New Poll New Poll Moved Topic Moved Topic Sticky Topic Sticky topic
Old Topic No new posts Hot Old Topic Hot Topic with no new posts Old Poll Old Poll Closed Topic Closed Topic Announcement Announcements
Theme
Change Theme
Visitor Statistics
The most visitors ever was 1743, on December 02, 2009 03:47 PM
Total Registered Members: 120412 Total Logged-in Users: 49
Total Topics: 126514 Total Anonymous Users: 30
Total Replies: 665260 Total Guests: 335
Total Posts: 791774    
Members ( View Memberlist )