aren Posted April 5, 2013 Share Posted April 5, 2013 Hey guys, Let's say I have a custom field named "Drink" - How can I output a list of all drinks based on this field values and sort it by name or date? Cheers, Aren Link to comment Share on other sites More sharing options...
diogo Posted April 5, 2013 Share Posted April 5, 2013 Welcome aren, Can you tell more about your "Drink" field? Where would those values be? How are drinks relating to each other in the admin? If you didn't decide those things yet, tell us what you want to do from the concept so we can give you suggestions on how to accomplish it. Link to comment Share on other sites More sharing options...
aren Posted April 5, 2013 Author Share Posted April 5, 2013 Hey diogo, I have a text field named "Drink" for each blog entry - where in each entry I'll add a kind of drink (eg: gin, water, beer etc) So what I want to do is create a list containing all drinks I've added in all blog entries so I'd have something like this: <h2>Drinks:</h2> <ul> <li>Beer</li> <li>Gin</li> <li>Water</li> etc </ul> In this example, the list is sorted alphabetically.. Link to comment Share on other sites More sharing options...
diogo Posted April 5, 2013 Share Posted April 5, 2013 Let's say your bog entries have a template called "entry" // finds all pages with template "entry" that have a non-empty "drink" field and sort alphabetically by the "drink" field $entries = $pages->find("template=entry, drink!=, sort=drink"); echo "<h2>Drinks:</h2>"; echo "<ul>"; foreach($entries as $entry){ echo "<li>"; echo $entry->drink; echo "</li>"; } echo "</ul>"; 1 Link to comment Share on other sites More sharing options...
aren Posted April 5, 2013 Author Share Posted April 5, 2013 Thanks Diogo! Can you just tell me what drink!= means? $entries = $pages->find("template=entry, drink!=, sort=drink"); Link to comment Share on other sites More sharing options...
diogo Posted April 5, 2013 Share Posted April 5, 2013 it just checks that the field is not empty. If for some reason you don't put anything on that field in some entries, they won't be included on the array, and there won't be any empty <li> on the site. edit: to explain better, doing (drink!=) is the same as doing (drink!=''),. Meaning: drink is not and empty string. Link to comment Share on other sites More sharing options...
Soma Posted April 5, 2013 Share Posted April 5, 2013 It means you get no empty drinks... Buahh 3 Link to comment Share on other sites More sharing options...
MatthewSchenker Posted April 5, 2013 Share Posted April 5, 2013 Greetings, What diogo is showing you is the SQL syntax for saying "not equal to." In other words, this statement says, "Find all pages using the 'entry' template where the 'drink' field is not empty." In other words, only grab pages where a drink was entered. The way it is shown now, you would get repeated drinks in your list. Are you hoping instead to just cite each drink that is mentioned at all in any blog post? Thanks, Matthew Link to comment Share on other sites More sharing options...
diogo Posted April 5, 2013 Share Posted April 5, 2013 Anyone else wants to step in and explain again the same? Link to comment Share on other sites More sharing options...
MatthewSchenker Posted April 5, 2013 Share Posted April 5, 2013 Greetings, Seems to happen a lot around here... Thanks, Matthew Link to comment Share on other sites More sharing options...
aren Posted April 5, 2013 Author Share Posted April 5, 2013 Greetings, What diogo is showing you is the SQL syntax for saying "not equal to." In other words, this statement says, "Find all pages using the 'entry' template where the 'drink' field is not empty." In other words, only grab pages where a drink was entered. The way it is shown now, you would get repeated drinks in your list. Are you hoping instead to just cite each drink that is mentioned at all in any blog post? Thanks, Matthew Ahh, yes I'd just like to show each drink featured in blog posts (no repeated drinks). Do I have to change something in the code? Thanks Matthew! Link to comment Share on other sites More sharing options...
Soma Posted April 5, 2013 Share Posted April 5, 2013 The best way would be tu use a page field with autocomplete. Like you can have a lost of drink and add one directly from autocomplete search. 1 Link to comment Share on other sites More sharing options...
diogo Posted April 5, 2013 Share Posted April 5, 2013 You could do it, but I can see already lots of problems in your structure, because , if you will have more information related to each drink, how will you do? repeat it in each entry? The best way to solve this in my opinion, is to create a "drinks" page, and start adding all your drinks as children of that page. You can have a template for drinks with the fields for the info. Then you just have to refer to that drink in the blog entry via a pageFieldType instead of a text field. For listing the drinks it also becomes simpler: $drinks = $pages->find("template=drink"); echo "<h2>Drinks:</h2>"; echo "<ul>"; foreach($drinks as $drink){ echo "<li>"; echo $drink->title; echo "</li>"; } echo "</ul>";And then you can do things as finding the articles that referred to one drink: $articles->find("template=article, drink.name=dry-martini"); To output the drink info inside the article is also easy: echo $page->drink->title; Edit: corrected typo. Link to comment Share on other sites More sharing options...
Joss Posted April 5, 2013 Share Posted April 5, 2013 If I had a list that included things like Gin and Beer, I doubt I would be able to sort it by anything by the time I got to the end...... 3 Link to comment Share on other sites More sharing options...
Martijn Geerts Posted April 5, 2013 Share Posted April 5, 2013 Can imagine, sort of..... Link to comment Share on other sites More sharing options...
aren Posted April 13, 2013 Author Share Posted April 13, 2013 You could do it, but I can see already lots of problems in your structure, because , if you will have more information related to each drink, how will you do? repeat it in each entry? The best way to solve this in my opinion, is to create a "drinks" page, and start adding all your drinks as children of that page. You can have a template for drinks with the fields for the info. Then you just have to refer to that drink in the blog entry via a pageFieldType instead of a text field. For listing the drinks it also becomes simpler: $drinks = $pages->find("template=drink"); echo "<h2>Drinks:</h2>"; echo "<ul>"; foreach($drinks as $drink){ echo "<li>"; echo $entry->title; echo "</li>"; } echo "</ul>"; Is the code above the same as the code below? <?php $drinks = $pages->find("template=drink"); ?> <h2><?php echo $page->title; ?></h2> <ul> <?php foreach($drinks as $drink); ?> <li><?php echo $entry->title; ?></li> </ul> Link to comment Share on other sites More sharing options...
DaveP Posted April 13, 2013 Share Posted April 13, 2013 (edited) You'll need to close the foreach, so something like this will work <?php $drinks = $pages->find("template=drink"); ?> <h2><?php echo $page->title; ?></h2> <ul> <?php foreach($drinks as $drink): ?> <li><?php echo $drink->title; ?></li> <?php endforeach; ?> </ul> Note that I've used a colon ( : ) after the foreach and then closed with endforeach. You could use foreach(){ and then <?php } ?>, but endforeach makes it clearer where the loop finishes (and which loop it is). Bit more info here. Edited re @nik's comment below and to change the colon back to a colon from being a smiley. Edited April 13, 2013 by DaveP 1 Link to comment Share on other sites More sharing options...
nik Posted April 13, 2013 Share Posted April 13, 2013 In addition to what @DaveP said you need to change $entry->title to $drink->title, as there's no variable called $entry there. That's a little typo in diogo's example left from the first version. 1 Link to comment Share on other sites More sharing options...
aren Posted April 13, 2013 Author Share Posted April 13, 2013 Thanks DaveP and nik! I find PHP a lot easier like that! Link to comment Share on other sites More sharing options...
diogo Posted April 13, 2013 Share Posted April 13, 2013 It was a typo indeed. I will correct it. Thanks nik. Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now