アクティブでカテゴリ 1 のすべての投稿を取得しようとしています。46 件の投稿があるはずですが、4 が出力され、投稿はカテゴリ 1 ではなくカテゴリ 12 からのものです。
$cat_id = 1;
$limit = 12;
// Count posts
$count_posts = count_cat_posts($cat_id);
これは count_posts 関数です
// Count category posts
function count_cat_posts($cat_id){
$stmt = $dbh->prepare("SELECT * FROM mjbox_posts WHERE post_active = 1 AND cat_id = ?");
$stmt->bindParam(1,$cat_id);
$stmt->execute();
$rows = $stmt->fetchAll();
$count_posts = count($rows);
return $count_posts;
}
すべての投稿を配列に保存します
// Retrieve all active posts in this category and order by lastest first
$resultarray = retrieve_cat_posts($cat_id, $offset, $limit);
これは私が使用している機能です。
// Retrieve active posts
function retrieve_cat_posts($cat_id, $offset, $limit){
// Get all the posts
$stmt = $dbh->prepare(" SELECT p.post_id, post_year, post_desc, post_title, post_date, img_file_name, p.cat_id
FROM mjbox_posts p
JOIN mjbox_images i
ON i.post_id = p.post_id
AND i.cat_id = p.cat_id
AND i.img_is_thumb = 1
AND post_active = 1
AND p.cat_id = ?
ORDER BY post_date
DESC
LIMIT ?,?");
$stmt->bindParam(1, $cat_id, PDO::PARAM_INT);
$stmt->bindParam(2, $offset, PDO::PARAM_INT);
$stmt->bindParam(3, $limit, PDO::PARAM_INT);
$stmt->execute();
while($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
$resultarray[] = $row;
}
return $resultarray;
}
オフセット変数を追加しませんでしたが、正しいです。
次のように投稿を出力します。
foreach($resultarray AS $value){
$filename = substr($value['img_file_name'],9);
$cat_id = $value['cat_id'];
// Wraps image, title, category
echo '<div class="itemWrap">';
// Item Image
echo '<a href="post.php?post_id='.$value['post_id'].'" class="itemImageLink"><img class="itemImage" src="create_thumb.func.php?path=img/fs/'.$filename.'&save=0&width=160&height=120" alt="'. stripslashes(stripslashes($value['post_title'])) .'"></a>';
// Item Title
echo '<div class="itemTitle"><a href="post.php?post_id='.$value['post_id'].'" class="itemTitleLink">' .stripslashes(stripslashes($value['post_title'])). '</a></div>';
// Item Category
echo '<div class="itemCat"><a href="cat.php?cat_id='.$cat_id.'">'. $cat_name = get_cat_name($cat_id) .'</a></div>';
// close itemWrap
echo '</div>';
}
mysqlクエリウィンドウで実行すると、クエリが機能します。では、なぜカテゴリ 12 から投稿が取得されるの$cat_id = 1
ですか?