Now I have been trying to tackle this issue for a few hours now, and I been trying google and the documentation and read it over and over, but I can't seem to understand it.
Let me try explain what im doing, but simplify the project, assume I am making a blog with cakePHP. I got this 3 tables to make it simple we use only the needed fields.
Table: posts
Desc: This table store all our posts
Fields:
postid - Identifier for the post
title - The posts title
Table: sections
Desc: this table contains categories
Fields:
sectionid - Identifier
sectionname - Name
Table: postlinks
Desc: this table store relations between posts and categories.
Fields:
postid – Post ID to determine what post is linked to what section
sectionid – Section ID to determine the section this post belong to
Let me now try explain what I want to achieve with cakePHP by showing an example with regular php and SQL codes:
(Note this code was written up on the go and might not work but it should give you an idea on what I'm trying to do)
<?php
/**
*Block of code to fetch all posts
*/
while ( $rows = mysql_fetch_array($query) )
{
$postid = $rows['postid'];
/** Join postlinks and sections **/
$sql = “SELECT sections.*, postlinks.* FROM postlinks
INNER JOIN sections
ON postlinks.sectionid = sections.sectionid
WHERE postlinks.postid = '$postid'”;
$query = mysql_query($sql);
print “-- TITLE --”;
print “-- CONTENT --”;
print “Posted in “;
/** Loop all categories **/
while ( $rows = mysql_fetch_array($query) )
{
print $rows['sectionname'] . “ “;
}
}
?>
And now I will try explain with words what I am trying to achieve:
When a visitor is browsing the web page, and enter the blog part of the site, I wish to list X latest posts, and this posts can be put in more than one category, and we will store all category links in a separate table, and here is where I am stuffed, I can manage to get the links out but I can't seem to join the tables and loop out the name.
Hope someone can shed some light on this problem I got with either point me in the right direction and/or show me an example code.
As mentioned I have been reading the documentation, and I have attempted searching for the answer but
association is implement as a join in cakephp.
please follow the concept for HABTM i.e "Has And Belongs To Many"
make a declaration in you model association.
declare the association in your cakephp query.
recursive -> -1 or 0 or 1 or 2
to Know more about association please visit this link enter link description here
Related
Hello.
So I'm doing the article submission atm and I need articles to have multiple categories... Article categories would be stored in articles table under categories field like this {"cat1","cat2","cat3"}. But a mind popped to my head... "What if I want to lookup articles by a category?". If I would do something like SELECT * FROM articles WHERE categories = ? then I can't define what categories which post has... So I can't lookup by a category this way. Any suggestions, solutions? Much appreaciated!
Edit: If I would retrieve all post from database and do some for each solution it would cause server loads or even crashes for several minutes. Right now I have 7,307 articles.
To resolve your actual problem, you can use this sql query
SELECT * FROM articles WHERE categories LIKE '%cat1%'
But, about database scheme, Best way should like to have articles categories and a pivot table article_categories table. A many to many relation would help
articles
id,
title
...
categories
id,
name, ...
article_categories
id,
article_id,
category_id
I have two tables:
media_folders
id, title
media
id, folder_id, title
They each have their own model
Media, MediaFolder. Nothing special about them except they have the static $table_name property set. Now I can assign a $has_many to the MediaFolder class which will find all associated Media. However, All i need is to get the number of media within a mediafolder and not the actual objects themselves. I want to end up with an attribute in a MediaFolder object called files which has a count of how many media rows has a folder_id of the current folder.
How would i do this?
Understand your case, but not your exact problem so can you provide some more details like:
1. does your problem is with building SQL query?
2. does your problem with building query using active records.. (if yes which framework you are using)
Need some more description.
If it's just some SQL :
$sql = "SELECT mf.folder_id, count(m.id) as files
FROM media_folders mf, media m
WHERE mf.id = m.folder_id
GROUP BY mf.folder_id"
I want to store reviews in a flexible system of categories and subcategories, and am currently in the process of designing the database structure for that. I have an idea how to do that, but I'm not entirely sure if it couldn't be done more elegant and/or efficient. These are my thoughts - if anybody can comment on if/how this can be improved I'd be really grateful.
(To keep this post concise, I only list the important field for the tables)
1.) The reviews are stored in the table "reviews". It has the following fields:
id: uniquite ID, auto-incrementing.
title: the title that will show up in <head><title>, etc.
stub: a version of the title without spaces, special chars, etc. so it can be part of the URL/URI
text: the actual content
2.) All categories are in the same table "categories"
id: unique ID, auto-incrementing.
title: the full title/name of the categorie how it will be output on the website
stub: version of the title that will be shown in the URL/URI.
parent_id: if this is a subcategory, here is the categories.id of the parent category. Else this is 0.
order_number: simple number to order the categories by (for display in the navigation menu)
3.) Now I need an indicator which reviews are in what categories. The can be in multiple. My first idea was to add a "review_list" field to the categories and have it contain all reviews.id's that should be in this category. However I think that adding and removing reviews from categories would be a hassle and "unelegant". So my current idea is to have a table "review_in_category" and have an entry for every review-category relation. The structure is:
id: Unique ID, auto-increment.
review_id: the reviews.id
category_id: the categories.id
So if a review is in 3 different categories it would result in 3 entries in the "review_in_category" table.
The idea is, that when a user opens www.mydomain.de/animation/sci-fi/ the wrapper script will break up the URL into its parts. If it finds more than one category with category.stub = "sci-fi", it will check which of those has a parent category with the stub "animation". Once the correct category is identified (most the time the stubs are unique anyway so this check can be skipped) I want to SELECT all review_id's from "review_in_category" where the category_id matches the the one determined by the wrapper script. All the review_id's are put into an array. A loop will iterate through this array and compose the SELECT statement for listing all review titles (and create links to them using the stub values) by "SELECT title, stub FROM reviews WHERE id=review_list[$counter]" and then add "OR id=review_list[$counter]" until the array is completely travelled.
SO my questions are:
- Is the method my creating a single SELECT statement with potentially a large number of "OR id=" parts an "elegent" and/or efficient way to handle this situation or are there better variants?
- Does using a "taxonomy"-style table (review_in_category) make sense or would it be better to store the "membership"/"relation" directly in the reviews or category tables?
- Any other thoughts... I just started to learn this stuff and appreciate any feedback.
Thank you
Your design looks sound.
To retrieve all reviews in a category, you should use a join:
SELECT reviews.title, reviews.stub FROM reviews, review_in_category WHERE reviews.id = review_in_category.review_id AND category_id = $category
I'm trying to have mysql outputting a list of article categories.
there are lots of articles, each with preset categories that are stored in mysql. But many articles have the same category, so my list get very long with similar category results.
My idea is that if the user has posted 1 post in a category, the category gets listed. but this needs to understand that the category should just be listed once even if the user has posted multiple times in that specific category. How can i do this?
This is what i got, but not working:
foreach( $result as $row ) {
if($result>1){
$kategorilink= "{$row['kategori']}";
echo $kategorilink;
}
}
Try SELECT DISTINCT * FROM .... This will give you each different value only once.
modifying mysql data in php is not a good idea, you can select disting categories from mysql like
select distinct(category) from article where full_name='$safe_name'
or you can add group by clause to your query to group your result according to categories
select * from article where full_name='$safe_name' group by (category)
if you want to check number of results you can use mysql_num_rows() like
if (mysql_num_row($result) > 1){ //code here}
Im having trouble with an mySQL Query .What i need is a query that will display on the same page where im viewing a post a list of links to posts in the same category on the side of the page.Im almost finished a music site similar to youtube where users can listen to a track and see a list of tracks beside the post that are in the same category.
Any Information Ive found on this is just related to Wordpress pluggins which im not using.
My Database Tables are
Table_categories
cat_id
cat_name
Table - Mixes
mix_id
mix_subject
mix_description
mix_date
mix_cat -- mix_cat is the foreign key for categories.cat_id
The cat_id of Categories is linked to the foreign key mix_cat in table Mixes.I Know it has something to do with ?id= number of the id in one of the table,s
Cheers guys Thanks for your help
Trevor
You could try something like
Select * from Table_Mixes where id={insert id here, don`t forget to force the int type to avoid SQL Injection}
Get the row and now you have a $mix object with a mix_cat property (or $mix['mix_cat'], if you get it as an array).
Now you just
select * from Table_Mixes where mix_cat={insert $mix->mix_cat here} order_by {whatever} limit {how many links?}
and build the links.