Jump to content

Display a list based on custom field


aren
 Share

Recommended Posts

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

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

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>";
  • Like 1
Link to comment
Share on other sites

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

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

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

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

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

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 by DaveP
  • Like 1
Link to comment
Share on other sites

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.

  • Like 1
Link to comment
Share on other sites

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

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