Archiving News stories on same page

10 posts by 2 authors in: Forums > CMS Builder
Last Post: April 5, 2013   (RSS)

By Kittybiccy - January 15, 2013

Hi guys,

I have a blog type page where the user will publish articles (records) in a list. They would like older records to be displayed in a seperate list on the same page (when a checkbox is ticked as 'archive'). Could someone please tell me how I can get the "current" records to display in one list on the page and those records ticked as 'archive' display in a different list on the page?

Ideally, the archived records would just display the title of the article whereas the "current" articles would display several fields which link to the detail page.

Thanks in advance!

Hannah

By gregThomas - January 15, 2013

Hi Hannah,

You can use the getRecords function to get either live or archived blog entries by adding a where statement to it. A check box value is stored in CMS Builder as either a 1 if a check box is checked, and a 0 if it isn't checked. So the following getRecords functions should work:

//Set the archived blog records to the $blogArchive variable
list($blogArchive, $blogArchData) = getRecords(array(
  'tableName' => 'blog',
  'loadUploads' => true,
  'allowSearch' => false,
  'where' => "archive = '0'"
));


// Set the live blog entries to the $blogLive variable
list($blogLive, $blogArchData) = getRecords(array(
  'tableName' => 'blog',
  'loadUploads' => true,
  'allowSearch' => false,
  'where' => "archive = '1'"
));

You might need to change the table name and the name of your archive field.

Then to display the data stored in the $blogLive and $blogArchive variables, something similar to this HTML code should work:

<div style="background-color: grey;float:right; width:300px">
<h3>Archived Blogs</h3>
<!-- STEP1: Display Archied records -->
<?php foreach ($blogArchive as $record): ?>
Record Number: <?php echo htmlencode($record['num']) ?><br/>
Archive (value): <?php echo $record['archive'] ?><br/>
Archive (text): <?php echo $record['archive:text'] ?><br/>
Title: <?php echo htmlencode($record['title']) ?><br/>
Date: <?php echo date("D, M jS, Y g:i:s a", strtotime($record['date'])) ?><br/><!-- For date formatting codes see: http://www.php.net/date -->
_link : <a href="<?php echo $record['_link'] ?>"><?php echo $record['_link'] ?></a><br/>
<hr/>
<?php endforeach ?>

<?php if (!$blogLive): ?>
No records were found!<br/><br/>
<?php endif ?>
<!-- /STEP1: Display Records -->
</div>


<!-- STEP2: Display Live Records (Paste this where you want your records to be listed) -->
<h1>Blog - Live Entries</h1>
<?php foreach ($blogLive as $record): ?>
Record Number: <?php echo htmlencode($record['num']) ?><br/>
Archive (value): <?php echo $record['archive'] ?><br/>
Archive (text): <?php echo $record['archive:text'] ?><br/>
Title: <?php echo htmlencode($record['title']) ?><br/>
Date: <?php echo date("D, M jS, Y g:i:s a", strtotime($record['date'])) ?><br/><!-- For date formatting codes see: http://www.php.net/date -->
Content: <?php echo $record['content']; ?><br/>
jhgjhg (values): <?php echo join(', ', $record['jhgjhgjhgjhg:values']); ?><br/>
jhgjhg (labels): <?php echo join(', ', $record['jhgjhgjhgjhg:labels']); ?><br/>
_link : <a href="<?php echo $record['_link'] ?>"><?php echo $record['_link'] ?></a><br/>

<!-- STEP 2a: Display Uploads for field 'uploads' (Paste this anywhere inside STEP2 to display uploads) -->
<!-- Upload Fields: extension, thumbFilePath, isImage, hasThumbnail, urlPath, width, height, thumbUrlPath, thumbWidth, thumbHeight, info1, info2, info3, info4, info5 -->
Uploads:
<blockquote>
<?php foreach ($record['uploads'] as $index => $upload): ?>
Upload Url: <?php echo $upload['urlPath'] ?><br/>

<!-- Uploads: Copy the tags from below that you want to use, and erase the ones you don't need.

Thumb Url: <?php echo $upload['thumbUrlPath'] ?><br/><br/>
Download Link: <a href="<?php echo $upload['urlPath'] ?>">Download <?php echo $upload['filename'] ?></a><br/><br/>

Image Tags:<br/>
<img src="<?php echo $upload['urlPath'] ?>" width="<?php echo $upload['width'] ?>" height="<?php echo $upload['height'] ?>" alt="" />
<img src="<?php echo $upload['thumbUrlPath'] ?>" width="<?php echo $upload['thumbWidth'] ?>" height="<?php echo $upload['thumbHeight'] ?>" alt="" /><br/>

info1 (Title) : <?php echo htmlencode($upload['info1']) ?><br/>
info2 (Caption) : <?php echo htmlencode($upload['info2']) ?><br/><br/>

Extension: <?php echo $upload['extension'] ?><br/>
isImage: <?php if ($upload['isImage']): ?>Yes<?php else: ?>No<?php endif ?><br/>
hasThumbnail: <?php if ($upload['hasThumbnail']): ?>Yes<?php else: ?>No<?php endif ?><br/>
<hr/>
// end uploads comment tag -->

<?php endforeach ?>
</blockquote>
<!-- STEP2a: /Display Uploads -->

<hr/>
<?php endforeach ?>

<?php if (!$blogLive): ?>
No records were found!<br/><br/>
<?php endif ?>
<!-- /STEP2: Display Records -->

You will probably need to change the field names in the code above I've used above. 

Let me know if you have any problems implementing the code.

Thanks!

Greg

Greg Thomas







PHP Programmer - interactivetools.com

By Kittybiccy - January 17, 2013

Hi Greg,

I think I'm being a complete idiot - I can't get it to work. I changed the table name to 'comment' to work with my code but now I'm getting no record found. Is it OK if I attach the page and you can see what I've done wrong? The page is also pulling in information from another record section.

Thanks!

Attachments:

comment-2050-blog.php 12K

By gregThomas - January 17, 2013

I've had a look through your code, everything looks correct as far as I can tell. Is the name of your archive field definitely 'archive'? 

Thanks

Greg

Greg Thomas







PHP Programmer - interactivetools.com

By Kittybiccy - January 21, 2013

Hi Greg,

Yes, that field is 'archive'. Any ideas? :)

By gregThomas - January 21, 2013

Hi Hannah,

I'm still not sure what's causing the issue. Could you fill out a second level support request for me?

https://www.interactivetools.com/support/email_support_form.php

If I can see the structure of your sections in CMS Builder I think I'll be able to come up with an answer.

Thanks!

Greg

Greg Thomas







PHP Programmer - interactivetools.com

By gregThomas - January 21, 2013

Hi Hannah,

I can see what the problem is now.

The comment section is using a dropdown menu to store if the article is archived or not, so the the database will store the value the user has selected from that. I made the following changes to your getRecord functions comment-2050-blog.php file:

//Set the archived blog records to the $blogArchive variable
list($commentArchive, $commentArchData) = getRecords(array(
'tableName' => 'comment',
'loadUploads' => true,
'allowSearch' => false,
'where' => "archive = 'Archive Article'"
));


// Set the live blog entries to the $blogLive variable
list($commentLive, $commentArchData) = getRecords(array(
'tableName' => 'comment',
'loadUploads' => true,
'allowSearch' => false,
'where' => "archive = 'Current Article'"
));

I've created a test file called comment-2050-blog-test.php for you, and uploaded it to your server. 

Thanks!

Greg

Greg Thomas







PHP Programmer - interactivetools.com

By Kittybiccy - April 5, 2013

Hi guys,

This all worked fine but I now want to apply it to another site, this time where the checkboxes relate to a day (Mon, Tues, Weds) on an events listing record. 

I shall try and explain what I need:
So you have a list of events, listed by day - Mon, Tues, Weds - on the page. These are separate multi records.
There is also a list for 'Other Events' which is displayed after these events. These are a different set of multi records.
What I need to do, is get the 'Other Events' that happen on the Monday to display with the Monday info, the 'Other Events' for Tues to display with the Tues events and same for Weds.
The 'Other Events' records use a checkbox to define which day they come under.

Does that make sense?

The code you gave me before, how would I adjust for Mon, Tues and Weds? Something like this?

//Set the archived blog records to the $blogArchive variable
list($othereventsArchive, $othereventsArchData) = getRecords(array(
'tableName' => 'otherevents',
'loadUploads' => true,
'allowSearch' => false,
'where' => "archive = 'Mon'"
));


// Set the live blog entries to the $blogLive variable
list($othereventsArchive, $othereventsArchData) = getRecords(array(
'tableName' => 'comment',
'loadUploads' => true,
'allowSearch' => false,
'where' => "archive = 'Tues'"
));

and then for each one:

<?php if (!$TuesLive): ?>

Any help greatly appreciated!!

By gregThomas - April 5, 2013

Hi,

I think the best way to do it is to filter out each day of the week individually would be like this:


  //get other events records
  list($otherevents, $othereventsArchData) = getRecords(array(
    'tableName' => 'otherevents',
    'loadUploads' => true,
    'allowSearch' => false,
  ));


  //Get the events records (table name probably needs changing)
  list($events, $othereventsArchData) = getRecords(array(
    'tableName' => 'events',
    'loadUploads' => true,
    'allowSearch' => false,
  ));

//Work out which array has more records
$largestCounter = max(count($events), count($otherevents));

?>
  <!-- set day of the week to filter -->
  <?php $day = 'Mon'; ?>
  <!-- Cycle through all of the events/otherevents -->
  <?php for($i=0;$i<$largestCounter;$i++): ?>
    <!-- if there is a record in the events array for this value of i, set it to the variable event -->
    <?php $event      = @$events[$i]; ?>
    <!-- if there is a record in the other events array for this value of i, set it to the variable other event -->
    <?php $otherevent = @$otherevents[$i]; ?>
    
    <!-- If the event records day matches the current day variable -->
    <?php if(@$event['archive'] == $day): ?>
      <!-- display its title (assuming it exists) -->
      <p><?php echo $event['title']; ?></p>
    <?php endif; ?>
    <!-- repeat for other event -->
    <?php if(@$otherevent['archive'] == $day): ?>
      <p><?php echo $otherevent['title']; ?></p>
    <?php endif; ?>
  <?php endfor; ?>

  <!-- Repeated for Tuesday -->
  <?php $day = 'Tues'; ?>
  <?php for($i=0;$i<$largestCounter;$i++): ?>
    <?php $event      = @$events[$i]; ?>
    <?php $otherevent = @$otherevents[$i]; ?>
    <?php if(@$event['archive'] == $day): ?>
      <p><?php echo $event['title']; ?></p>
    <?php endif; ?>
    <?php if(@$otherevent['archive'] == $day): ?>
      <p><?php echo $otherevent['title']; ?></p>
    <?php endif; ?>
  <?php endfor; ?>

This is just example code, so you'll have to make a few changes to get it working (most likely the section names and variables).

So the getRecords function is set to retrieve all of the records from the otherevents and events sections. Then the largestCounter variable is used to work out which array has more records, this is then used to loop through all of the records.

If the field archive matches the day variable, it displays its contents. 

Let me know if you have any questions.

Thanks!

Greg

Greg Thomas







PHP Programmer - interactivetools.com