Jump to content

Handling categories based on checkbox


melissa_boyle
 Share

Recommended Posts

Hi guys,

I have a page which lists excerpts of content based on their category, this is chosen within the template by checkbox. I have all the content filtering through no problem but need to know how to display a simple "there are no posts available" if the checkbox has not been selected. I have tried the following but it doesn't seem to work :(.

if (count($child->Travel_escapes)<1) {
echo "<div class='post-content span12'>
<h2 class='error'>There are no posts in this section</h2>
</div>
";
}
 
Any help would be greatly appreciated,
 
Melissa
 
Link to comment
Share on other sites

is Travel_escapes the checkbox field? If that is the case, your code doesn't work because you are treating it as an array. This should work:

if ($child->Travel_escapes < 1) {

or even:

if (!$child->Travel_escapes) {
  • Like 4
Link to comment
Share on other sites

Thank you so much for your help Diogo, this makes sense but doesn't seem to work consistently I have included the full code below perhaps its the way I have formatted it?

<?php
foreach ($results as $child) {
//$image = $child->images->first->size(150, 100);
foreach($child->blog_image as $image) {
$large = $image; 
}
if ($child->seasonal==1) {
//why does this not work? $pagination = $results->renderPager();
echo "<div class='post-content span12'>";
 
echo "<div class='image span6'><a title='Read full story' href='post.php'>";
echo "<img title='Image' alt='Image' src='$large->url' /></a></div>";
echo "<div class='span6'><h2>$child->post_title</h2>";
echo "<div class='post-details'><span>Published $child->post_date ·<a title='seasonal' href='$child->url'>Seasonal";
echo "</a></span>";
echo "<p>$child->blog_excerpt</p>";
echo "<a href='{$child->url}' title='Read Full Story' class='blue-btn'>Read Full Story</a>
</div>
</div>
</div>";
}
}
 
if (!$child->seasonal) {
echo "<div class='post-content span12'>
<h2 class='error'>There are no posts in this section</h2>
</div>
";
}
 
 
?>
 
 
Thank you again for your help,
 
Melissa
Link to comment
Share on other sites

You could do something like

if (!$child->seasonal) {

Nothing to see here

}else{

all my lovely results 

}

If you want to display if it IS selected, then do not do 

if ($child->seasonal==1) {

but rather

if ($child->seasonal) {
  • Like 1
Link to comment
Share on other sites

Thank you Joss, I can't use the below as it will show other category posts. I have changed the code as suggested to: if ($child->seasonal)  but still get the "no posts" message even when their are posts for that category :(

if (!$child->seasonal) {

Nothing to see here

}else{

all my lovely results 

}

Thank you,

Melissa

Link to comment
Share on other sites

Mellissa, what are the actual fields you have for your template and what values do you have for the check boxes?

Also, since you are using check boxes, does this mean that something can be part of more than one category? If it should only belong to one, then checkboxes are a bad idea.

You can set up several conditions:

if (something && !somethingelse && !somerthingother){

But if you have variable categories, this is going to be very limiting. Normally, when I do categorisation, I use a select (via a page field) for the categories (that are saved separately) and then use find() to search for posts with a certain category. 

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