Multiple Where statements from one section...

3 posts by 2 authors in: Forums > CMS Builder
Last Post: November 8, 2012   (RSS)

By crazyfish - November 7, 2012

The client has a section named blog. It has a list from another section named blog_sections. List option from database, checkboxes, option values -num option labels - title.

Options include Press, Events, Inspirations etc.

On the home page they would like the one most recent post from each option to be displayed as seen here http://americadesigns.com/index1.php

I can't get my head wrapped around how to add the "where" type of statement in each loop, or one by one if I have to...

Originally it was the most recent six and has changed. Code for that process shown below.


// load records
list($blogRecords, $blogMetaData) = getRecords(array(
'tableName' => 'blog',
'perPage' => '6',
));


<?php foreach ($blogRecords as $record): ?>
<div class="blog_box">
<?php foreach ($record['thumb'] as $upload): ?>
<?php if ($upload['isImage']): ?>
<a href="<?php echo $record['_link'] ?>"><img src="<?php echo $upload['urlPath'] ?>" width="<?php echo $upload['width'] ?>" height="<?php echo $upload['height'] ?>" alt="" /></a><br/>
<?php else: ?>
<a href="<?php echo $upload['urlPath'] ?>">Download <?php echo $upload['filename'] ?></a><br/>
<?php endif ?>

<?php endforeach ?>
<div class="large"><?php echo $record['title'] ?></div>
<br/>
<?php echo $record['summary'] ?><br/>

</div>
<?php endforeach ?>


So - I need either one most recent record from each option shown or a way to filter out at this level options.

I can't seem to get head wrapped around the logic nor the code to accomplish this task.
As always, thanks for your help.

Re: [crazyfish] Multiple Where statements from one section...

By gregThomas - November 8, 2012

Hi,

So in the blog section there is series of checkboxes that link to another section called blog_sections?

I've done some testing and I think the best solution would be something like this:

<?php foreach ($blogRecords as $record): ?>
<div class="blog_box">
<?php foreach ($record['thumb'] as $upload): ?>
<?php if ($upload['isImage']): ?>
<a href="<?php echo $record['_link'] ?>"><img src="<?php echo $upload['urlPath'] ?>" width="<?php echo $upload['width'] ?>" height="<?php echo $upload['height'] ?>" alt="" /></a><br/>
<?php else: ?>
<a href="<?php echo $upload['urlPath'] ?>">Download <?php echo $upload['filename'] ?></a><br/>
<?php endif ?>

<?php endforeach ?>
<div class="large"><?php echo $record['title'] ?></div>

<br/>
<?php echo $record['summary'] ?><br/>

<?php
if($record['blog_sectionsFields:values'][0] > 0){
$additionString = implode(',',$record['blog_sectionsFields:values']);

list($additionsRecords, $additionsMetaData) = getRecords(array(
'tableName' => 'blog_sections',
'where' => "num IN ($additionString)",
'loadUploads' => true,
'allowSearch' => false,
'limit' => '1',
'orderBy' => 'createdDate DESC'
));
}
?>
<?php foreach ($additionsRecords as $record2): ?>
Record Number: <?php echo htmlencode($record2['num']) ?><br/>
Title: <?php echo htmlencode($record2['title']) ?><br/>
Content: <?php echo $record['content']; ?><br/>
<hr/>
<?php endforeach ?>

</div>
<?php endforeach ?>


You will need to swap the field names and section names so that they match the names you have used.

This works by creating a variable called $additionString string that contains a list of num values that have been checked in the blog section in the format x, x, x, x. You will need to change $record['blog_sectionsFields:values'] to the name of the field that contains the values from blog_sections and add :values to the end of it.

This is then added to the where statement of the getRecords function using the IN term. This will only return records that are in the $addtionString. I've also limited the where statement to only return one record, and that will be the newest record using the limit and orderBy keys in the getRecords array.

Let me know if you have any problems implementing this.

Thanks!
Greg Thomas







PHP Programmer - interactivetools.com