PHP laravel don't select duplicate fields shared in two tables - php

In my project I want to query orders. every food have a category and every category have a parent. so my problem is some orders have two type parent category. like Indian food and italian food in one order. I used select multiple element. and parent categories are shown inside it. one another item is all. so when user selects all parent categories the orders that have two type of categories like explained above it's shows more than 1 in table and I have no idea how to select only one of this duplicate fields.
my query code is :
if (isset($subset) && $subset!=""){
$query->leftJoin('z_food_orders','z_food_orders.order_id','=','z_orders.id')
->leftJoin('z_foods','z_food_orders.food_id','=','z_foods.id')
->leftJoin('z_food_cats','z_foods.cat_id','=','z_food_cats.id')
->leftJoin('z_res_subset','z_food_cats.parent_id','=','z_res_subset.id');
if (count($subset) == 1){
$query->where('z_res_subset.id',$subset);
}else{
$query->whereIn('z_res_subset.id',$subset);
}
}

Related

database management to goods to store their categories

I have to create a web site witch contain goods. these goods have their own categories.for a
example a good can be categorized in many categories or sub categories.
I already have a item table witch contain their properties. and can anyone say how to keep it`s categories in database without duplicating data
Ok. In this case an Item may have many categories and a category may have many Items. So its a many to many relationship. Try as follows,
Your Item table may look like this,
Item(id, description, amount, ... )
In your Category table,
Category(id, name, description, ... )
And there will be another table to assign categories to items,
ItemCategoryAssignments(itemId, categoryId)
And there will be two foreign keys at ItemCategoryAssignments table. That,
itemId references id in the Item tabel
and
categoryId references id in the Category tabel
Like that. There will be no duplicated rows. Hope this will help.

Create a list in php from two tables in mysql

How could I create a list in php of categories and types of topics that are mixed together?
Meaning, the list should look like this:
Sports (this is a category)
Soccer (this is a type)
Golf (this is a type)
Basketball (this is a type)
Science (this is a category)
Biology (this is a type)
Chemistry (this is a type)
Anatomy (this is a type)
The difficult part is that sports and science come from the same table (called categories), and soccer, golf, basketball, biology, chemistry and anatomy all come from a different table (called types). In the MySQL table "types" there is a foreign key that points to the id of the entry in the categories table. The current setup in MySQL cannot easily be changed as the navigation of the site is dependent on it.
How could i use php to put the types in between the categories?
Currently the code i have is this:
<?php session_start ();
include 'connect.php';
$result = mysql_query("SELECT * FROM categories JOIN types ON categories.catid = types.typecat");
while($row = mysql_fetch_array($result))
{
echo $row["catname"];
}
?>
However, that outputs "SportsSportsSportsSportsSportsScienceScienceScience," which it obviously shouldnt. There are 5 types in sports, and 3 in science. So i am not sure what is happening there and i cant proceed on to the next step of adding in php to include the types.
I think you need two lists with main category and sub-category, if so you can do as follows
fetch categories from db and display in list 1 with sub category list empty
on change of category submit the form using jquery or javascript
based on category posted fetch sub-categories from database and display in list 2 and select attribute of list 1 make it as selected based on posted category value.
You can use ajax too instead of submitting form.
I believe you have an id_cat in your types table.
Then you should use SQL JOINs.
I don't have the code for you, but the query would be either:
select category, type from categories c, types t on c.id = t.id group by category;
or:
select distinct category from categories;
Then query each category individually.
I suspect the single query would be more efficient.

How to display records from SQL through menu, using PHP?

i have made a static drop-down menu for my e-commerce website. i have various categories and then sub categories.
my main tabs are, "CLOTHES", "FOOTWEAR", "ACCESSORIES" etc.
The clothes tab is divided into two parts one is brand wise and one is type(shirts, jeans, etc.)
Now my question is, if i go to the clothes tab and then click on shirts, how can i traverse and retrieve the records from my SQL table.
I have made the connection with the database, my table name is 'products'. The page to display products is list.php.
I am new to PHP and i know a little bit about this language.
have a pages table and categories table
also have cat_parent in the categories table
Then have a php loop to go through each topmost categories and then subcats and ......... and then pages.
watch it in action this is the site i am working a the moment
First you should have categories & sub categories in single 'categories' DB table.
In products table, you will have a category_id column as foreign key.
So based on this category_id column, you can retrieve products from your DB.
Also each product in DB should have assigned a particular category.
You have to use AJAX or a Javascript function to achieve this type of functionality. Here is how:
Use an onChange event in your static drop down menu HTML code.
Make a function in a <script> tag to catch the id of selected item.
Pass this id to the PHP file in the URL.
Give your <div> id name in this Javascript code.
After calling the PHP file catch the id from get method and pass it to the query.
You can get the desired result.

Remap concatinated values in mysql db

I have 2 mysql tables one contains category mapping relationships between a supplier and our store: Basically what we call their categories eg ~ denotes sub category level:
Cateogry Mapping Relationship Table
Supplier Cat..........| Our Cat.....
dogs~leashes~long.....| pets~walking
dogs~leashes~long.....| pets~travel
dogs~leashes~short....| pets~walking
dogs~leashes~nylon....| pets~walking
dogs~feeding .........| pets~feeding
the other table contains supplier item ids with the categories that the supplier has the products in. Multiple categories are concatenated in the same field with a ','.
Such as the following:
Supplier Item Table
Supplier item ID...| Supplier item Categories
28374 ............| dogs~leashes~long,dogs~leashes~nylon
My task is to replace the item paths in the supplier list with the correct paths from our store category list so I can put them in our database.
So the result of the php / mysql function Im trying to build for the above data modification would be (I dont care if I run this in php or mysql which ever is easier to get the job done.):
Supplier item..| Supplier item Categories ..............| New item Categories
28374 ........| dogs~leashes~long,dogs~leashes~nylon ..|pets~travel,pets~walking
Im not sure how to handle the concatenated field and I would appreciate any help at all
thank you
write a query that selects id,cat from supplier item table
In a php loop split the category by ',' and do a select to get your category
write into a new table id/your category
check new table for validity
delete old table and rename new table

Articles and Categories - The efficient way

I want to add categories to my website - but with more complex sorting.
Lets say I have article a. now, I want a to be under main category a, but also on category c thats its parent is b.
I thought of doing it like this:
An article would have a main category field, and also parent_ids
parent_ids would be every category this item is belong to.
my problem is: lets say i have: parent_ids: |1|5|2|7|.
now i am in category id - 7.
how would my select query look like?
select * from categories where parent_ids LIKE '%|7|%'
is it efficient? is there a better idea?
Never store multiple values in one column. All column values in a normalized database should be atomic (single-valued).
The relationship between articles and categories is many-to-many. That means you should have a separate table, such as article_category (article_id, category_id) containing one row for each category each article is in.

Categories