我有一个问题,一直困扰着我一段时间。我在我的页面上有10个活动,在它的底部我有一个"加载更多" -button,使用AJAX / jQuery从MySQL加载另外10行。问题是结果的排序不正确。前10行是正确排序的,但是当我点击"加载更多"按钮它会加载另外10行,其类别和日期与前10行不对应。
例如:
**Title** **Category** **Location** **Date**
Title 1 Boardgames Copenhagen 11/9/2015
Title 2 Boardgames Copenhagen 12/9/2015
Title 3 Social Activity Copenhagen 14/9/2015
Title 4 Sports London 16/9/2015
Title 5 Sports London 23/9/2015
前5行按位置,类别和日期正确排序
ORDER BY k.name DESC,aa.category_id,aa.date
但是,例如,如果我加载接下来的5行,则这些行不会被排序,因此它们与第一行匹配。如果我尝试按"加载更多"它可能看起来像这样。按钮:
**Title** **Category** **Location** **Date**
Title 1 Boardgames Copenhagen 11/9/2015
Title 2 Boardgames Copenhagen 12/9/2015
Title 3 Social Activity Copenhagen 14/9/2015
Title 4 Sports London 16/9/2015
Title 5 Sports London 23/9/2015
Title 6 Boardgames Copenhagen 13/9/2015
Title 7 Boardgames Copenhagen 14/9/2015
Title 8 Social Activity Copenhagen 14/9/2015
Title 9 Sports London 16/9/2015
Title 10 Sports London 22/9/2015
它可能与我的查询中的LIMIT 10有关?我感谢所有的帮助!
以下是我前10行的查询:
SELECT ar.id,ar.title,ar.time,ar.href,k.name AS kommune,aa.date,c.name AS cat
FROM activity_results ar
INNER JOIN activity_analyzer aa
ON aa.activity_id = ar.id
INNER JOIN user_activity_preferences uap
ON uap.category_id = aa.category_id
INNER JOIN user_kommune_preferences ukp
ON ukp.kommune_id = aa.kommune_id
INNER JOIN kommuner k
ON k.kommune_id = aa.kommune_id
INNER JOIN categories c
ON c.id = aa.category_id
INNER JOIN users u
ON u.id = ukp.user_id
WHERE (uap.user_id AND ukp.user_id = '$_SESSION[user_id]')
AND ar.status = '2'
AND aa.overtegnet = '0'
AND aa.date != '0000-00-00'
AND aa.date >= now()
AND (aa.gender = '0'OR aa.gender = u.gender)
ORDER BY k.name DESC,aa.category_id,aa.date
LIMIT 10
接下来的10行:
SELECT ar.id,ar.title,ar.time,ar.href,k.name AS kommune,aa.date,c.name AS cat
FROM activity_results ar
INNER JOIN activity_analyzer aa
ON aa.activity_id = ar.id
INNER JOIN user_kommune_preferences ukp
ON ukp.kommune_id = aa.kommune_id
INNER JOIN kommuner k
ON k.kommune_id = aa.kommune_id
INNER JOIN categories c
ON c.id = aa.category_id
INNER JOIN users u
ON u.id = ukp.user_id
WHERE ukp.user_id = '$_SESSION[user_id]'
AND ar.status = '2'
AND aa.overtegnet = '0'
AND aa.date != '0000-00-00'
AND aa.date >= now()
AND (aa.gender = '0'OR aa.gender = u.gender)
AND ar.id > $last_activity_id
ORDER BY k.name DESC,aa.category_id,aa.date
LIMIT 10
答案 0 :(得分:0)
以下是适用于您的分页示例。您必须每次都要发送要加载的页面,但它将适用于您正在进行的操作。
if(!isset($page)){
$page = 1;
}
$limit = 10;
$start = ($page - 1) * $limit + 1
$query = sprintf(
'SELECT ar.id,ar.title,ar.time,ar.href,k.name AS kommune,aa.date,c.name AS cat
FROM activity_results ar
INNER JOIN activity_analyzer aa
ON aa.activity_id = ar.id
INNER JOIN user_activity_preferences uap
ON uap.category_id = aa.category_id
INNER JOIN user_kommune_preferences ukp
ON ukp.kommune_id = aa.kommune_id
INNER JOIN kommuner k
ON k.kommune_id = aa.kommune_id
INNER JOIN categories c
ON c.id = aa.category_id
INNER JOIN users u
ON u.id = ukp.user_id
WHERE (uap.user_id AND ukp.user_id = '$_SESSION[user_id]')
AND ar.status = '2'
AND aa.overtegnet = '0'
AND aa.date != '0000-00-00'
AND aa.date >= now()
AND (aa.gender = '0'OR aa.gender = u.gender)
ORDER BY k.name DESC,aa.category_id,aa.date
LIMIT %s,%s', $start, $limit
);