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 drewh01 - April 24, 2014

I tried that but get errors.

http://illuminatefilmfestival.com/films_test.php

Attached is the source code. But here is the part of the code that I am trying to work with:

<?php if ($film_list_page_controlsRecord['show_table_row']): ?>
<tr>
<td><?php if ($film_list_page_controlsRecord['date_time']): ?>
<?php if ($record['screening_time']): ?>
<span style="color:#05387F"><?php echo date("D, M jS g:i a", strtotime($record['screening_time'])) ?></span>
<?php endif ?>
<?php endif ?></td>

<td height="0"></td>

<td width="223"><?php if ($film_list_page_controlsRecord['screening_venue']): ?>
<?php if ($record['screening_venue']): ?>
<a href="http://illuminatefilmfestival.com/venues"><?php echo $record['screening_venue'] ?>
</a><?php endif ?>
<?php endif ?></td>

<td width="104"><?php if ($film_list_page_controlsRecord['more_info']): ?><br />
[<a href="<?php echo $record['_link'] ?>">More Info</a>]
<?php endif ?></td>
</tr>
<?php endif ?>

Hopefully you can see what I am trying to do. I was hoping to be able to set up a section where I can globally turn on and off specific parts of the list elements.

Thanks!!!

By drewh01 - April 25, 2014

Got it, thanks!!!!