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);
 }
June 14, 2010

How to make categories and current page highlighting in PHP?

Question by Markus Ossi

I am trying to find some example code or best practices about making CMS-type categories with PHP.

This is a problem that has for sure been solved gazillion times but for some reason I am unable to find any example code using PHP about how to implement this.

As far as I can tell, there are two parts to the problem. The first one has to with the styling side of things:

  • outputting the link in the navigation so that the current page has a special style (class=”active”) and
  • to not print out the link for the current page.

The second part is handling categories, subcategories and the dynamic pages under the categories.

The second part seems pretty straightforward. I am thinking of making it so that the name of the category in the navigation is a link to categories.php?id=x and on this page I just print out the pages with that category id. Then, if the user klicks on a page he will be taken to pages.php?id=y.

However, I am not quite sure on how to make a navigation to check if we are now on the current page. Should I just use some case switch or what?

Any ideas or links to some good example code are much appreciated.

Answer by epalla

If you’re loading the page CMS-style presumably you’ve got some sort of page identifier that’s accessible within the code? A current_page_id or something like that? From there I usually just do this:

<ul id="menu"><?
foreach ($menu_items as $menu_item) {
    ?><li <?=($menu_item['page_id'] == $current_page_id) ? 'class="active"' : ''?>><a href="<?=$menu_item['link']?>"><?=$menu_item['title']?></a></li><?
}
?></ul>

Answer by Starx

no need for PHP, use CSS

a:active { color:#09f; }

UPDATE
use this little jquery code

$("*").find("a[href='"+window.location.href+"']").each(function(){
   $(this).addClass("current");
   $(this).attr('href',"#"); //nullifying the link
   //add your own logic here if needed
})
...

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