2

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 :D <div style="height: 150px; width: 200px; overflow: auto;">

2
  • Step back and look at what's required, and stop fixating on the PHP code. Learn the concepts involved, and the code will naturally flow from this. Commented Mar 24, 2012 at 14:34
  • Thanks why M here TO LEARN :D Commented Mar 24, 2012 at 14:38

2 Answers 2

3

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> ' . "\r\n"; 
}

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?

Sign up to request clarification or add additional context in comments.

1 Comment

hmmmmm your TAG system solution is cool thanks 'L try that but I need multiple categories solution
1

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);
 }

7 Comments

yea I understand the table concept need help in fetching data from categories table & displaying in HTML FORM multiple check box to select category & after POST processing categories IDs in new table
thanx a lot frnd 'L try this code after post how to insert these multiple selection array in DB along with insert query to add data in articles table
Ahhh Its a multiple select... I want to use check boxes in the categories list
Got checkbox display <input type="checkbox" name="selcats[]" value="$catid"> ' .$catname;
How can i list lets say 10 articles (title, text, date) with it's categories listed next to it? Do i need to first select articles and as i show them in a while loop, make another query for each article to get category names of that article? or is there a left join solution... i try to printout like Article Title posted in X,Y,Z (x,y,z are categories...) but i dont want to make extra mysql query calls
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.