"if" statements for no matching criteria

8 posts by 2 authors in: Forums > CMS Builder
Last Post: March 25, 2010   (RSS)

Hi All,

I think my mind went to sleep.

I have a checkbox field in a multi-record editor and I want to be able to output a “no records found with the checkbox checked” message if there are no records with the checkbox checked.

I’ve tried a number of iterations of "if" statements and can’t seem to get it right. The latest is:
<?php if (!$my_tableRecord['my_field'] == 1): ?“no records found with the checkbox checked <?php endif ?>
Which only seems to work at all if there’s an:
$e_blast_events_noticeRecord = @$e_blast_events_noticeRecords[0]; // get first record
But then, it obviously only toggles for the first record. Remove the code and also obvious, there's a not defined error.

_________________________________

In the same viewer I’m using a "where" statement to restrict which dates are displayed in a group.

I want to output the same kind of “no records found meeting the where clause restrictions” notice if there are no records that meet those restrictions. I have absolutely no idea how to set this up.

Here’s the "where" statement that I’m using:
<?php
list($my_tableRecords, $my_tableMetaData) = getRecords(array(
'tableName' => 'my_table',
'where' => '(NOW() + INTERVAL 7 DAY) >= event_reception_date AND event_reception_date >= TIMESTAMP(CURDATE(), "00:00:00") OR (NOW() + INTERVAL 7 DAY) >= event_start_date AND event_start_date >= TIMESTAMP(CURDATE(), "00:00:00")',
));

?>


Thanks,

Jerry Kornbluth
The first CMS Builder reference book is now available on-line!







Take advantage of a free 3 month trial subscription, only for CMSB users, at: http://www.thecmsbcookbook.com/trial.php

Re: [gkornbluth] "if" statements for no matching criteria

By Jason - March 24, 2010

Hi Jerry,

Was wondering if you could clarify the problem a little. Do you want to display a "no records found" message when neither the date or checkbox restrictions are met or do you want to display 2 separate messages (one for the checkbox and one for the date restriction)?

Maybe you could attached your viewer file and I could take a look at the code for you.

Thanks.
---------------------------------------------------
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" statements for no matching criteria

By gkornbluth - March 24, 2010 - edited: March 24, 2010

Thanks Jason,

2 separate messages, the viewer criteria are independent and are in two separate areas of the viewer.

The first is a simple check for a value in a checkbox.

<?php foreach ($e_blast_events_noticeRecords as $record): ?><?php if ($record['apbc_event'] == 1): ?>

my code...

<?php endif; ?>
<?php endforeach; ?>


I want to be able to display a “no records found with the checkbox checked” message if there are no records with the checkbox checked.

The other is the same requirement for the "where" statement noted above.

best,

Jerry
The first CMS Builder reference book is now available on-line!







Take advantage of a free 3 month trial subscription, only for CMSB users, at: http://www.thecmsbcookbook.com/trial.php

Re: [gkornbluth] "if" statements for no matching criteria

By Jason - March 24, 2010

Hi,

Okay. So for the first one, you're going through each record and checking to see if the checkbox field is 1. What you need to do is something like this: <?php $ischecked=0; ?><?php foreach ($e_blast_events_noticeRecords as $record): ?><?php if ($record['apbc_event'] == 1): ?> <?php $ischecked=1; /><?php endif; ?>
<?php endforeach; ?>
<?php echo $ischecked==0 ? "No records found"; ?>
So the variable $ischecked will remain 0 unless a record is found that has apbc_event set to 1. If the variable is still 0 at the end of the loop, then no record was found.For the second one, you just want to test to see if any records were retruned using that "where" clause. This can be done like this:<?php echo count($getRecords)==0 ? "No records found"; ?>The function count() just returns the number of elements in an array. So if the array has a length of 0, then there were no records returned from the database. Let me know if that works.
---------------------------------------------------
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: [gkornbluth] "if" statements for no matching criteria

By Jason - March 25, 2010

Hi Jerry,

Sorry about that. I think this will work better. For the first scenario, try this code:
<?php $ischeck=0; ?>
<?php foreach ($e_blast_events_noticeRecords as $record): ?>
<?php if($record['check']==1) $ischeck=1; ?>
<?php endforeach ?>
<?php echo $ischeck==0 ? "No Records Found"; ?>


So this will output the message "No Records Found" if there are no records in the database with the field "check" set to 1.

For the second scenario:
<?php
list($e_blast_events_noticeRecords, $e_blast_events_noticeMetaData) = getRecords(array(
'tableName' => 'e_blast_events_notice',
'where' => '((NOW() + INTERVAL 7 DAY) >= event_reception_date AND event_reception_date >= TIMESTAMP(CURDATE(), "00:00:00")) OR ((NOW() + INTERVAL 7 DAY) >= event_start_date AND event_start_date >= TIMESTAMP(CURDATE(), "00:00:00"))',
));
if(!$e_blast_events_noticeRecords) echo "No Records Found.";
?>


Again, this will print the message "No Records Found" if there are no records in the database that meet those requirements. If there are records, however, it will display nothing.

Let me know if that works better.
---------------------------------------------------
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" statements for no matching criteria

Hi Jason,

Thanks for the update.

I actually got it to work with a few minor modifications.

For the check box: <?php $ischeck= 0; ?>
<?php foreach ($e_blast_events_noticeRecords as $record): ?>
<?php if($record['apbc_event']==1) $ischeck=1; ?>
<?php endforeach ?>

<?php if ($ischeck==0) echo "no records found."; ?>


And for the where statement I pulled the

if(!$e_blast_events_noticeRecords) echo "No events are opening or have a reception during the next 7 days."; out and created 2 separate "if" statements so that I could style them separately.

<?php if(!$e_blast_events_noticeRecords) echo "No events are opening or have a reception during the next 7 days.";
?>
And...
<?php if($e_blast_events_noticeRecords) echo "These events are opening or have a reception during the next 7 days."; ?>

One question. How come (I guess what are the rules that govern) not requiring an <?php endif; ?> at to close an "if" statement?

Again thanks for your help.

Best,

Jerry Kornbluth
The first CMS Builder reference book is now available on-line!







Take advantage of a free 3 month trial subscription, only for CMSB users, at: http://www.thecmsbcookbook.com/trial.php

Re: [gkornbluth] "if" statements for no matching criteria

By Jason - March 25, 2010

Hi Jerry,

Good to hear that worked out for you. You only need <?php endif ?> if your "if" statement covers more than 1 <?php?> block.

ex:

<?php if($x==1?>

----some code---

<?php endif ?>


This could also be put into 1 <?php?> block with no endif

ex:

<?php if($x==1){

---some code--

}

?>

---------------------------------------------------
Jason Sauchuk - Project Manager
interactivetools.com

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