Page 5 of 5

Random Content part from a given Article ID

Posted: Tue 23. Oct 2007, 22:49
by pSouper
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}

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"]));
}


?>
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 :)

SHOW CONTENT manual

Posted: Thu 25. Oct 2007, 12:55
by stemmen
HI all

Has anyone made a manual.

I have no idea have to use {SHOW_CONTENT: id, id, id}.

If I put it inside my template, is it to replace {CONTENT} and {LEFT} etc.?

Is it supposed to be used alongside {CONTENT} and {LEFT} etc.?

What are the actual functions?

Obviously, I am not a php-expert, so any help will be highly appreciated.

Reg.Jan

Posted: Thu 25. Oct 2007, 13:09
by pSouper
SHOW_CONTENT is now redundant for the lastest few releases and can now be used via the content part 'contentpart alias'

if you still insist on using the {RT} too then you simply add the replacement tag {SHOW_CONTENT:cp,contentpartID,contentpartID,contentpartID} to antywhere in your content text - for example, in your article summary or plain text/wywiwyg content parts etc.

each (comma seperated) contentpartID will add a teaser of that content part to the list (best to try it to see it)

the cp part of the tag can also be cpa - this simple shows the contentparts in acceding order.

the contentpartId's are fount in the backend > article menu by the side of each contentpart and look like this... [ID:16]plain text (it's the number only ok ;) )

it does NOT replace {CONTENT} or {RIGHT} etc. it can be used within a template and may be included within {CONTENT} or {RIGHT} etc. To do this you add the {SHOW_CONTENT:cp,xxx} to the bottom blocks of the template that corespond to the area you wish this to appear such as within the {RIGHT} block... this will adda div above the content of the {RIGHT} block that contains the contentpart [ID:116]....

Code: Select all

{<div id="rightBox">{SHOW_CONTENT:CP,116}{RIGHT}</div>

give it a go and see what you get :)

Posted: Thu 25. Oct 2007, 13:28
by flip-flop
And please, have a look into the docu: http://www.phpwcms-docu.de/article_tags.phtml :wink:

Re: {SHOW_CONTENT} Show content of a particular content part

Posted: Wed 26. Nov 2008, 18:53
by sparks
still, a random CP would be needed. I tried the last "hack" of this RT and it does not work.

Re: {SHOW_CONTENT} Show content of a particular content part

Posted: Wed 26. Nov 2008, 19:16
by flip-flop
{SHOW_CONTENT:MODE,id,id,id} is pegged into durably. -> http://www.phpwcms-docu.de/article_tags.phtml

Re: {SHOW_CONTENT} Show content of a particular content part

Posted: Wed 26. Nov 2008, 20:59
by pSouper
was it my random_cp hack that didn't work?
if so - please let me know the phpwcms version you are using along with any errors or signs of failure you are experiencing and I will look into an update if needed.

Re: {SHOW_CONTENT} Show content of a particular content part

Posted: Thu 27. Nov 2008, 09:22
by update
This is working smoothly with r273 (sorry to say that, but this is still the newest)

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.
//----------------------------------------------------------------------------------------------
// ------------------------------------------------------------------
// obligate check for phpwcms constants
if (!defined('PHPWCMS_ROOT')) {
   die("You Cannot Access This Script Directly, Have a Nice Day."); }
// ------------------------------------------------------------------

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"]));
}


?>

I had to comment

Code: Select all

//$content["all"] = (psTagParser ($content["all"]));
due to some errors and have added

Code: Select all

// ------------------------------------------------------------------
// obligate check for phpwcms constants
if (!defined('PHPWCMS_ROOT')) {
   die("You Cannot Access This Script Directly, Have a Nice Day."); }
// ------------------------------------------------------------------
to meet the standard.
Really cool thing! Thanks!

Re: {SHOW_CONTENT} Show content of a particular content part

Posted: Thu 27. Nov 2008, 10:06
by pSouper
hehe - sorry about the commented line - that's residue from additional stuff i do elsewhere in my scripts - thanks for pointing it out.

also - you're right, it is always good to add those lines to your added scripts - preventing direct access and potential back doors.