FRIHOST FORUMS SEARCH FAQ TOS BLOGS COMPETITIONS
You are invited to Log in or Register a free Frihost Account!


Adding Categories





jasonf90
Hi guys,

I am trying to categorise content on my site. I have created a a few tables in my database, a product table where the user inserts a content, then there is a category where they create categories of their own.

When a user creates a new product they can select to add a category from the category table.

I have already written the code that allows the user to create a category. All i need to work out is how the user can add a category to their product.

I just need to know how to write the code to add the categories the user creates to their products

thanks
Very Happy
Marcuzzo
you could try adding the CategoryID to the record in the product table.
jmraker
If a product can be in 1 or more categories you can store the categories in one field with the keys seperated with something for explode() but if the field is a varchar(255) it's possible 255 letters won't be enough and getting a list of every product in a category requires a "CONCAT(',', category, ',') LIKE '%,123,%' "

Or you can create a product_category table that contains records that with every record links a product with a category. I think most here would recommend this method if a product can be in more than 1 category, otherwise a field that's the key to a category can be added to the product table
jasonf90
ive created the tables in the database so that the users has its own category table.

i just need the php code to bring up the dynamic selected value in a drop down box
sonam
It is not problem to create drop down menu from tables. For me is problem to understand how this tables are looking. Are you have each column for each submitted user category or you are fill all in one column.

Quote:
ive created the tables in the database so that the users has its own category table.


How is looking this tables???

Sonam
jasonf90
Im not to sure what you mean,

i have arrange my database like this, with the product table linking to the category table
sonam
jasonf90 wrote:
Im not to sure what you mean,

i have arrange my database like this, with the product table linking to the category table


I think about how category table looking not how is linked to product table. For example this is simple table with coma separated categories:

Code:
id    name    category
1     jason    travel, holiday, car
2     sonam  events, travel


PHP for read mysql
Code:
$result = mysql_query("SELECT category FROM category_table WHERE id=1 AND name='jason' ");
while ($row = mysql_fetch_assoc( $result )) {
              $cat = $row;
     }


PHP for creating HTML
Code:
$category = explode(',' $cat);
foreach(category as $val){
trim($val);
$select = @$select. '<option value="' . $val . '">' . $val . '</option>';
}


HTML
Code:
Select category:<br />
<select name="category"><?php echo $select; ?></select>


Note: this is not tested.

Sonam
jasonf90
thanks for that, i think i understand what you mean. I will give it a go and let you know if it works.

Thanks a lot
jasonf90
thanks a lot for the help, you inspired me to try something and it worked, here my code
Code:
   <?php
   //get id
   $user = $_SESSION['userno'];
   // connect to the database
   include ('connect.php');
   // Write out our query.
   $query = "SELECT categoryName FROM tblCategory";
   // Execute it, or return the error message if there's a problem.
   $result = mysql_query($query) or die(mysql_error());
   
   $dropdown = "<select name='category'>";
   while($row = mysql_fetch_assoc($result)) {
     $dropdown .= "\r\n<option value='{$row['categoryName']}'>{$row['categoryName']}</option>";
   }
   $dropdown .= "\r\n</select>";
   ?>


Thanks for the help
sonam
jasonf90 wrote:
thanks a lot for the help, you inspired me to try something and it worked, here my code
Code:
   <?php
   //get id
   $user = $_SESSION['userno'];
   // connect to the database
   include ('connect.php');
   // Write out our query.
   $query = "SELECT categoryName FROM tblCategory";
   // Execute it, or return the error message if there's a problem.
   $result = mysql_query($query) or die(mysql_error());
   
   $dropdown = "<select name='category'>";
   while($row = mysql_fetch_assoc($result)) {
     $dropdown .= "\r\n<option value='{$row['categoryName']}'>{$row['categoryName']}</option>";
   }
   $dropdown .= "\r\n</select>";
   ?>


Thanks for the help


Nice to see you are got it. Like said, the code is dependent of table structure and programmer. Good luck. Wink

Sonam
Related topics
Script php about gallery
Tutorialized, 4,988 Free Tutorials in 430 categories!
Google News Offers RSS and Atom Feeds
MSN probs
How do you generate forum traffic?
Add Contextual Search to Your Blog
science vs. religion
cards adding problem?
CON-serve-va-tives and LIE-ber-alls are essentially the same
Deism vs Atheism
Adding a new domain
adding url to google
All About WoW
Adding advertisements
Reply to topic    Frihost Forum Index -> Scripting -> Php and MySQL

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.