category and subcategroy in same table - php

I have to create a table for category and subcategory in the same table.My table format will be as following:
Id Title Parent id
1. Spain None
2. Barcelona 1
3. India None
4. Delhi 3
Please give me mysql query for this.

Create a normal table in which the value of parent ID would be zero (0) for top level entities.
You may also keep the column nullable by having NULL for top level entities.
I would recommend to use zero for type safety.
Please use this as a starting point and not as a copy paste solution.

i think question already asked on stack overflow please refer this link
stackoverflow
stackoverflow
The first thing that you should do is read this article at mysql.com.
Managing Hierarchical Data in MySQL
or here
i hope this reference give you your solution.

Related

create dynamic menu based on level with codeigniter

I want to make dynamic menu based on laravel. here's the important information :
table_menu :
contains field name etc. and level_id with varchar type
table_level :
contains id with int type and name of the menu
I use codeigniter and sql
so the idea is, instead of inserting data to table_menu one by one (for example if I've 5 menu and 2 level, that would be 10 menu data in total on table menu).
so I think, the best idea for this is that I just need to insert csv on field level_id in table_menu (level_id => 1(admin),4(user),5(etc). and that will give me 5 menu no matter how much level. right?
But the question HOW? is that even POSSIBLE?
I've tried like this :
$this->db->where('parent', true)->where_in('level_id', $this->session->userdata('level_id'))->get('table_menu')->result();
but It didn't work. can anyone help me?
finally i figured it out!
I read a lot about find_in_set sql query, and finally succeed. here's the final code :
$this->db->query("SELECT * FROM table_menu where FIND_IN_SET( ".$this->session->userdata('level_id').", level_id) > 0")->result();

Codeigniter - HAVING and LIKE

Related question
I have a very similar query to this question below:
Search GROUP_CONCAT using LIKE
I think my question is pretty much identical, but in CodeIgniter. Therefore I think I am just looking for the same answer but converted into active record language...
My question
To provide some background, in my case I have a many to many relationship so with three tables:
companies which has two fields (company_id and company_name)
sectors which has two fields (sector_id and text)
companies_sectors which has two fields (company_id and sector_id)
(One company can operate in multiple sectors, multiple companies operate in the same sector.)
I have grouped by company to show sectors.sector_name as a group_concat field and I have given an alias to this concatenated field at the select level:
$this->db->select('sectors.sector_id, GROUP_CONCAT(DISTINCT sectors.text SEPARATOR "; ") as sector_text', false);
I want to include a filter which selects where 'sector_text' (the group_concat field) includes the text from the query form. I understand that, because I want to run the filter on an aggregated list, I should use "having" and not "where". Per the answer to the link above, it looks like MySQL has a HAVING LIKE, but I was struggling to replicate this under CodeIgniter's active record (in fact my understanding is that CodeIgniter's ->like() is a WHERE x LIKE y which is not what I am looking for...)
At the moment I am only using a LIKE:
$this->db->like('sectors.text', $this->input->post('sector_text') );
But this filters before the grouping, which means the output will only show the sector that was searched for. For example, if Company A operates in "fishing" and "shipping" while Company B operates only in "fishing", and a user searched for "fishing", I want the result to show:
Company A - Fishing; Shipping
Company B - Fishing
(This is the desired result!)
But at the moment I am only getting:
Company A - Fishing
Company B - Fishing
... which I think is because I have used like, which filers pre-grouping?
Can someone please assist? Many thanks in advance!
PS If I can also use the alias "sector_text" instead of sector.text that would be ideal (I think I have read that "Having" allows you to use the alias?)
I found the answer! I just modified the "LIKE" to:
$sector_text = $this->input->post('sector_text');
$this->db->having("sector_text LIKE '%$sector_text%' ");
It feels a little off doing it via active record, but it works. If there is a solution that keeps it within active record then please let me know as I would probably prefer this!
Many thanks!

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.

Select items that may be related (like: for 'orange', give 'bread') from MySQL database using PHP

I have a system in which I have to select "similar" records. Imagine a database containing a big list of products and when the user enters partial name of a product, a list of products come up as suggestions about the product he is searching for. These products have a longer description field too.
This is NOT about a WHERE product_name LIKE '%entered_string%' query, I think. The logic is akin to the one Stack Overflow might use, id est: when you ask a question, it prompts you with Questions that may already have your answer and Similar questions, both obviously using a method to derive what I want to ask from my question title/content and search against the database, showing the results.
I just wonder whether it is accomplishable with PHP and using MySQL as the database.
Example:
Entering food should give us results like 1kg oranges, bread and cookies. Both of these would have something similar which could help to link them programmatically to each other.
There can lots of methods to approach this scenario. but I think straight one is to have multiple keywords/tags mapped with every item. so when user types in, you would not be searching item table, you should be searching the mapped keywords and based on that searching loading the relevant items.
If you want similar products to show up, you need to put that information in your database.
So, make a category for foods, and assign every food product to that category. That way you can select similar products easily. There is no other efficient way to do this
So your database:
categories:
|id|name
1 fruit
2 Cars
Products
|id|name|category_id
1 apple 1
2 Ford focus 2
And you can select like this:
SELECT `name`,`id` FROM `products` WHERE category_id = 1;
Another way (as suggested in a comment) are tags
Products
|id|name|tags
1 apple "fruit food delicious"
2 Ford focus "Car wheels bumper"
Best way is to use a fulltext search on the tags:
SELECT * FROM `products` WHERE MATCH(tags) AGAINST ('fruit')
Make sure to have a fulltext index on tags.

Quality questionnaire php mysql graphipcs

i'm making a questionnaire about a service quality, its contains the options (poor, regular, good, very good). It's contains 6 questions (radio button) and a suggestion box (textbox).
In the table of the database i created 6 rows for questions, 1 for suggestion and 1 for date (a friend of mine tole me to use this but i didn't get why).
q1) I'm going to atribute a value form 1 to 4 to the radio buttons options, and i'd like to sum every answer for each question, and then divide by the numbers of user that answered that question and give the mean. how am i supposed to to that? I'd also like generate reports of the month, of the year.
q2) not only about the questionnaire but for registration too. I need all the fields to be completed, no blank options, if he don't complete all of fields it'll not be submitted and there will be a warning message to the user.
q3) about the field type, i'd like it to be the same class that is in the database, i'm having a "problem". Ex: Name(varchar) : 1234(int), in the field 'name' of the table of the database 1234 will be shown as name, and i don't want this, i want only the type that i declared in the construction of the table.
q4) i'd also like to know if it's possible to create pizza graphics, about the percentage of each question, is this possible?
q5) I'm using phpmyadmin and some of my id's are auto_increment, but 'cause of my tests they at a high number, i'd like to restart to 0 the ids number, is this possible?
Thanks for the attention.
To me this sounds like what you more need is a complete surveying package. This has been done before and there are many great paid and free options.
If you are attached to a php/mysql answer, a quick search finds this as a nice open source option.
http://www.limesurvey.org/

Categories