Random Content part from a given Article ID
Posted: Tue 23. Oct 2007, 22:49
hi all, I realise that SHOW_CONTENT is now built in and I use it/ recommend it thoroughly
what I have spent a few mins adapting it to do a small job i need... show a random content part from a given article - think of it as a 'random quotes' script on steroids - the steroids are that it can show any content part not just plain text.
OK- a small adaptation to Jens's script means a big thanks and all credit go to him.
A working version can be seen http://www.teensunitefightingcancer.org as the 'QUOTES' box in the {RIGHT} column - this is a live site so no funny business please!
As it's a replacement tag, you know the procedure by now... save this code as a file name in template/inc_script/frontend_render/randomCP.php
then use it by adding the article ID in the tag as per the script header blurb... {RANDOMCP:xx}
hope it helps, pSouper
__________
A 'minor bug-ette' is that I had to comment out the line that checks if the article end date has past but I can't figure out why it does not work - basically, with the line in it only show the same CP over & over again. any help with this would be very nice too
what I have spent a few mins adapting it to do a small job i need... show a random content part from a given article - think of it as a 'random quotes' script on steroids - the steroids are that it can show any content part not just plain text.
OK- a small adaptation to Jens's script means a big thanks and all credit go to him.
A working version can be seen http://www.teensunitefightingcancer.org as the 'QUOTES' box in the {RIGHT} column - this is a live site so no funny business please!
As it's a replacement tag, you know the procedure by now... save this code as a file name in template/inc_script/frontend_render/randomCP.php
then use it by adding the article ID in the tag as per the script header blurb... {RANDOMCP:xx}
Code: Select all
<?php
//----------------------------------------------------------------------------------------------
// {SHOW_CONTENT}
// AUTHOR: Jens Zetterström
// ADAPTED BY: pSouper
// DESCRIPTION: Shows a random content part from within a given article (you supply the article ID - it show a random content part from within that article).
// INSTALLATION: Put the code in frontend_render (for example in a file called random_cp.php)
// Note: If the article content part contains replacement tags (for example {SITE}),
// they will not be replaced because code in frontend_render is executed after those
// replacement tags already have been processed. To fix, put the code in
// content.article.inc.php instead.
// USAGE: {RANDOMCP:article_id}
// where article_id is the id of the ARTICLE.
//----------------------------------------------------------------------------------------------
function random_cp($aid, $db)
{
$CNT_TMP = '';
$template_default = $GLOBALS["template_default"];
$sql = "SELECT * " .
"FROM " . DB_PREPEND . "phpwcms_articlecontent " .
"INNER JOIN " . DB_PREPEND . "phpwcms_article ON " . DB_PREPEND . "phpwcms_article.article_id = " . DB_PREPEND . "phpwcms_articlecontent.acontent_aid " .
"WHERE acontent_aid = " . $aid . " " .
"AND acontent_visible = 1 " .
"AND acontent_trash = 0 " .
"AND " . DB_PREPEND . "phpwcms_article.article_deleted=0 AND ".DB_PREPEND."phpwcms_article.article_begin < NOW() " .
// the line below is deleted as it introduces a bug that stops it showing random CP's - not sure why, help?
//"AND " . DB_PREPEND . "phpwcms_article.article_end > NOW() ";
"ORDER BY RAND()";
if($cresult = mysql_query($sql, $db) or die("error retrieving article from database"))
{
if($crow = mysql_fetch_array($cresult))
{
//echo "<pre>";print_r($crow);echo"</pre>";
// Space before
if($crow["acontent_before"])
{
$CNT_TMP .= '<div style="margin:' . $crow["acontent_before"] . 'px 0 0 0; padding:0 0 0 0; clear:both;"></div>';
}
// include content part code section
include("include/inc_front/content/cnt" . $crow["acontent_type"] . ".article.inc.php");
//check if top link should be shown
if($crow["acontent_top"])
{
if($template_default["article"]["top_sign_before"].$template_default["article"]["top_sign_after"])
{
$CNT_TMP .= $template_default["article"]["top_sign_before"];
$CNT_TMP .= '<a href="#top">'.$template_default["article"]["top_sign"].'</a>';
$CNT_TMP .= $template_default["article"]["top_sign_after"];
}
else
{
$CNT_TMP .= '<br /><a href="#top">' . $template_default["article"]["top_sign"] . '</a>';
}
}
// Space after
if($crow["acontent_after"])
{
$CNT_TMP .= '<div style="margin:0 0 ' . $crow["acontent_before"] . 'px 0; padding:0 0 0 0; clear:both;"></div>';
}
}
}
return $CNT_TMP;
}
if( ! ( strpos($content["all"],'{RANDOMCP:')===false ) )
{
$content["all"] = preg_replace('/\{RANDOMCP:(.*?)\}/ie', 'random_cp("$1", $db);', $content["all"]);
$content["all"] = (psTagParser ($content["all"]));
}
?>
__________
A 'minor bug-ette' is that I had to comment out the line that checks if the article end date has past but I can't figure out why it does not work - basically, with the line in it only show the same CP over & over again. any help with this would be very nice too