How can I implement a language system in my mysql database? - php

I am creating a video player application with php and mysql.
The application has videos that are gathered in playlists like this:
Playlists table:
+----+------------------+------+
| id | name | lang |
+----+-------------------------+
| 1 | Introduction | 1 |
+----+-------------------------+
Videos table:
+----+--------------+-------------+
| id | name | playlist_id |
+----+--------------+-------------+
| 1 | Video1 | 1 |
| 2 | Video2 | 1 |
+----+--------------+-------------+
It worked fine until now, because I need to build a searcher that finds videos depending on its name and language.
I though of creating another field called lang in the videos table, but then I realize that this maybe would contradict the normalization database rules. Because I would be repeating unnecessary information.
What can I do to select the videos without creating another field? Or do I need to create a new one with the repeated information?
EDIT:
JOIN LEFT both tables is not a solution, because I maybe add in the future a new table that links to playlists such as courses.

You can make LANGUAGE_ID COLUMN in Videos table,which will foreign key references to Playlists.lang .
Try above solution.
Hope this will help you.

You need to be clear about what attribute you want to assign to which entity (playlist, video or possibly course). You can assign language ids to both, playlist and video list items independently. Who is to say that you are not allowed to include a video with a language id of 2 in a playlist that carries a language id of 1? (This could, for example be a video in a foreign language that you want to appear in a playlist of your own language).
To search for suitable items you should then definitely use some kind of join (on video.playlist_id=playlist.id). The resulting table will contain both, video.language_id and playlist.language_id, which is not a case of having redundant information, as I have tried to explain above since they refer to different entities.

Related

How do I use multiple tables to create a user profile?

I made a firm to add a user to my database now I want to have two tables. One table keeps track of the languages the user knows and the other table the design software the user uses.
Would I create 3 tables (profile, languages, software) each with an I'd field and when I add a user add a row to each table?
As you begin to add several many-to-many relationships, you need more tables to 'link' the information together. Here's how I would tackle the problem:
Note The IDs should all be unique indexed columns. Consider using AUTO_INCREMENT.
Table 1: Contains user's profile information
| ProfileID |UserInfo |
|=======================|
| 0 | Info |
|-----------------------|
| 1 | Info2 |
|-----------------------|
Table 2: Stores the possible languages
|LanguageID |LanguageName|
|========================|
| 50 | Python |
|------------------------|
| 51 | Java |
|------------------------|
and so on...
Table 3: Stores the Profile links to the languages
|ProfileID |LanguageID |
|========================|
| 0 | 50 |
|------------------------|
| 0 | 51 |
|------------------------|
| 1 | 50 |
|------------------------|
Every time you wanted to add a language to a user's profile, you'd create an entry in this table.
You would add two more tables for the software a user knows. One table for all the possible types of software, and another to store the links.
When you want to retrieve the information, you would do an operation such as the one below:
SELECT * FROM Table3
LEFT JOIN Table2
ON Table3.LanguageID = Table2.LanguageID
WHERE ProfileID = [TheProfileIDToSearch]
This structure uses JOIN to link tables together to return information from several tables at once. Here is a W3Schools quick explanation about SQL JOINS.

insert reference in comment table that joins two sessions (blog and news)

I am finalizing a comments system and was with a doubt.
I have a table for blogs and one for news, and they accept comments.
My comments table receives the text and the id.
I wonder if I need to (or should I) go through some sort of reference to know where the comment comes from.
table comment
id | id_content | text | ref
1 | 1 | test | blog
2 | 1 | test | news
thanks
depending on the number of comments you expect to receive there are two ways of doing this ...
1 - parent_tbl, parent_id - in one big comment table
2 - two tables for comments with a parent_id - one for each primary table
either way you need to index properly, the second will always work faster, but it doesn't expand well if you say add "press_releases" now you have to duplicate code, tables, what not.

Most optimized way to organize blog category in MySQL & url mod_rewrite?

I've been creating a blog and I'm unable to decide the best way to categorize the articles.
The current way I thought was best would be to include the category in the articles table which includes the following rows.
articles structure
+----------------------+
| article_id |
| article_title |
| article_content |
| article_category |
+----------------------+
In each article where I write I'd include the article category. Example.
+------------+---------------+------------------+------------------+
| article_id | article_title | article_content | article_category |
+------------------------------------------------------------------+
| 1 | How do I.... | Want to know... | How-to |
| 2 | Learn faceb.. | Facebooks new... | Social Network |
+------------+---------------+------------------+------------------+
and then use if(isset($_GET['article_id']) && isset($_GET['article_category'])){ } to retrieve both together, or would it be better to create a new table for the specific categories? an example below.
categories structure
+-----------------+
| category_id |
| category_title |
| article_id |
+-----------------+
and whenever I need to retrieve the articles based on categories I'd just use it from the categories table (The categories structure above is just an example)?
Also if someone could spare another moment to answer a simple question I'd appreciate it. I'll be using the articles table above as an example with the information.
instead of using the following to retrieve the articles,
www.example.com/article.php?article_id=1&article_category=How-to
I would like to display the article url as followed, could this be accomplished by using mod_rewrite or would I need to create folders in my server for it to be possible?
www.example.com/How-to/How-do-i
Does stackoverflow use mod_rewrite to do the following?
www.stackoverflow.com/questions/id/title?
Note: I know how to display the title in the url, all I've been wondering is about the rewriting.
Thank you for your time.
What you want is a regular 1:n relationship. One article can have one category, one category can have multiple articles. This can be resolved with two different models where one holds an identifier of an element of the other model. Speaking in terms of SQL: You would have two tables where in articles you'd save the category_id (remember: one article one category, so this is a correct implementation).
articles
+----------------------+
| article_id |
| article_title |
| article_content |
| category_id |
+----------------------+
categories
+-----------------+
| category_id |
| category_title |
+-----------------+
This would usually be done using a foreign key constraint. Once done, SQL checks if an entry is valid (so an article cannot have a number for category_id that doesn't exist in table categories). Also this will allow you to specify, e.g. that once a category is deleted, all referencing articles shall be deleted as well, or not.
If you want one article to be able to have multiple categories, you are lookin for a n:m relationship. In SQL you create a third table
articles_categories
+-----------------+
| article_id |
| category_id |
+-----------------+
Both attributes build the primary key, each is a foreign key as well.
To your second question: Yes this can easily be done via mod_rewrite. There are lots of tutorials out there and it should be quite simple. SO probably uses this as well, but I can't tell you for sure.
As for storing/getting the data from your database, a very straightforward approach would be to have three tables. The articles table will stay the same, but the categories table needs to have the article id dropped from it. The third table will map M:N relationships between the two, and looks like:
id | article_id | cat_id
---------------------------
When an article is saved with a category, a new entry will be put in this table (we'll call it articles_categories_map) to relate the two. When you want to retrieve all articles by category, the query will be thus:
SELECT `article_id` FROM `articles` INNER JOIN `articles_categories_map`
ON `articles`.`id`=`articles_categories`.`article_id` INNER JOIN `articles_categories`
ON `articles_categories_map`.`category_id`=`articles_categories`.`id`
WHERE `articles_categories`.`title` = ""
Note: This approach will allow you to attach several categories to one article, and vice versa.
As for rewriting your URLs, mod_rewrite can certainly do this. You should take a little time investigating common approaches to this problem when using MVC. This will give you insight into how you might want your rules to be structured.
Cheers
Take a look at using pivot tables. This isn't some fancy datatype in MySQL, but just a concept of how to organize your data. The idea is similar to a Many To Many relationship. Where many articles have relationships with many categories. This also allows you the flexibility of assigning more than one category to an article. PHP frameworks like Laravel, make these sort of relationships a breeze. Here's a basic overview.
articles Table
articles structure
+-----------------+
| id |
| article_title |
| article_content |
+-----------------+
categories Table
categories structure
+-----------------+
| id |
| category_title |
+-----------------+
Then you use a pivot table to have different relationships between articles and categories.
categories_articles Table
articles_categories structure
+-----------------+
| id |
| article_id |
| category_id |
+-----------------+
In this pivot table you can have multiple rows for the same article to indicate multiple categories for that article. In the end, you'd do something like this.
<?php
$article = // Get the article you want, including it's id.
$categoryIds = // SELECT * FROM categories_articles WHERE article_id = $article['id']
$categories = // SELECT * FROM categories WHERE id = (Each of the $categoryIds)
This is obviously psuedo code, and you can actually get this all done with a single SQL query, but you get the idea.

Inserting and searching for multiple keywords

I am planning designing a PHP MySQL database for a local museum's photo gallery.
One thing that terrifies me now is how to implement saving multiple keywords for a single image for items where more than one technic or material was used to design them, e.g.
PAINTING TECHNICS:(liner, wiping, scumbling), MATERIAL USED: (oil, canvas,).
Ideally, I would have to use the select option lists to save them for each record. But if I want to save two or more of the words from the same list, I cant.
Do you have any suggestions I might consider?
joseph
You will need to implement a one-to-many relationship between your image table and your keyword table. One image can have many keywords. Each row in your keyword table will need to reference a row in the image table through a foreign key. For example:
images
id | asset
---------------
1 | image1.png
2 | image2.png
keywords
id | image_id | keyword | category
-----------------------------------------------
1 | 1 | liner | painting techniques
2 | 1 | wiping | painting techniques
3 | 1 | scumbling | painting techniques
3 | 2 | oil | material used
Or something along those lines. This is how you allow one row (an image) to be associated with many rows (keywords) in a different table.
For each keyword submitted, you would add another row to the keywords table, making sure to reference the associated image.

Creating a MySQL table upon user registration

I am developing a website which will collect and store information for users.
I need to create a table specific to each individual user on registration to store the information they are searching for using the website. The table created will be named after the newly registered user's username.
Then when the user is logged in and runs the search, the data collected will be stored in a database using a MySQL insert query where the table name is a variable containing the logged-in user's username which will also be the table name.
I am an amateur developer and have searched everywhere to try and find a solution but I cannot seem to find anything evenly remotely similar to my problem.
Thank you in advance for any help!
Creating tables on the fly is more trouble than it's worth and very much swimming against the tide with any SQL database.
The reason you haven't found any docs about the approach you mention is because this problem is generally (almost without exception) solved best by having all the data in one or more tables, and including a column to specify which entity (user) the row is associated with. In your case, this might be an email address, or a username, or just a sequential number.
E.g.
| user_id | email | first_name | last_name | fave_color |
- - - - - -
| 1 | "a#b.c" | "anton" | "aardvark" | "red" |
| 2 | "b#c.d" | "miles" | "o'brien" | "infrared" |
| ... | | | | |
First take name from user like:
$fullname="$fname"."_"."$lname";
Then, write a query like this to create a table of that name
$sql="CREATE TABLE $fullname(ALL THE COLLUMNS YOU WANT TO CREATE)";
$result1=mysql_query($sql, $db_link);
this query is from my project. Works fine in wampserver.

Categories