Creating a if statement inside of a foreach loop

5 posts by 3 authors in: Forums > CMS Builder
Last Post: April 25, 2014   (RSS)

By drewh01 - April 21, 2014 - edited: April 21, 2014

I want to create a separate section editor to control (via checkbox) when a field appears in a foreach loop appears using a IF statement.  (see attachment)

My reason to do this is to globally turn off the description (or any field) without having to go back through each record and deleting the text.

How can I do this?  I tried creating it, but received an error.

Notice: Undefined variable: film_list_page_controlsRecord in /home/illumina/public_html/includes/film-list-simple.php

See simple (not actual) example below:

<?php foreach ($filmsRecords as $record): ?>
<?php echo $record['title'] ?><?php echo $record['sub_title'] ?>
<?php if ($film_list_page_controlsRecord[‘more_info_description’]): ?>
<?php echo $record['description'] ?>
<?php endif ?>
<?php endforeach ?>

Is this even possible? If so, how can I make it work?

By Chris - April 24, 2014

Hi drewh01,

That's definitely possible, and you're going about it the right way. Did you add a getRecords() call to load the $film_list_page_controlsRecord variable at the top of the page?

// load record from 'site_general_information'
list($film_list_page_controlsRecords, $film_list_page_controlsMetaData) = getRecords(array(
  'tableName'   => 'film_list_page_controls',
  'where'       => '', // load first record
  'loadUploads' => true,
  'allowSearch' => false,
  'limit'       => '1',
));
$film_list_page_controlsRecord = @$film_list_page_controlsRecords[0]; // get first record
if (!$film_list_page_controlsRecord) { dieWith404("film_list_page_controls record not found!"); } // show error message if no record found

Does that help? If not, please post the PHP source code for your page as an attachment.

All the best,
Chris

By rconring - April 25, 2014

As Chris pointed out, You need to add this line of code to films_test.php after the viewer in order to place the first record in the film_list_page_controlRecord array:

$film_list_page_controlsRecord = @$film_list_page_controlsRecords[0]; // get first record

Ron Conring
Conring Automation Services
----------------------------------------
Software for Business and Industry Since 1987

By drewh01 - April 25, 2014

Got it, thanks!!!!