Home >Backend Development >PHP Tutorial >ECSHOP tutorial call classification article, _PHP tutorial
In the past few days, I helped a friend build a shopping mall, using ECSHOP. I feel that the ECSHOP function is indeed powerful enough, but for those who do not understand the program For humans, it is still quite complicated to use, and it would take a lot of trouble to make a slight modification. It would be great if there was a tag library like Dongyi.
For example, when using Deep Blue, you need to call articles by category. This is a very common function, but the official does not provide it, so I had to think of a way by myself, and finally found a way. Please take a look at the ECSHOP tutorial
The specific implementation method:
For example, the homepage calling method:
1. First open the index.php file and find the following code:
$smarty->assign(<span>'</span><span>new_articles</span><span>'</span>, index_get_new_articles()); <span>//</span><span> 最新文章</span>
Add the following below it:
<span>//</span><span>调用方法</span> <span> $smarty</span>->assign(<span>'</span><span>class_articles_4</span><span>'</span>, index_get_class_articles(<span>4</span>,<span>6</span>)); <span>//</span><span> 分类调用文章 </span><span>//</span><span>调用多个就修改传进去的参数,以及模板接收的变量,其中上面的4就是文章分类ID,其中6是调用数量</span> $smarty->assign(<span>'</span><span>class_articles_5</span><span>'</span>, index_get_class_articles(<span>5</span>,<span>6</span>)); <span>//</span><span> 分类调用文章</span> $smarty->assign(<span>'</span><span>class_articles_6</span><span>'</span>, index_get_class_articles(<span>6</span>,<span>6</span>)); <span>//</span><span> 分类调用文章</span> $smarty->assign(<span>'</span><span>class_articles_7</span><span>'</span>, index_get_class_articles(<span>7</span>,<span>6</span>)); <span>//</span><span> 分类调用文章</span> $smarty->assign(<span>'</span><span>class_articles_8</span><span>'</span>, index_get_class_articles(<span>8</span>,<span>6</span>)); <span>//</span><span> 分类调用文章</span>
<span>//</span><span>在最后?>这个之前增加以下函数</span> <span> function index_get_class_articles($cat_aid, $cat_num) { $sql </span>= <span>"</span><span>SELECT article_id, title,open_type,cat_id,file_url FROM </span><span>"</span> .$GLOBALS[<span>'</span><span>ecs</span><span>'</span>]->table(<span>'</span><span>article</span><span>'</span>). <span>"</span><span> WHERE cat_id = </span><span>"</span>.$cat_aid.<span>"</span><span> and is_open = 1 LIMIT </span><span>"</span><span> . $cat_num; $res </span>= $GLOBALS[<span>'</span><span>db</span><span>'</span>]-><span>getAll($sql); $arr </span>=<span> array(); </span><span>foreach</span> ($res AS $idx =><span> $row) { $arr[$idx][</span><span>'</span><span>id</span><span>'</span>] = $row[<span>'</span><span>article_id</span><span>'</span><span>]; $arr[$idx][</span><span>'</span><span>title</span><span>'</span>] = $row[<span>'</span><span>title</span><span>'</span><span>]; $arr[$idx][</span><span>'</span><span>short_title</span><span>'</span>] = $GLOBALS[<span>'</span><span>_CFG</span><span>'</span>][<span>'</span><span>article_title_length</span><span>'</span>] > <span>0</span> ?<span> sub_str($row[</span><span>'</span><span>title</span><span>'</span>], $GLOBALS[<span>'</span><span>_CFG</span><span>'</span>][<span>'</span><span>article_title_length</span><span>'</span>]) : $row[<span>'</span><span>title</span><span>'</span><span>]; $arr[$idx][</span><span>'</span><span>cat_name</span><span>'</span>] = $row[<span>'</span><span>cat_name</span><span>'</span><span>]; $arr[$idx][</span><span>'</span><span>add_time</span><span>'</span>] = local_date($GLOBALS[<span>'</span><span>_CFG</span><span>'</span>][<span>'</span><span>date_format</span><span>'</span>], $row[<span>'</span><span>add_time</span><span>'</span><span>]); $arr[$idx][</span><span>'</span><span>url</span><span>'</span>] = $row[<span>'</span><span>open_type</span><span>'</span>] != <span>1</span> ?<span> build_uri(</span><span>'</span><span>article</span><span>'</span>, array(<span>'</span><span>aid</span><span>'</span> => $row[<span>'</span><span>article_id</span><span>'</span>]), $row[<span>'</span><span>title</span><span>'</span>]) : trim($row[<span>'</span><span>file_url</span><span>'</span><span>]); $arr[$idx][</span><span>'</span><span>cat_url</span><span>'</span>] = build_uri(<span>'</span><span>article_cat</span><span>'</span>, array(<span>'</span><span>acid</span><span>'</span> => $row[<span>'</span><span>cat_id</span><span>'</span><span>])); } </span><span>return</span><span> $arr; }</span>
2. The second step is to add the following code where you want to call the index.dwt template (note: adjust the list of articles with a category ID of 8 in the above settings):
<!--{<span>foreach</span> <span>from</span>=$class_articles_8 item=article}--> <li><a href=<span>"</span><span>{$article.url}</span><span>"</span> title=<span>"</span><span>{$article.title|escape:html}</span><span>"</span>><!--{$article.short_title|truncate:<span>15</span>:<span>true</span>}--></a></li> <!--{/<span>foreach</span>}-->
The above method was called successfully.