multi category in page PHP MYSQL - php

i have problem with multi category for page in php and mysql,
i have "Business" , "Category" and "Business_con_Category" tables in mysql.
the Business_con_Category table is connect between Business to Category,
so i think doing select multiple, and implode for add to table.
but my problem is editing how i can know if remove category and delete from table ?
i treid in to array old category and new category and compare, but is not work good..
do you have an idea
thanks!

An easy solution is to not try to remove only some rows from business_con_category. Instead, remove all rows for the given business, and then add the rows with the categories that still should be present.

Related

PHP - Mysql how show items from a DB related to specific column

Hello below some info about my problem
My_DB name is: products
Table name is: spec_products
Columns in spec_products are:
id|sku|brand|title|price
How can i show (with php) only items from the brand column?
In particular it's a piece of code that i need insert in sidebar of single product page that show products correlated to the brand
Here is the MySQL query:
Select brand From spec_products;
use clause distinct to avoid duplicate values if more of one of your products have the same brand
select dintinct brand from spec_products
From what I understand, you want to display only the brands from the table. If this is the case, it is a simple Selecting Data from MySQL that you would have got by simple search on Google.
You can refer to a W3School tutorial here
You can simply run a SELECT query for brands:
SELECT brand FROM spec_products

Create A List From a Category Section in mysql

I was wondering if mysql has a way to look at a column and only retrieve the results when it finds a unique column once. For example
if the table looks like this:
id name category
1 test Health
2 carl Health
3 bob Oscar
4 joe Technology
As you can see their are two rows that could have the same category. Is their a way to retrieve the result where the array will one only return the category once?
What I am trying to do is get all the categories in the database so I can loop through them later in the code and use them. For example if I wanted to created a menu, I would want the menu to list all the categories in the menu.
I know I can run
SELECT categories FROM dbname
but this returns duplicate rows where I only need the cateogry to return once. Is there a way to do this on the mysql side?
I assume I can just use php's array_unique();
but I feel like this adds more overhead, is this not something MYSQL can do on the backend?
group by worked perfectly #Fred-ii- please submit this as answer so I can get that approved for you. – DEVPROCB
As requested by the OP:
You can use GROUP BY col_of_choice in order to avoid duplicates be shown in the queried results.
Reference:
https://dev.mysql.com/doc/refman/5.5/en/group-by-handling.html
By using database normalization, you would create another table with an unique id and the category name and by that link those two together, like
select * from mytable1
on mytable1.cat = mytable2.id
group by mytable1.cat
You can ofcourse also use group by without multiple tables, but for the structure, I recommend doing it.
You can use select distinct:
SELECT DISTINCT categories
FROM dbname ;
For various reasons, it is a good idea to have a separate reference table with one row per category. This helps in many ways:
Ensures that the category names are consistent ("Technology" versus "tech" for instance).
Gives a nice list of categories that are available.
Ensures that a category sticks around, even if no names currently reference it.
Allows for additional information about categories, such as the first time it appears, or a longer description.
This is recommended. However, if you still want to leave the category in place as it is, I would recommend an index on dbname(categories). The query should take advantage of the index.
SELECT id, name from dbname GROUP BY categoryname
Hope this will help.
You can even use distinct category.

PHP MYSQL - Search comma separated list with a comma separated list

Dear Stackoverflowers,
I have a mysql query which checks if some sub categories are part of a categoryID to show all products from subcategories in the main category.
For example:
Category
Subcategory 1
Subcategory 2
Subcategory 3
For example products are added to subcats but are ALL beeing shown in the head category.
This works fine and i do this with WHERE cID in (' . $subids . ')
But now comes it, the cID used to hold just one value but since it has to be possible to add products to multiple categories i now save the multiple selected ids as comma separated in the cID field.
So im searching for a way to basicly find a match from a comma seperated list within a comma separated list because cID has now become comma separated ID's and FIND_IN_SET does not work with this.
I hope someone can shine some light, thank you all!
Have a read of these:
Many-to-many data model
Junction table
Implementing this, as opposed to comma-separated values, should probably save you and your coworkers a lot of headache and simplify your queries, effectively eliminating the need of complex queries. It should be faster, too.
Add full-text search index on this column in the database and then use following sql query
Ref - http://dev.mysql.com/doc/refman/5.0/en/fulltext-search.html
Storing a CSV in a single field is not a good idea. Searching on that requires use of LIKE clause.
where CID LIKE '$cid1,' or CID LIKE ',$cid1,' or CID LIKE ',$cid1' or
CID LIKE '$cid2,' or CID LIKE ',$cid2,' or CID LIKE ',$cid2' or
.... so on
Also FIND_IN_SET would work too.
where FIND_IN_SET( $cid1, CID ) OR FIND_IN_SET($cid2, CID) .. so on
i now save the multiple selected ids as comma separated in the cID field. That's a very bad approach ,why would you store a CSV in a single field ,you shud change your design ,make a seperate table where each value will have a new row and will be mapped to a FK.
What you really need to do,is work on your DB design,read on Normalization and other related topics,I have so added some links,
MSDN Link on Many to Many
Databae Normalization

Combining multiple rows or results with the same title to form drop down menus with PHP and MySQL

So I am picking up a project that was quit halfway through by the last guy so that I could get some more practice with PHP and databases. I have run into a problem, and I am sure it is common enough that there is a standard solution, but I am unable to find one.
The db I am working with has 4,600, so reorganizing is out of the question. It is a db of liquers for a wholesaler. Here is what the results page looks like currently:
What I am trying to set it up so the results are returned in list form, with only one title and dropdown menus for the different sizes/prices of products that looks like this:
The problem is that there are multiple entries in the db for each product. In this example there are 3, while some have 1, and some have 2.
I am really not sure how to go about this, and any help would be greatly appreciated. Thank you.
I'm not sure about the PHP syntax, but pseudocode here's what you could do:
allProductsReturnedFromMySQL = QueryYourDatabaseForAllProducts()
Hashtable[productId, List[productSizes]] dropDownsByProduct;
Hashtable[productId, commonProductInformation] uniqueProducts;
foreach (product in allProductsReturnedFromMySQL) {
if product.productId not in uniqueProducts
then add it with the product information that does not vary
if product.productId not in dropDownsByProduct
then add it with an empty list
append the size of this product to the corresponding list in dropDownsByProduct
}
After that little bit of logic you'll have all your unique products with the common properties for each one, and a way to fetch the corresponding sizes drop down. If you wanted to do this purely in SQL to minimize the data that's transferred, you could do something like this:
-- this would get you your products
select distinct id, property1, property2 from product
-- this would get you your drop downs by product
select id, size from product order by id
You can then build the same drop down hashtable by iterating through the second result set.
I'm not sure if this is the best way, but I've always approached this by altering the query so that it is sorted by product name. Then as you iterate through the rows, check to see if the product name matches the one you just processed. If it's the same, then this row is a different size of the same project.

mysql id field just submitted

Ok, I find myself doing this often. I'll attach ids to my tables and I'll "link" the tables together. Just a simple example, a team roster might have categories. Each category has an id. When I add players into the roster, I'll assign them a category id as well to signify that they are part of that category.
My question is, for this example, say I'm creating a category and a player at the same. I submit the category but now I have to get the category id to assign it to the player table row. Any suggestions on how to do this efficiently? Currently I would query the database again, and search for specific data related to the category, which doesn't seem very effective.
Note also, the id's Im using are generated by mysql.
Any suggestions would be helpful!
Check out mysql_insert_id()

Categories