My new tag: {ARTICLELIST:max_count}

Post custom hacks and enhancements for phpwcms here only. Maybe some of these things will be included in official release later.
thanhnq
Posts: 19
Joined: Wed 29. Sep 2004, 03:17

My new tag: {ARTICLELIST:max_count}

Post by thanhnq »

Hi,
I write the following code to display articles list with pages. Pls. tell if there some thing wrong or will cause errors with the whole phpwcms system!

Code: Select all

//In file  \include\inc_front\content.func.inc.php add the following 
// usage: ARTICLELIST:max_count (display max_count summaries from CatAlias) 
$content["all"] = preg_replace('/\{ARTICLELIST:(\d+)\}/ie', 'get_summary_list ($content["cat_id"], $template_default, $1, $db);', $content["all"]);  
=====================

Code: Select all

//Add the following into the file \include\inc_front\front.func.inc.php. 
//The level 1 link to each cat must be in this format: http://www.yourweb.com/index.php?[color=blue]cat_alias[/color]&page=0&sum=0 

function get_pager_data($total, $limit, $page) { 
           $total  = (int) $total; 
           $limit    = max((int) $limit, 1); 
           $page     = (int) $page; 
           $numPages = ceil($total / $limit); 

           $page = max($page, 1); 
           $page = min($page, $numPages); 

           $offset = ($page - 1) * $limit; 

           $ret = array ("offset" => $offset, "limit" => $limit, "numPages" => $numPages, "page" => $page);            

           return (sizeof($ret)) ? $ret: array();          
} 

function get_data_from_acat($cat_id, $offset, $limit, $dbcon) { 
   //This code block is similar to the get_actcat_articles_data function, so you can modify the function instead    
   $ao = get_order_sort($GLOBALS['content']['struct'][ $cat_id ]['acat_order']); 
   $sql  = "SELECT *, UNIX_TIMESTAMP(article_tstamp) AS article_date FROM ".DB_PREPEND."phpwcms_article "; 
   $sql .= "WHERE article_cid='$cat_id' AND "; 
   $sql .= "article_public=1 AND article_aktiv=1 AND article_deleted=0 "; 
   $sql .= "AND article_begin<NOW() AND article_end>NOW() ";    
   $sql .= "ORDER BY ".$ao[2]." LIMIT ".$offset.",".$limit.";";    

   if($result = mysql_query($sql, $dbcon)) { 
      while($row = mysql_fetch_array($result, MYSQL_ASSOC)) { 
         $data[$row["article_id"]] = array( 
                           "article_id"      => $row["article_id"], 
                           "article_cid"      => $row["article_cid"], 
                           "article_title"      => $row["article_title"], 
                           "article_subtitle"   => $row["article_subtitle"], 
                           "article_keyword"   => $row["article_keyword"], 
                           "article_summary"   => $row["article_summary"], 
                           "article_redirect"   => $row["article_redirect_to"], 
                           "article_date"      => $row["article_date"], 
                           "article_username"   => $row["article_username"], 
                           "article_sort"      => $row["article_sort"], 
                           "article_notitle"   => $row["article_notitle"], 
                           "article_created"   => $row["article_created"], 
                           "article_image"      => unserialize($row["article_image"]) 
                                   ); 
      } 
      mysql_free_result($result); 
   } 
   return (count($data)) ? $data : array();    
} 

// returns article summary for ARTICLELIST-replacement 
function get_summary_list ($cat_id, $template_default, $max_count, $dbcon) {        
   if ($_GET['id']) {       
      return $GLOBALS['content']["main"]; 
   } 
   $page = $_GET['page']; 
   $total = $_GET['sum']; 
   if (!$total) { 
      $sql = "SELECT count(*) FROM ".DB_PREPEND."phpwcms_article WHERE article_cid='$cat_id';"; 
      $result = mysql_query($sql, $dbcon); 
      $total = mysql_result($result, 0, 0); 
      mysql_free_result($result);       
   } 
   $limit = $max_count; 
   // work out the pager values 
   $pager  = get_pager_data($total, $limit, $page); 
   $offset = $pager["offset"]; 
   $limit  = $pager["limit"]; 
   $page   = $pager["page"]; 

   $teaser_content = get_data_from_acat ($cat_id, $offset, $limit, $dbcon); 
   $teaser_content = list_articles_summary ($teaser_content, $template_default); 
    
   $alias = html_specialchars($GLOBALS['content']['struct'][$cat_id]['acat_alias']); 

   // output paging system (could also do it before we output the page content) 
   if ($page == 1) // this is the first page - there is no previous page 
           $teaser_content .= "<p> Previous"; 
   else            // not the first page, link to the previous page 
           $teaser_content .= '<a href=index.php?' . $alias . '&page=' . ($page - 1) . '&sum=' . $total . '>Previous</a>'; 

   for ($i = 1; $i <= $pager["numPages"]; $i++) { 
           $teaser_content .= " | ";        
           if ($i == $page) 
               $teaser_content .= $i; 
           else 
               $teaser_content .= '<a href=index.php?' . $alias . '&page=' .$i. '&sum=' . $total . '>'.$i.'</a>'; 
   } 
    $teaser_content .= " | ";    
   if ($page == $pager["numPages"]) // this is the last page - there is no next page 
           $teaser_content .= " Next </p>"; 
   else            // not the last page, link to the next page 
           $teaser_content .= '<a href=index.php?' . $alias . '&page=' . ($page + 1) . '&sum=' . $total . '>Next </a>'; 

   return $teaser_content;          
} 
=====================
Last edited by thanhnq on Mon 25. Oct 2004, 09:12, edited 1 time in total.
Pappnase

Post by Pappnase »

hello

sorry but when you not tell what error you get we can't help :wink:
wich version did you use?

is this a hack!?
thanhnq
Posts: 19
Joined: Wed 29. Sep 2004, 03:17

Post by thanhnq »

Oh no, it's good for me and no error returned! I just want to verify that it's good or not because it's really a hack!
:lol:
Pappnase

Post by Pappnase »

hello

if you build such modifications please add this in the future under hacks and enhancements ok!?

i will move it to this section now.
Magnum
Posts: 91
Joined: Thu 12. Aug 2004, 00:56
Location: Berlin
Contact:

Post by Magnum »

hmmm, i paste this code in my cms! i saw no news. what do this code exactly?

sorry for my schoolenglish

greetz
User avatar
isac
Posts: 410
Joined: Tue 18. Nov 2003, 13:13
Location: Portugal
Contact:

Post by isac »

thanhnq,

Why don´t you put the first code in

Code: Select all

phpwcms_template/inc_script/frontend_render/
and the second code in

Code: Select all

phpwcms_template/inc_script/frontend_init/
:?:

It's the same thing and you don't have to modified the original files

don't forget to enable this in confi.inc.php

Code: Select all

$phpwcms["allow_ext_init"]    = 1;  //allow including of custom external scripts at frontend initialization
$phpwcms["allow_ext_render"]  = 1;  //allow including of custom external scripts at frontend rendering
please correct me if i'm wrong
User avatar
isac
Posts: 410
Joined: Tue 18. Nov 2003, 13:13
Location: Portugal
Contact:

Post by isac »

I tried this hack and i like it very much

Thanks thanhnq :D
User avatar
isac
Posts: 410
Joined: Tue 18. Nov 2003, 13:13
Location: Portugal
Contact:

Post by isac »

But there is a problem:

replacement tags like {SPACER:1x10} or {GT:style}{/GT} don't work :(

Do you intent to fix this?
User avatar
nekket
Posts: 613
Joined: Tue 18. Nov 2003, 15:46
Location: Baden-Baden
Contact:

Post by nekket »

Try to insert this @ the end of the file:

Code: Select all

//$content["all"] = preg_replace('/\{GT:(.+?)\}(.*?)\{\/GT\}/ei', "get_gt_by_style(\$gt, \"\$1\", \"\$2\")", $content["all"]);
shakkalakka
Posts: 21
Joined: Thu 16. Dec 2004, 22:52
Location: Winterthur, Switzerland
Contact:

Post by shakkalakka »

First: thanks to thanhnq for this great mod!

Then: I found a small bug in your second code snippet in the function 'get_summary_list'. When you do the query to get the total number of articles in the specific cat, you now count all articles, even the deleted ones. To change this simply add 'AND article_deleted=0' to the WHERE statement. See below:

your code:

Code: Select all

   if (!$total) {
      $sql = "SELECT count(*) FROM ".DB_PREPEND."phpwcms_article WHERE article_cid='$cat_id';";
      $result = mysql_query($sql, $dbcon);
      $total = mysql_result($result, 0, 0);
      mysql_free_result($result);       
   } 
changed code:

Code: Select all

   if (!$total) {
      $sql = "SELECT count(*) FROM ".DB_PREPEND."phpwcms_article WHERE article_cid='$cat_id' AND article_deleted=0;";
      $result = mysql_query($sql, $dbcon);
      $total = mysql_result($result, 0, 0);
      mysql_free_result($result);       
   }
creep
Posts: 10
Joined: Tue 28. Dec 2004, 15:53

Post by creep »

Perfect!
Is work for me too!

But if you want to show pagination only if is realy needed
This code:

Code: Select all

   // output paging system (could also do it before we output the page content) 
   if ($page == 1) // this is the first page - there is no previous page 
           $teaser_content .= "<p> Previous"; 
   else            // not the first page, link to the previous page 
           $teaser_content .= '<a href=index.php?' . $alias . '&page=' . ($page - 1) . '&sum=' . $total . '>Previous</a>'; 

   for ($i = 1; $i <= $pager["numPages"]; $i++) { 
           $teaser_content .= " | ";        
           if ($i == $page) 
               $teaser_content .= $i; 
           else 
               $teaser_content .= '<a href=index.php?' . $alias . '&page=' .$i. '&sum=' . $total . '>'.$i.'</a>'; 
   } 
    $teaser_content .= " | ";    
   if ($page == $pager["numPages"]) // this is the last page - there is no next page 
           $teaser_content .= " Next </p>"; 
   else            // not the last page, link to the next page 
           $teaser_content .= '<a href=index.php?' . $alias . '&page=' . ($page + 1) . '&sum=' . $total . '>Next </a>'; 

   return $teaser_content;          
} 
Replase with:

Code: Select all

  if ($total > $limit) {
   // output paging system (could also do it before we output the page content) 
   if ($page == 1) // this is the first page - there is no previous page 
           $teaser_content .= "<p> Previous"; 
   else            // not the first page, link to the previous page 
           $teaser_content .= '<a href=index.php?' . $alias . '&page=' . ($page - 1) . '&sum=' . $total . '>Previous</a>'; 

   for ($i = 1; $i <= $pager["numPages"]; $i++) { 
           $teaser_content .= " | ";        
           if ($i == $page) 
               $teaser_content .= $i; 
           else 
               $teaser_content .= '<a href=index.php?' . $alias . '&page=' .$i. '&sum=' . $total . '>'.$i.'</a>'; 
   } 
    $teaser_content .= " | ";    
   if ($page == $pager["numPages"]) // this is the last page - there is no next page 
           $teaser_content .= " Next </p>"; 
   else            // not the last page, link to the next page 
           $teaser_content .= '<a href=index.php?' . $alias . '&page=' . ($page + 1) . '&sum=' . $total . '>Next </a>'; 

  }
   return $teaser_content;          
} 
User avatar
isac
Posts: 410
Joined: Tue 18. Nov 2003, 13:13
Location: Portugal
Contact:

Post by isac »

Hello

this replacement tag don't work with new DEV Release: 1.1.9 anymore. Do you intent to make it compatible?

Thanks
brans

Post by brans »

may I see a demo of this tag ?
User avatar
isac
Posts: 410
Joined: Tue 18. Nov 2003, 13:13
Location: Portugal
Contact:

Post by isac »

isac wrote:Hello

this replacement tag don't work with new DEV Release: 1.1.9 anymore. Do you intent to make it compatible?

Thanks
Sorry it works, but some replacment tags like {DATE_ARTICLE) and {SPACER:1x9} simply don´t render...

Anyone?
snezko
Posts: 21
Joined: Fri 11. Mar 2005, 18:33
Location: Wien
Contact:

add summary

Post by snezko »

hi i would like to add a summary into this output where can i found it. i tried it, but without success. thanks
many thanks Snezko
Post Reply