php - 将每个类别获取的结果数量限制为仅 4 个,而不是显示 "See More Results"

标签 php mysql

再次遇到问题,需要一些帮助来解决它。

我需要限制每个有任何结果的类别中检索结果的数量,以仅显示 4 个及以下类别将显示指向该类别的链接,例如“查看更多结果”或类似内容。

我最初的想法是,我需要更改或添加一些内容到实际查询中,所以我添加了 LIMIT 4,但结果是我最终只显示一个类别,其中包含 4 个项目类别,就是这样。

请帮忙

这是我的脚本

if($category_type == 'links_only'){
    $category_topic_query = 'SELECT l.category_name, l.category_folder, l.category_page, f.parentpageID, f.ImagePath, f.referring_url, f.templateTitle, f.xls, f.xlsx, f.ots, f.gdocs, f.numbers, f.templateImage, f.templimgwidth, f.templimgheight FROM lcategories l INNER JOIN Files f ON f.parentpageID = l.ID WHERE f.pageID = "0" ORDER BY f.parentpageID';
    $resc = $db->prepare($category_topic_query);
    $resc->execute();
$last_topic = NULL;
$add_rowNum = 1;
while ($category_topic = $resc -> fetch()){
    $category_topic_ID = $category_topic['parentpageID'];
    if($category_topic_ID !== $last_topic) {
        $add_rowNum++;
    $category_topic_name = str_replace("&", "&", $category_topic['category_name']);
    $category_topic_url = DST.$category_topic['category_folder'].DS.$category_topic['category_page'];
    $divider_div = ($add_rowNum % 2 == 0) ? '
<div class="clear"></div>' : FALSE ;
    $first_ul = ($category_topic_ID == 1) ? FALSE : '</ul>
</div>'.$divider_div ;
print<<<END
$first_ul
<div style="float: left; margin: 10px; width: 349px;">
<h2 class="h_unln"><a href="$category_topic_url">$category_topic_name</a></h2>
<ul class="arrow">

END;

$last_topic = $category_topic_ID;
}
    $links_array = ($category_topic['referring_url'] == NULL) ? FALSE :'
    <li>
    <ul>
    <li><a href="'.DST.$category_topic['ImagePath'].DS.$category_topic['referring_url'].'"><img src="'.$image_path.$category_topic['ImagePath'].DS.$category_topic['templateImage'].'" width="'.$category_topic['templimgwidth'].'" height="'.$category_topic['templimgheight'].'" title="'.$category_topic['templateTitle'].'"></a></li>
    <li><a href="'.DST.$category_topic['ImagePath'].DS.$category_topic['referring_url'].'">'.$category_topic['templateTitle'].'</a></li>
    <li>'.$numbers_im.$gdocs_im.$ots_im.$xlsx_im.$xls_im.'</li></ul></li>';


print<<<END
$links_array

END;
}
print<<<END
</ul>
</div>
<div class="clear"></div>

END;
}

最佳答案

    $limit = ($_GET['limit'])? $_GET['limit'] : 4 ; //For the first query only 4 links will be displayed
    $offset = ($_GET['offset'])? $_GET['offset'] : 0;

像这样调整您的查询:

$category_topic_query = "SELECT l.category_name, l.category_folder, l.category_page, f.parentpageID, f.ImagePath, f.referring_url, f.templateTitle, f.xls, f.xlsx, f.ots, f.gdocs, f.numbers, f.templateImage, f.templimgwidth, f.templimgheight FROM lcategories l INNER JOIN Files f ON f.parentpageID = l.ID WHERE f.pageID = '0' ORDER BY f.parentpageID 
LIMIT  $limit OFFSET $offset";

然后在“查看更多链接”中

<a href='<? echo $_SERVER['PHP_SELF'] ?>?limit=20&offset=4'> See more </a>

关于php - 将每个类别获取的结果数量限制为仅 4 个,而不是显示 "See More Results",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18826123/

相关文章:

Php脚本未按预期导入新记录

php - 如何在没有 window.open 的情况下从 Javascript 启动 PHP 文件

PHP通过类静态属性缓存对象

php - mysql server has gone away,序列化和锁等待错误

mysql - Debian 安装 MySQL 特定版本不可用

Python 的 MySQLdb 无法使用 Homebrewed MySQL 找到 libmysqlclient.dylib

php - 从日期范围查找可用房间

php - 以编程方式确定哪个 FOREIGN KEY 约束失败

javascript - 如何在 codeigniter 中为同一个表创建动态/链接下拉列表

php - 迭代期间取消设置数组值是否节省内存?