Please help me how to create vertical menu or sub menu using these tables in PHP.
The two tables are cat(id,cat_name,main_cat_id) and main_cat(id,main_cat_name).
main_category table
id main_category_name
1 Hotels
2 Compressors
3 Apparel & Clothing
4 Automobile
5 Car Rentals
6 Mobiles & Accessories
7 Computers & Peripherals
8 Doctors
9 Education
category table
id category_name main_category_id
1 Pizza Center 1
2 2 Star Hotel 1
3 Hotel & Restaurant 1
4 5 Star Hotel 1
5 Air Compressor 2
6 Flare Nuts 2
7 Auto Accessories-Car Stereos 4
8 Automotive Parts, Components & Spares 4
9 Motorcycles, Scooters & Two Wheeler Parts 4
My policy of encouraging a prior attempt applies here. You've laid out the database, which is great, but you need to open a text editor and start on the PHP. You can do it!
Readers here could give you a working answer, but that suffers a number of drawbacks:
Firstly, they might misunderstand what you need, and so spend a long time on something that does not help you.
Secondly, there are several million people out there that would like people to work for them for free, and we'd be here until Doomsday if we did that.
Finally, if you get a working answer with no personal research, you might not learn anything.
Here's a suggested plan of action:
Install WAMP/MAMP/LAMP on your computer
Set up your database structure and test data in a MySQL instance on your development machine
Write some PHP code to connect to the database and run a test query on it, dumping the data in a web page
Once that works, modify the query to read the categories, using an ORDER clause to get them in the right order
Now you have enough information to create a menu. Look up the format of a <select> tag using <option> tags, each of which contains the name from your database. You'll need a value attribute for each one - this is usually the id from each row of the database
Wrap the above in a <form>
From here, you can expand your menu to contain a second level. The simplest way of doing that is to research the <optgroup> tag - much under-used, in my opinion. This allows you to set up a two-level menu that is intuitive for users and simple to read values from in code.
Give that a go?
Related
I think than search is one of most trivial problems in mysql, but i have an interesting request for online shop. By the way, I use Laravel v8 Eloquent for quering, if it helps with solution.
Lets assume that we have db table goods
id | name
1 | Fried chicken leg
2 | Chicken freezed
3 | Vegetable soup with chicks
4 | Enchickened grains
and when user type chicken I want to show first a lines that starts with search query (id 2 in example table), and it would be perfect if after those entries were those that have a search query as entire word (id 1) that starts with it, followed by any other line that contains search query. I ask about it primarily because of need of pagination since shop has a thousands of entries, so I do not want to make and merge results for few subsequent requests
I tried to make two requests and merge them, making a pagination out of these results, but I have quite complicated and rather expensive resulting request that touches a few relations for goods like promo, categories, blocks and few more, so it will be a twice or more as expensive and this is not something I want to see if there is a way to optimize a request.
EDIT Example:
If I enter chicken I want to see Chicken freezed first, then (ideally but not required) Fried chicken leg and after that anything that have chicken in it. if prioritize in regex: $chicken.* -> .*\bchicken.* -> %chicken% (last one for mysql)
A long time ago, I built an importer for xt:Commerce that worked fine up to v4.1. The data came from the same PIM system as before.
But now, v4.2.0 is out, and they've added a nested set for the categories. Anyways that's ok, but they've added two new columns called categories_left and categories_right - and they mess up my importer.
First of all, I really don't understand what those columns are for. My importer works and puts all stuff correctly in the database, but in the frontend, the cats style is broken. The cause is that my importer sets all cats in those two columns to 0.
If I add the cats in the backend, it works fine and the frontend style is working. It seems that these columns are incrementing themselves by 2.
categories_id categories_left categories_right parent_id
1 1 2 0
2 3 4 1
3 5 6 1
4 7 8 1
5 9 10 1
So I implemented a func in the importer that will do exactly the same shown above. On the homepage, the frontend style is fine, but if I click on a category the style is broken again.
Could anyone explain what these two columns are for, and how to get this fixed, please?
If it is only a manual/one time import, you can leave the columns categories_left and categories_right empty. After the import add a dummy-category in the administration backend. When the new category is saved, all categories_left/right columns are recalculated and saved to the database.
Solution:
Got the answer by myself and built a function that loops through the whole cats.
This helped me a lot: nested set "Raupe", full documentation here: wolkekraft Nested Set (german)
Im new to programming and databases in general. I've read PHP.MYSQL for DUmmies 4th edition thus far, and am trying to create a database of my own which stores the Menus/Inventories of shops. I am currently using XAMPP/MYSQL.
Sample tables of my database are as follows:
SHOPINFO
Shopname Outlet Category Subcategory Item
PizzaHut Main Drinks Carbonated Cola
PizzaHut Main Drinks Non-carbonated Orange Juice
BurgerKing Central London Burgers Beef Whopper
BurgerKing South London Burgers Beef Whopper Jr.
BurgerKing South London Drinks Carbonated Cola
I am currently wondering if i should split the above table up into :
SHOPINFO
Shopname Outlet Category Subcategory Item
PizzaHut Main 1 1 1
PizzaHut Main 1 2 3
BurgerKing Central London 1 5 4
BurgerKing South London 1 5 7
BurgerKing South London 3 3 2
Where the Categories,Subcategories and Items are all split up into different tables where they are identified by their CategoryID,SubCategoryID and ItemID respectively.
The 2 Major Questions i have regarding this decision are:
1.Would it really be beneficial to split the table up? I am asking this as would it not be far easier to query the first table rather than the second table? E.g i could simply do something like
$query="SELECT * FROM SHOPINFO WHERE Shopname='BurgerKing' AND
Outlet='South London' AND Category='Drinks' AND
Subcategory='Carbonated'";
$result=mysqli_query($cxn,$query) or die("Error");
while($row=mysqli_fetch_assoc)
{
extract($row);
echo "$Item";
}
instead of the query that would have to be done if the table was split(Which i have no idea how to do, and which makes my mind hurt).
2.If i were to split the table up, i'm guessing that i would have to create separate tables for each shop for the Category,SubCategory and Item, using the Shopname & Outlet as primary keys, which i would then link to the SHOPINFO table with foreign keys, probably using ON DELETE CASCADE & ON UPDATE CASCADE. The main question is, exactly what benefits would doing this bring about, apart from making the querying more complicated?
PS: My concerns are regarding future scalability.(E.g Adding countries,Cities,States to Outlets in the future)
Advice,Help,Opinions,Insults and Flaming are appreciated.
Thanks!
I think that your entire structure is wrong. You need to split everything up into separate tables, starting with a table of companies - Pizza Hut and Burger King are the only entries which you have shown. Then you need a table of branches which would contain a foreign key to the company along with atomic data about each branch, like its branch number, address etc. Then you need a category table, whose data seems to be either drinks or burgers, followed by a subcategory table (carbonated, non-carbonated, beef, chicken, etc) which contains a foreign key to the category. The you need a products table (cola, orange juice, whopper, whopper jnr) which has a foreign key to the subcategory. Finally, there would be a branch-products join table which has two fields (branch number, product number).
This structure, whilst seemingly cumbersome, is the most flexible and allows one to write all kinds of queries with little difficulty - total sales by company, total sales by branch, etc. This may not be clear when you are defining your database but you will appreciate this in the future when you are asked for some data aggregation which you had not foreseen.
Keeping everything in one table is a very bad idea: let's say that Burger King is taken over by The Grill Master (an invented name): this way you only have to change one record to reflect the change, whereas when using one huge table, you would have to update many records.
The 'huge one table' approach comes from people who use Excel as their database manager and basically have no experience of anything more complicated. The professional answer is to use a relational database with everything split up into tables.
I apologise if the above seems condescending; I am a doctoral candidate who is looking at the use of Excel within companies which use relational databases, so I am somewhat biased in my views.
We are currently considering migrating our database code in php to Doctrine2. It seems to us that most of the things we would like the do are possible with Doctrine2. There is only one thing which we are not sure if it is possible and how we should do that. Our database model can't be changed right now because another project is using the same database. We can only change one thing at a time. Of course we are also curious how to improve the database model but that is not our main concern.
The issues is like this. We have a product table containing basic product information like this:
[product_id | code | name | description | price]
4 AS84GD Steel brush Strong steel brush..... 25.50
28 HDD21N Sand paper Sand paper for wood..... 0.40
etc
We have different users for our product which al have different extra product properties. So we have an product_extra_field table which could look like this:
[extra_field_id | name]
3 weight
4 color
9 supplier
and finally the table with the extra data product_extra_field_data
[product_id | extra_field_id | value]
4 3 280
4 4 brown
4 9 company_x
28 3 3
28 4 yellow
28 9 company_y
How do we right the necessary Doctrine2 code to get this data in one object, so we could have an object like this?
[Product]
Id : 4
Code : AS84GD
Name : Steel brush
Description : Strong steel brush.....
Price : 25.50
Weight : 280
Color : brown
Supplier : company_x
I've added a tag for Hibernate because I can read (N)Hibernate code and translate the ideas to Doctrine2.
So far, I don't think you can achieve that with mapped superclasses.
But, you could use native SQL within the doctrine result set mapping:
http://docs.doctrine-project.org/en/latest/reference/native-sql.html
Those are one of the drawbacks when mapping between objects and relational data, I mean, if you really want just one entity instead of replicating the relationships from the database.
You could create several classes:
- Product
- ExtraField
- ExtraFieldValue
With the following relations:
One Product has many ExtraFields (One To Many relation), one ExtraField has many ExtraFieldValues (One To Many relation), consedering your DB structure.
Then you have to customize your getters/setters to get only the first result of the collections (if in fact your relations are One To One)...
Don't know if I'm clear enough.
I have been trying to come up with a way design a webpage to allow users to make selections on a webpage and then query those selections in a database and returned to the user.
For example, my database is a list of houses. The data base table is set up something like this:
Country Region State City House
1 1 1 1 A
1 1 1 1 B
1 1 1 2 C
1 1 1 2 D
1 1 1 3 E
1 2 2 4 G
1 2 3 5 H
1 3 4 6 I
1 4 5 7 J
From the search bar, I want the user to be able to say show me any and all houses in region 2, any houses in state 3, and any houses in city 1.
In this example, the data base query would come back with houses G,H,A, and B. The order of the search and results don't really matter right now.
Is something like this even possible? I have considered using check boxes on the website but ideally I would rather just have one search bar using autocomplete (jquery) where each entry has specific values tied to it. These values are what would be passed to the search query as variables.
you could use comma separator as when you use google maps, then split the content of the search box using the comma delimiter and then you will have the 3 parameters that you need.
Your best bet would be <select> elements in a <form method="GET" action="somefile.php">, and then within the file somefile.php retrieving the $_GET values that the user posted on the form in the <select> elements and then generating SQL queries appropriately to retrieve the data.
The answer is yes, it is possible, but you're asking a lot if you're asking for someone to do it for you.
As #khanahk said, you could use select boxes.
I couldn't figure out how to search for more than just one term at a time.
You could have multiple select boxes, such as four or five on one line, with a submit button to search using the paramters selected by those select boxes.
Or you could do something really cool with a search box that uses keywords to separate the different type of search, ex.) House-a IN region-1
I'd take a look at the following link:
http://www.arroyocode.com/client-side-keyword-search-with-jquery-ui-autocomplete-and-asp-net-mvc-4