March 24, 2012

One Article multiple categories system in PHP SQL

Question by Vehlad

Multiple categories selection system for Article

Earlier M using two tables
articles & categories and save category ID in articles table
But in this system I can save only one category ID per article
I want to save Article in Multiple categories

While searching I found same question on StackOverflow

I understand the whole concept of adding one more table of relationship & saving Article ID & Category ID in this table.
But not aware how to implement multiple selection system using arrays in New Article Form & Edit Article Form.

earlier I am showing Select in my form to display categories list in Add Article & Edit Article Page.

pls someone explain me How to show categories list in multiple checkbox style in my form so user can select multiple categories and then after POST how to get checkbox data and run query to insert data in both Article Table & New Relationship table with selected categories ID

want to display category List like this screenshot

Many Many Thanks…

EDIT:
I use echo '<input type="checkbox" name="selcats[]" value="$catid"> ' .$catname;
to display Categories check box
Its showing in a row side by side.
how to change display like screenshot i.e. list with scrollbar
& need to process this array and insert in new table while inserting article in databse.

EDIT 2
got the checkbox display correct in a scroll list by using a div 😀
<div style="height: 150px; width: 200px; overflow: auto;">

Answer by Foreba

In my case, I use a “tag system”, for instance: You have an article with one category, nothing will change that… In adition, you can create another field in the article table with the relevant words (or whatever you want) and separete them with spaces or commas.

// Get the data from your database
$tags = 'world, travel, turism, cities';

// Separate the values
$tags = explode(", ", $tags);

// Create a link for each one
foreach($tags as $t)
{
    echo ' <a href="?tag=' . $t . '">' . ucfirst($t) . '</a> ' . "rn"; 
}

It should output:

 <a href="?tag=world">World</a> 
 <a href="?tag=travel">Travel</a> 
 <a href="?tag=turism">Turism</a> 
 <a href="?tag=cities">Cities</a>

And it means that you can SELECT articles that have the title LIKE the tag, or whatever you want to search.

Is that what you was looking for?

Answer by Starx

You basically require two tables, with this structure

table_categories

_____________________________
|    id    |      title    |
-----------+---------------+

table_category_detail

______________________________________________
|    id    |  categoryId   |    articleId    |
-----------+---------------+------------------

To extract all categories, select all from the table_categories and put up into the select menu with mutiple selection enabled.

Next, when posted get the selected box values and insert into table_category_detail one by one

This is how you create a select box

$query = "SELECT * FROM table_categories";
$result = mysql_query($query);
echo '<select multiple="multiple">';
while($row = mysql_fetch_assoc($result)) {
   echo '<option value="'.$row['id'].'">'.$row['title'].'</option>';
}
echo "</select>";

Or a Multiple Check Box

while($row = mysql_fetch_assoc($result)) {
   echo '<input type="checkbox" name="selcats[]" value="'.$row['id'].'"> ' .$row['title'];
}

After the post:

 // Here write codes to insert the article first

 $articleId = mysql_insert_id(); //get the id
 $values = $_POST['selcats'];
 foreach($values as $value) {
      $query = "INSERT into `table_category_detail` VALUES(NULL, $value, $articleId)";
      $result = mysql_query($result);
 }

Author: Nabin Nepal (Starx)

Hello, I am Nabin Nepal and you can call me Starx. This is my blog where write about my life and my involvements. I am a Software Developer, A Cyclist and a Realist. I hope you will find my blog interesting. Follow me on Google+

...

Please fill the form - I will response as fast as I can!