dede totalresult是什么?如何正确使用?

99ANYc3cd6
预计阅读时长 19 分钟
位置: 首页 DEDE建站 正文

Of course. Let's break down dede.totalresult, a very important and frequently used variable in the DedeCMS (织梦CMS) templating system.

dede totalresult
(图片来源网络,侵删)

What is dede.totalresult?

In simple terms, dede.totalresult is a DedeCMS global variable that holds the total number of records (results) found by the current query.

It is most commonly used within list templates (those in the /templets/default/ directory, like list_article.htm) to display the total number of articles or items in a category, search result, or other list-based page.


How It Works: The Context

dede.totalresult is not just a random number. It is generated by the PHP backend when a page is requested. Here's the typical flow:

  1. A user visits a category page (e.g., http://your-site.com/plus/list.php?tid=5).
  2. DedeCMS's PHP code queries the database to get all articles belonging to category ID 5.
  3. The query also calculates the COUNT(*) of these articles.
  4. This total count is stored in the global variable $totalresult in the PHP code.
  5. DedeCMS then parses the list template (list_article.htm). During parsing, it exposes this PHP variable to the template engine through the global $totalresult; declaration.
  6. In the template, you access it using the syntax {dede:global name='totalresult'/}.

How to Use It in Your Templates

The standard way to use dede.totalresult is within the {dede:global} tag.

dede totalresult
(图片来源网络,侵删)

Basic Usage

This will simply output the total number as a plain integer.

<p>共有文章:{dede:global name='totalresult'/} 篇</p>

Example Output: 共有文章:128 篇


Practical Example: A Complete Pagination Block

dede.totalresult is most powerful when used with pagination. It gives the user context about the size of the result set they are browsing.

Here is a common and well-structured pagination block you would find in a list_article.htm template.

dede totalresult
(图片来源网络,侵删)
<div class="dede_pages">
    <ul class="pagelist">
        <!-- "Total Items" Info -->
        <li><span class="pageinfo">共 <strong>{dede:global name='totalresult'/}</strong> 条记录</span></li>
        <!-- "Previous Page" Link -->
        {dede:prepage/}
        <!-- Page Number Links -->
        {dede:pagelist listsize='5' listitem='info,index,end,pre,next,pageno'/}
        <!-- "Next Page" Link -->
        {dede:nextpage/}
    </ul>
</div>

Explanation of the example:

  • <li><span>...: This line uses dede.totalresult to display the total number of records, which is great for user experience.
  • {dede:prepage/}: Generates a link to the previous page of results.
  • {dede:pagelist listsize='5' listitem='...'}: This is the core pagination tag.
    • listitem='...': Specifies which parts of the pagination to display. info is the part that shows "Total X records, Y pages", which often uses totalresult internally.
    • listsize='5': Shows 5 page numbers before and after the current page.
  • {dede:nextpage/}: Generates a link to the next page of results.

Key Points and Troubleshooting

  1. Scope: dede.totalresult is only available on list-type pages. You will not find a value for it on single-article pages (like article_article.htm) or the homepage. Trying to use it there will result in empty output or an error.

  2. When it might be empty: If the query returns no results (e.g., an empty category or a search with no matches), dede.totalresult will be 0.

  3. Custom SQL Queries: If you are using a custom query in your PHP code (e.g., in a custom PHP page or a plugin), you must manually set the $totalresult variable for it to be available in the template.

    // In your custom PHP file
    $query = "SELECT * FROM `#@__archives` WHERE typeid = 10";
    $dsql->SetQuery($query);
    $dsql->Execute();
    $totalresult = $dsql->GetTotalRow(); // This is the key line
    global $totalresult;
    // Then include and parse your template
    include(DEDETEMPLATE.'/custom_list.htm');
  4. Caching: Be aware that DedeCMS uses extensive caching. If you modify a template file, you may need to clear the cache (in the DedeCMS backend -> System -> Cache Settings) or rename the cache directory temporarily to see your changes take effect.

Summary

Feature Description
What it is A DedeCMS template variable that holds the total count of records for the current list page.
Syntax {dede:global name='totalresult'/}
Primary Use Case Displaying the total number of articles in a category, search results, etc., especially in pagination.
Location Primarily used in list templates (e.g., list_article.htm).
Availability Not available on single-article or static pages. Its value depends on the backend database query count.

By understanding dede.totalresult, you can create more informative and user-friendly list pages for your DedeCMS website.

-- 展开阅读全文 --
头像
织梦CMS数据库连接失败怎么办?
« 上一篇 2025-12-28
c语言and or not优先级
下一篇 » 2025-12-28

相关文章

取消
微信二维码
支付宝二维码

目录[+]