Include News content on main page

11 posts by 3 authors in: Forums > CMS Builder
Last Post: January 6, 2011   (RSS)

By ScottL - January 5, 2011

I'm setting up the index.php and I'll have two separate tables - basically just two framed boxes. I want to include the two most recent News entries in the boxes - one on the left box, and one on the right box. Can someone point me in the right direction to accomplish this?

Thanks

Re: [thinkng] Include News content on main page

By Jason - January 5, 2011

Hi,

The first thing you need to do is get the two latest news entries. In this example, I'm assuming the name of your table is "news"

list($newsRecords,$newsMetaData)=getRecords(array(
'tableName' => 'news'
'allowSearch' => false,
'orderBy' => "createdDate DESC",
'limit' => 2,
));


Next we assign these records to separate variables:

$leftNewsBox=array();
$rightNewsBox=array();

$leftNewsBox = @$newsRecords[0];
$rightNewsBox = @$newsRecords[1];


So we have the first record in $leftNewsBox and the second record in $rightNewsBox.

Last we do a check to make sure there is a record in each variable before we output our information:

<?php if($leftNewsBox):?>
*OUTPUT LEFT BOX NEWS RECORD*
<?php endif ?>

<?php if($rightNewsBox):?>
*OUTPUT RIGHT BOX NEWS RECORD*
<?php endif ?>


Give this a try.

Hope this helps
---------------------------------------------------
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: [thinkng] Include News content on main page

By Chris - January 5, 2011

Hi thinkng,

Assuming that your News section has 'title' and 'content' fields that you want to display, you can do this for your left news box:

<?php if($leftNewsBox):?>
<div>
<h3><a href="<?php echo $leftNewsBox['_link'] ?>">
<?php echo htmlspecialchars($leftNewsBox['title']) ?>
</a></h3>
<?php echo htmlspecialchars($leftNewsBox['content']) ?>
</div>
<?php endif ?>


Alternately, you can paste in code from a News detail page, changing $record to $leftNewsBox.

You can simply repeat that for your right news box, changing $leftNewsBox to $rightNewsBox.

Does that answer your question?

Also, is it possible to limit the text that is shown from the News, to a max number of words, then click to read the whole story?


Check out [url http://www.interactivetools.com/forum/gforum.cgi?post=66587#66587]this post on showing the first X words in a field[/url]. Does that help?

Please let me know if you have any questions.
All the best,
Chris

Re: [chris] Include News content on main page

By ScottL - January 5, 2011

I'm getting this error with the 'news' load records:

Parse error: syntax error, unexpected T_CONSTANT_ENCAPSED_STRING, expecting ')' in /home/content/52/7231752/html/indextest.php on line 28

It is the 'allowSearch' => true,



Here's the code:

<?php header('Content-type: text/html; charset=utf-8'); ?>
<?php


// load viewer library
$libraryPath = 'cmsAdmin/lib/viewer_functions.php';
$dirsToCheck = array('/home/content/52/7231752/html/','','../','../../','../../../');
foreach ($dirsToCheck as $dir) { if (@include_once("$dir$libraryPath")) { break; }}
if (!function_exists('getRecords')) { die("Couldn't load viewer library, check filepath in sourcecode."); }

// load records
list($main_pageRecords, $main_pageMetaData) = getRecords(array(
'tableName' => 'main_page',
'where' => whereRecordNumberInUrl(1),
'limit' => '1',
));
$main_pageRecord = @$main_pageRecords[0]; // get first record

// show error message if no matching record is found
if (!$main_pageRecord) {
header("HTTP/1.0 404 Not Found");
print "Record not found!";
exit;

list($newsRecords, $newsMetaData) = getRecords(array(
'tableName' => 'news'
'allowSearch' => true,
'orderBy' => "createdDate DESC",
'limit' => 2,
));
}
?>


Thanks

Re: [thinkng] Include News content on main page

By Jason - January 6, 2011

Hi,

You need to have a comma after your table name:

list($newsRecords, $newsMetaData) = getRecords(array(
'tableName' => 'news',
'allowSearch' => true,
'orderBy' => "createdDate DESC",
'limit' => 2,
));


Hope this helps
---------------------------------------------------
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] Include News content on main page

By ScottL - January 6, 2011

Thanks Jason

That cleared out that error message. I think everything is structured okay, but I'm getting this error:

Notice: Undefined variable: leftNewsBox in /home/content/52/7231752/html/indextest.php on line 57



Here's the code:


<?php header('Content-type: text/html; charset=utf-8'); ?>
<?php


// load viewer library
$libraryPath = 'cmsAdmin/lib/viewer_functions.php';
$dirsToCheck = array('/home/content/52/7231752/html/','','../','../../','../../../');
foreach ($dirsToCheck as $dir) { if (@include_once("$dir$libraryPath")) { break; }}
if (!function_exists('getRecords')) { die("Couldn't load viewer library, check filepath in sourcecode."); }

// load records
list($main_pageRecords, $main_pageMetaData) = getRecords(array(
'tableName' => 'main_page',
'where' => whereRecordNumberInUrl(1),
'limit' => '1',
));
$main_pageRecord = @$main_pageRecords[0]; // get first record

// show error message if no matching record is found
if (!$main_pageRecord) {
header("HTTP/1.0 404 Not Found");
print "Record not found!";
exit;

list($newsRecords, $newsMetaData) = getRecords(array(
'tableName' => 'news',
'allowSearch' => true,
'orderBy' => "createdDate DESC",
'limit' => 2,
));

$leftNewsBox=array();
$rightNewsBox=array();

$leftNewsBox = @$newsRecords[0];
$rightNewsBox = @$newsRecords[1];


}
?>
<h1>Main Page</h1>
<p><br/>
Title: <?php echo $main_pageRecord['title'] ?><br/>
Content: <?php echo $main_pageRecord['content'] ?><br/>
</p>



<div align="center">
<table width="80%" border="0" cellspacing="0" cellpadding="0">
<tr>


<td width="50%"><div align="center"> <br>
<table width="250" border="2" cellspacing="0" cellpadding="5" bgcolor="white" bordercolor="#629371">
<tr>
<td><?php if($leftNewsBox):?>
*OUTPUT LEFT BOX NEWS RECORD*
<?php endif ?> </td>
</tr>
</table>
<br>
</div></td>
<td width="50%"><div align="center"> <br>
<table width="250" border="2" cellspacing="0" cellpadding="5" bgcolor="white" bordercolor="#629371">
<tr>
<td><?php if($rightNewsBox):?>
*OUTPUT RIGHT BOX NEWS RECORD*
<?php endif ?></td>
</tr>
</table>
<br>
</div></td>
</tr>
</table>
<br />
<br />
</div>



<?php if (!$main_pageRecord): ?>
No record found!<br/>
<br/>
<?php endif ?>

Re: [thinkng] Include News content on main page

By Jason - January 6, 2011

Hi,

What's happening is all your variables are being defined inside an if statement where they shouldn't be. Change you're code at the top to look like this:

<?php


// load viewer library
$libraryPath = 'cmsAdmin/lib/viewer_functions.php';
$dirsToCheck = array('/home/content/52/7231752/html/','','../','../../','../../../');
foreach ($dirsToCheck as $dir) { if (@include_once("$dir$libraryPath")) { break; }}
if (!function_exists('getRecords')) { die("Couldn't load viewer library, check filepath in sourcecode."); }

// load records
list($main_pageRecords, $main_pageMetaData) = getRecords(array(
'tableName' => 'main_page',
'where' => whereRecordNumberInUrl(1),
'limit' => '1',
));
$main_pageRecord = @$main_pageRecords[0]; // get first record

// show error message if no matching record is found
if (!$main_pageRecord) {
header("HTTP/1.0 404 Not Found");
print "Record not found!";
exit;
}

list($newsRecords, $newsMetaData) = getRecords(array(
'tableName' => 'news',
'allowSearch' => true,
'orderBy' => "createdDate DESC",
'limit' => 2,
));

$leftNewsBox=array();
$rightNewsBox=array();

$leftNewsBox = @$newsRecords[0];
$rightNewsBox = @$newsRecords[1];

?>


That should take care of the error.
---------------------------------------------------
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] Include News content on main page

By ScottL - January 6, 2011 - edited: January 6, 2011

That worked! Thanks.

Any idea why the News text appears with html markup instead of being interpreted?


This is what I'm using:

<a href="<?php echo $leftNewsBox['_link'] ?>">
<?php echo htmlspecialchars($leftNewsBox['title']) ?>
</a></h3>
<?php echo htmlspecialchars($leftNewsBox['content']) ?>
</div>
<?php endif ?>



Also, within that code, what would I use to include an image from the News record?

Thanks

Re: [thinkng] Include News content on main page

By Chris - January 6, 2011 - edited: January 6, 2011

Hi thinkng,

htmlspecialchars() is what's causing the HTML to display instead of getting interpretted. Try this:

<?php echo $leftNewsBox['content'] ?>

The easiest way to get the code to display your record the way you want is to use the Code Generator to generate a detail page for your News section, then copy the parts you want into your page, and finally change the name of the variable from $record to either $leftNewsBox or $rightNewsBox. This includes displaying an image from your News records.

If you're having trouble, please post the News detail page that the Code Generator generates as an attachment and we'll show you how to stitch the two together.
All the best,
Chris