If any item's checkbox is true

8 posts by 3 authors in: Forums > CMS Builder
Last Post: August 11, 2011   (RSS)

By rez - August 8, 2011 - edited: August 8, 2011

Each item has a "new_item" checkbox. how do I only show this list and the surrounding elements if any items checkbox is marked true? If there are no "new_items" I dont need to show anything, including the titles and rule.

list($itemsRecords, $itemsMetaData) = getRecords(array(
'tableName' => 'items',
));

<?php IF THERE ARE ANY RECORDS MARKED TRUE LIST THEM IF NOT, DONT SHOW ANYTHING BELOW ?>
<h2 class="title1">latest items</h2>
<img src="images/rule_8.jpg" width="270" height="40" alt="party supplies" />
<ul class="departmentlinks">
<?php foreach ($itemsRecords as $record): ?>
<?php if($record['new_item']):?>
<li><a href="<?php echo $record['_link'] ?>"><?php echo $record['name'] ?></a>
<?php endif?><?php endforeach ?>
</ul>
<?php END IF THERE ARE ANY RECORDS MARKED TRUE LIST THEM IF NOT, DO NOTHING?>


may have something to do with a where statement in the top code but i don't know how to put this together so even my title1 doesnt show if nothing is true.

Re: [rez] If any item's checkbox is true

By Jason - August 8, 2011

Hi,

First thing you would do is add "new_item" to your where clause, to only return records where the check box has been checked. Like this:

list($itemsRecords, $itemsMetaData) = getRecords(array(
'tableName' => 'items',
'where' => "new_item = '1'",
));


You can then only output your block if records were returned like this:

<?php if ($itemsRecords): ?>

<h2 class="title1">latest items</h2>
<img src="images/rule_8.jpg" width="270" height="40" alt="party supplies" />
<ul class="departmentlinks">
<?php foreach ($itemsRecords as $record): ?>
<?php if($record['new_item']):?>
<li><a href="<?php echo $record['_link'] ?>"><?php echo $record['name'] ?></a>
<?php endif?><?php endforeach ?>
</ul>

<?php endif ?>


Hope this helps
---------------------------------------------------
Jason Sauchuk - Project Manager
interactivetools.com

Hire me! Save time by getting our experts to help with your project.
http://www.interactivetools.com/consulting/

Re: [Jason] If any item's checkbox is true

By davidcmsb - August 8, 2011

Hi Jason,

I'm following this, trying to learn, and have a couple of questions.

1. With respect to your use of <?php if($record['new_item']):?>, I would have thought that this IF would have been something like if if($record['new_item'] ==1). Can you please explain and help me understand why you used instead <?php if($record['new_item']):?>? Does this catch and include an item if it is other than O, so that ==1 does not need to be used?

2. Why is there the need for this IF clause in light of the 'where' => "new_item = '1'", at the top? I would have thought that this would have only included records where the new_tem field was set to 1, which would obviate a need for the IF clause.

Thanks in advance for helping me understand and clarifying these things.

David

Re: [davidcmsb] If any item's checkbox is true

By Jason - August 8, 2011

Hi,

You're right, using 'where' => "new_item = '1'" will only return records where the check box has been checked. In the if statements, we're using the entire record set:


<?php if ($itemsRecords): ?>

This means that we'll only execute the if any records where returned.

Are you using this inside of a foreach loop? If you are, you can get rid of the if statement entirely since the foreach loop will only execute on records with the box checked.

hope this helps
---------------------------------------------------
Jason Sauchuk - Project Manager
interactivetools.com

Hire me! Save time by getting our experts to help with your project.
http://www.interactivetools.com/consulting/

Re: [davidcmsb] If any item's checkbox is true

By Jason - August 9, 2011

Hi David,

I think there was a misunderstanding as to where you wanted to use this code. Using MySQL to limit the records returned to only records where the check box has been checked means that you can do 1 of 2 things, depending where you need to control output:

Case 1: If you need to control the output for a single block of content that is not inside the foreach loop, you can use the <?php if ($itemsRecords):?> method. This means that we only want to output a block of content if records were returned (ie, 1 or more records exist with the check box checked).

Case 2: If the condition was meant for inside your foreach loop, then you can remove the if statement entirely. It won't have any use, since there would only be records to loop through if they had the box checked.

Hope this helps clarify.
---------------------------------------------------
Jason Sauchuk - Project Manager
interactivetools.com

Hire me! Save time by getting our experts to help with your project.
http://www.interactivetools.com/consulting/

Re: [Jason] If any item's checkbox is true

By rez - August 11, 2011 - edited: August 11, 2011

If understand what he might be confused about, I think this is another way of saying what Jason says in #1.

IN MY CASE, using only the "where" statement at the top, and removing what's in red would put this on my viewer page:



<h2 class="title1">latest items</h2>
<img src="images/rule_8.jpg" width="270" height="40" alt="party supplies" />
<ul class="departmentlinks">
</ul>


which would result in displaying the "latest items" title and a rule under it, along with an empty ul (probably adding white space) even though there may be no "new_items" checked because these things are outside the if loop / check.

Wrapping that whole thing in the additional if statement also hides the title and rule if there are no "new_items" checked.

I totally suck at getting to the point, asking and answering. lol. I think that may help though?

Re: [rez] If any item's checkbox is true

Thanks, Jason and Rez, that was helpful, don't know right now if I fully get it, but I think when I study this more I should. Rez, that was helpful and helped me better understand things here.