Displaying a User's Posts by Category - php

Afternoon All,
Looking for some direction with listing posts by category.
I have begun coding a CMS (as a PHP beginner) and up until now all was going well.
I have made a link to pre-set categories (Category 1, Category 2 etc etc)
I also have a categories table with ID and Category name.
A table for users posts with CatID and category name in it as well
I'm guessing I would need to join tables to be able to list any posts in specific category (Select Cat 1 to see all Cat 1 posts. Same for Cat 2, 3 etc etc)
When a user adds a post it fills the category name in users posts table but I get no CAT ID and nothing added into categories table so how do i call on this to display categorised posts?
I have a feeling I am probably thinking to much into things and over-complicating what should probably be simple to do.
Ihe code i have at the moment (see below) has no effect at all?
Please help point me in the right direction, I have tried everything.
Many thanks to all in advance
CODE:
$catSql ="SELECT ID, Category
FROM categories
LEFT JOIN users_posts
ON CatID, category, BlogID";
$catQry = mysqli_query($link, $catSql);
while ($row = mysqli_fetch_assoc($catQry)){
if($row['category_name'] != $lastCategory)
{
$lastCategory = $row['category'];
echo "<br /><strong>$lastCategory</strong>";
}
echo $row['category'] . ' <br />';
}

Your SQL is wrong I think this should work
$catSql = "SELECT *
FROM categories
LEFT JOIN users_posts
ON categories.ID = users_posts.CatID";

Your SQL for listing posts could check to see if a categoryID exists in the URL, and if it does, use it to filter the results.. Then, you would just need to create some links to the same page to add the required category id to the URL.
<a href='?cat=1'>Cat 1</a> | <a href='?cat=2'>Cat 2</a> | etc..
SQL
$sql = "SELECT * FROM POSTS";
if(isset($_GET['cat'])){
$catID = (int)$_GET['cat']; //or something similar
$sql .= " LEFT JOIN category USING categoryID WHERE categoryID = $catID";
}

Related

creating breadcrumb on article page with php using two queries

I want to have breadcrumb on my website looks like this: Home / Category / Sub Category / Article example: https://jsfiddle.net/10kLs0rc/
table structures
categories: id -- parentid -- title
articles: id -- categoryid -- title -- content
*
It's easy to print article's title to breadcrumb. But I also want to print category of article. (even subcategory if article has it. as you can see the example above.)
I think i need php code with double query, one for "categories" and other for "articles" table.
*
EDIT
Page i want to build called "article.php" user will visit: "article.php?id=1"
I can fetch all data I want, except articles parent categories.
Here is the code I simply have, and I want to create another query for fetching parent categories of article but I'm stuck.
<?php
$id = $_GET['id'];
$query = $handler->query("SELECT * FROM article WHERE id='$id'");
while($r = $query->fetch()) {
echo "
<h3>$r[title]</h3>
<p>$r[content]</p>
";
}
?>
This is the query you want :
select
`a`.`title` as `art_title`,
`a`.`content` as `art_content`,
`c1`.`title` as `cat_title`,
`c2`.`title` as `cat_title_parent`
from `article` as `a`
left join `categories` as `c1`
on `c1`.`id` = `a`.`categoryid`
left join `categories` as `c2`
on `c2`.`id` = `c1`.`parentid`
where `a`.`id`=2;
And Here is the POC on [SQLFiddle
So in your php :
while($r = $query->fetch()) {
echo '<i>';
if(isset($r['cat_title_parent']))
echo '/'.$r['cat_title_parent'];
if(isset($r['cat_title']))
echo '/'.$r['cat_title'];
echo '</i><br />';
echo '<h3>'.$r['art_title'].'</h3>';
echo '<p>'.$r['art_content'].'</p>;
}

Wordpress - Search and display categories in results (NOT POSTS!)

I have a lot of categories configured on WordPress and I want a way to let the user search (in a free-text input form) for a particular category.
Then, in the results page I only want to list those categories (NOT POSTS!) and show a link to the category page.
It's there a way (or plugin way) to achieve this easily?
UPDATE
And also how about searching for nested categories?, I mean, I have category A with a lot of child categories. It is possible to search for only those child categories (knowing the ID of A)?
UPDATED: SOLUTION
As #AhmadAssaf suggested I ended up making my own query. Here I post the code that I have used in my web:
UPDATED
The JOIN must be done with taxonomies.term_id and not taxonomies.term_taxonomy_id
function getCategories($string) {
global $wpdb;
$categories = $wpdb->get_results("
SELECT terms.term_id, terms.name, taxonomies.description
FROM wp_terms as terms
LEFT JOIN wp_term_taxonomy as taxonomies ON taxonomies.term_id = terms.term_id
WHERE taxonomies.taxonomy = 'category' && terms.name LIKE '%".$string."%'
GROUP BY taxonomies.term_id
");
return $categories;
}
you can issue a custom mysql query to get all the categories details for a category that its name matches a string you pass
function getCat($string) {
global $wpdb;
$cat= $wpdb->get_results("
SELECT *
FROM wp__term_relationships
LEFT JOIN wp__term_taxonomy
ON (wp__term_relationships.term_taxonomy_id = wp__term_taxonomy.term_taxonomy_id)
LEFT JOIN wp__terms on wp__term_taxonomy.term_taxonomy_id = wp__terms.term_id
WHERE wp__term_taxonomy.taxonomy = 'category' && wp__terms.name LIKE '%".$string."%'
GROUP BY wp__term_taxonomy.term_id
");
return $cat;
}

Fetching Product Detail from Child Category via Parent Category in PHP

I have category table which stores all category info [parent and child both] , category_child table which stores parent and child category relation, and product_category table which stores relation between child_category and product.
category - All Category {cid, cname, isParent, status} column.
category_child - Realtion {parent_id, child_id}.
category_product - Relation {product_id, child_category_id}
product - All product details {pid, pname, pimage, pprice,pstock}
I am displaying all Parent Category Link in Front page. Now, Whenever any person will click on parent category Link, I want to display 4 product information from each child category of parent category.
here is my code, which is horrible at the moment, and i am looking for some help in minimising it as much as possible.
$fetchChild = $mysqli->query("SELECT child_id from category_child where parent_id='".$mysqli->real_escape_string($pid)."'");
$listchild = array();
if($fetchChild->num_rows > 0) {
$n = 1;
while($storeChild = $fetchChild->fetch_assoc()) {
$listchild['child_id'][$n] = $mysqli->query("SELECT product_id from category_product where child_category_id='".$mysqli->real_escape_string($storeChild[$n])."'");
if($listchild['child_id'][$n]->num_rows > 0) {
$i = 1;
while($storeMore = $listchild['child_id'][$n]->fetch_assoc()) {
$listchild['product_id'][$i] = $mysqli->query("SELECT pid, pname, pimage, pprice, pstock from product where pid='".$mysqli->real_escape_string($storeMore[$i])."'");
if($listchild['child_id'][$n]['product_id'][$i]->num_rows > 0) {
$me = 1;
while($smeLast = $storeMore[$i]->fetch_assoc()) {
$listchild['child_id'][$n]['product_id'][$i]['pid'] = $smeLast['pid'];
$listchild['child_id'][$n]['product_id'][$i]['pid'] = $smeLast['pname'];
$listchild['child_id'][$n]['product_id'][$i]['pid'] = $smeLast['pimage'];
$listchild['child_id'][$n]['product_id'][$i]['pid'] = $smeLast['pprice'];
$listchild['child_id'][$n]['product_id'][$i]['pid'] = $smeLast['pstock'];
$me++;
}
} else {
echo '<meta http-equiv="refresh" content="0; url=index.php?error=Something+Went+Wrong+We+are+Fixing+it" />';
}
$listchild['product_id'][$i]->free();
$i++;
}
}
$listchild['child_id'][$n]->free();
$n++;
}
} else {
echo '<meta http-equiv="refresh" content="0; url=index.php" />';
}
$fetchChild->free();
Kindly help in minimizing nested while and query in my code.
Thanks
If you want, you can put everything into one SQL query using JOIN statement.
SELECT `category`.*, `product`.* FROM `product`
LEFT JOIN `category_product` ON `category_product`.`product_id` = `product`.`pid`
LEFT JOIN `category_child` ON `category_child`.`child_id` = `category_product`.`child_id`
LEFT JOIN `category` ON `category`.`cid` = `category_child`.`child_id`
WHERE `category_child`.`parent_id`='".$mysqli->real_escape_string($pid)."'
But I don't think it's the best solution.
PS. By the way, there is no LIMIT of 4 products per child category in your code, so I haven't put it either.
You can reduce all your queries to one query with JOINS. Using joins will allow you to return results from one table (e.g. product) basing on the conditions provided in another table or tables (e.g. category_product, category_child).
Read more about joins somewhere at Stack Overflow or browse some other resources. For example http://www.codinghorror.com/blog/2007/10/a-visual-explanation-of-sql-joins.html .
You should never use SQL query in a loop! It is very slow, you can overload the sql server etc...
Your database structure is bad. If you want to store hiearchical tree there are better options:
Tree 1 level:
1A2
Tree 2 level:
1A6
2B3 4C5
Tree 3 level:
1A12
2B7 8C9 10D11
3E4 5F6
You have a left and a right value by each node, and you can have the parent id too. You can check whether a node is leaf or branch if you check the difference of the right-left. By leaves it is one. You can check whether a leaf is under a branch if its left is bigger than the left of the branch and its right is smaller than the right of the branch. etc... This structure is described on several sites, for example here: nested set model
After you transformed your model to nested set, you can use a simple sql to ask for your products. Something like this:
SELECT
p.*
FROM
categories c, categories b, products p, product_categories pc
WHERE
b.category_id = ?
AND
c_category_id <> b.category_id
AND
c.category_left BETWEEN b.category_left AND b.category_right
AND
c.category_right - c.category_left = 1
AND
c.category_id = pc.category_id
AND
p.product_id = pc.product_id
This is good for beginning, your code will contain group by, limit, etc... because you want to ask only a single product per category... (or you can simply use order by rand() and limit 4 ...)
You should use prepared statements instead of manual escaping... http://php.net/manual/en/mysqli.prepare.php

Listing Items by Category in PHP

Can someone help me with listing items by category in PHP?
I'm trying to create a simple list of books by category:
JavaScript
JavaScript Patterns
Object-Oriented JavaScript
Ajax
Ajax Definitive Guide
Bulletproof Ajax
jQuery
jQuery Cookbook
Learning jQuery 1.3
I have no problems with the data structure or SQL query:
BOOK: book_id, book_title, fk_category_id
CATEGORY: category_id, category_name
SELECT category.category_name, book.book_title
FROM category LEFT OUTER JOIN book
ON category.category_id = book.fk_category_id;
My problem is that I don't know how to write the PHP script to list the books under each category header.
I know enough to get the result set into an array, but then I'm stumped on using that array to group the items as shown.
Another Stack question addresses almost the same thing, but the responses stop short of solving the PHP code: List items by category
Thanks!
Add an ORDER BY category.category_name clause to your query so that as you loop through the resulting rows, the items in each category will be grouped together. Then, each time the category is different from the previous one seen, print out the category as the heading before printing the title.
$category = null;
foreach ($rows as $row) {
if ($row['category_name'] != $category) {
$category = $row['category_name'];
print "<h1>".$category."</h1>\n";
}
print $row['book_title']."<br/>\n";
}
Order the results by category and then just iterate thru, putting a category header whenever the category name changes.
The ordering is most easily done in the SQL query. You don't even need an intermediate array.
SELECT category.category_name, book.book_title
FROM category LEFT OUTER JOIN book
ON category.category_id = book.fk_category_id
ORDER BY category.category_name
And then for the PHP
$res = mysql_query($query);
$lastCategory = '';
while ($row = mysql_fetch_assoc($res))
{
if($row['category_name'] != $lastCategory)
{
$lastCategory = $row['category_name'];
echo "<br /><strong>$lastCategory</strong>";
}
echo $row['book_title'] . ' <br />';
}
You do not need to put all your results into an array first. You can just fetch them as you go.

PHP: MYSQL COUNT query with linked tables

I am looking for a cleaner way to do this. My code works, but I know it can be better. I have three tables: one with a list of Category Groups, One with a list of categories that are linked to category groups, and one with a list of news stories that are linked to the categories.
I need to loop through all of the names of the Category Groups, followed by the names of the categories that are in the category groups, with the number of news stories in each category listed as well.
I have three tables: CategoryGroups, Categories, News.
I have a set of queries. The first queries all the rows from the CategoryGroups table:
$result = mysql_query( '
SELECT cat_group_id, cat_group_name FROM CategoryGroups
' );
The second query is inside the looped results of the first query and finds all the categories that have a news item and are linked to a specific category group:
<?php
while( $row = mysql_fetch_assoc( $result ) ){
$id = $row['cat_group_id'];
$name = $row['cat_group_name'];
echo "<h3>$name</h3>";
$sql = mysql_query("
SELECT category_id, title FROM `Categories`
WHERE cat_group_id = $id
AND category_id IN
(SELECT news.category_id FROM news)
");
while( $row = mysql_fetch_assoc($sql) ) {
$title = $row['title'];
$catid = $row['category_id'];
$numbers = mysql_query("
SELECT * FROM news
WHERE category_id =$catid"
);
$nums = mysql_num_rows($numbers);
echo "$title ($nums)<br/>\n";
}
?>
I would like to limit this to one or two queries, with efficiency in mind. I know this can be done, however I have not been successful in my attempts.
thanks.
Why not JOIN the tables?
SELECT cat_group_name, title, count(newsid)
FROM CatagoryGroups
INNER JOIN Categories ON cat_group_id
INNER JOIN News ON category_id
GROUP BY cat_group_name, title
looks like it should be close, if table news has a newsid column (it's gotta have SOME primary key, right? well, count that;-). With the obvious indexes the JOINs should be quite fast, and your PHP code can do whatever output formatting you may need from that.
I suggest you need to get a book on SQL, such as "SQL Queries for Mere Mortals."
$sql = mysql_query("
SELECT cg.cat_group_name, c.title, COUNT(n.category_id) AS NumNews
FROM `CategoryGroups` cg
JOIN `Categories` c USING (cat_group_id)
JOIN `News` n USING (category_id)
GROUP BY cg.cat_group_name, c.title");
Then loop over the result and output a new <h3> each time the cat_group_name is different from the previous row.

Categories