Reputation: 43
hi im trying to get this particular code to work for a category page. not sure how to arrange it.
$cat = $_GET['cat'];
}
$record_count = $db->query("SELECT * FROM posts WHERE category_id='$cat'");
$per_page = 3;
$pages = ceil($record_count->num_rows/$per_page);
echo $cat;
if (isset($_GET['p']) && is_numeric($_GET['p'])){
$page = $_GET['p'];
}else{
$page = 1;
}
if($page<=0)
$start=0;
else
$start = $page * $per_page - $per_page;
$prev = $page - 1;
$next = $page + 1;
$query = $db->prepare("SELECT post_id, title, date, image, LEFT(body, 150) AS body, category FROM posts WHERE posts.category_id=$cat INNER JOIN categories ON categories.category_id=posts.category_id order by post_id desc limit $start, $per_page");
$query->execute();
$query->bind_result($post_id, $title, $date, $image, $body, $category );
This includes pagnation, and is in particular looking at this line. but to isolate the category_id needed is seeming harder (for me at least) than expected
$query = $db->prepare("SELECT post_id, title, date, image, LEFT(body, 150) AS body, category FROM posts WHERE posts.category_id=$cat INNER JOIN categories ON categories.category_id=posts.category_id order by post_id desc limit $start, $per_page");
using two mysql tables
posts: post_id title date image body link linkname category_id
categories: category category_id
Upvotes: 4
Views: 251
Reputation: 1745
You have pieces of your query out of order (specifically, where WHERE goes) and you are not doing prepared statements correctly. Note that you put a question mark where each variable goes and then use bind_param to fill in the question marks in order. The first part, "iii"
, means that there are three integers.
$query = $db->prepare("SELECT post_id, title, date, image, LEFT(body, 150) AS body, category
FROM posts
INNER JOIN categories ON categories.category_id=posts.category_id
WHERE posts.category_id=?
order by post_id desc
limit ?, ?");
$query->bind_param("iii", $cat, $start, $per_page);
$query->execute();
Upvotes: 2