I have a mySQL table which stores categories and subcategories nested to unlimited level. The table structure is :
cat_id, parent_id, cat_name
1 0 Ingredients
2 1 Veg
3 1 Non-veg
4 3 Egg
5 2 Potatoes
I want to show the complete list of these items in a PHP page in a tree view. So first the items that have parent id "0" will be shown and if they have child items the child elements will be shown with recursion to unlimited level. The output will be something like the following but in table format :
id title
1 Ingredients
2 Veg
5 Potatoes
3 Non-veg
4 Egg
I am able to achieve this with the help of multiple queries. First I get all the records with parent id = 0 and then I loop through all the items and inside the loop I check if there are child records present show them (recursively).
But now I need to add pagination and show only 10 records per page.
Is there a way to achieve pagination and dynamically show 10 records per page including the child and child-child records ?
How will I calculate the total number of pages and get the records ? I will also have to add filters later in these type of tables.
You mean, only 10 records of the root items, the ones that have cat_id of 0? For your first query, just do SELECT * FROM cats WHERE parent_id = 0 LIMIT 10. Then your subsequent queries will only find the descendants of that first 10.
For total number of pages (sets of 10), do SELECT COUNT(*) / 10 AS number_of_pages FROM cats WHERE parent_id = 0.
there is a fast way to get all results in an html table, and then apply jquery pagination like :
$(document).ready(function() {
$('#mytable').DataTable();
} );
site source : https://www.datatables.net/examples/basic_init/zero_configuration.html
by this the client will wait only the first time, and after this, click next is directly a client event not necessary to disturb the server.
also you php and html will be clean with a lot of functions like :
sorting, filters, search, ...
demo in jsfiddle : http://jsfiddle.net/0wvtpzc8/
Related
I have an archive page that displays the number of articles published. Because there were so many, I ran a pagination script:
for 127.0.0.1/archive/2/?p=x&pp=y
where p is the page number and pp is number of articles to display per page
The pagination looks like this
Prev 1 2 3 4 ... 12 NEXT
with each item linking to p x
I also have the items per page setter: 25 | 50 | 100 (y)
Now I have a PHP script that fixes pp into a session variable. But I am worried about duplicate content (since incrementing pp values will be inclusive) and also content not getting indexed because its not in the pagination link. so in the example above, pages 5-11 will not be indexed.
Any ideas on how to fix this?
Afternoon,
I am using a recursive function to get a list of items from a data table. But if the results are more than 2000 it seems to make 'mysql go away'.
I have split the database into 3 tables :
menu = category_id,category,parent_id
item_categories = item_id,category_id
item_info = item_id,price etc..
All the _ids are integers only.
The issue I have is there is 7000 categories at present with sub-levels up to 7 deep.
There are around 80000 items at present which can be in multiple categories.
The database is for quite-specific boiler spares department.
When a category is chosen I would like to count all items in the category and all of their sub categories as well as sort them by price. I need to get a total count for pagination, but i only wish to display about 50 results per page which i sort either by price ASC or price DESC depending on client choice.
To try to allow the page load to look nice and give it a little extra time I am loading the page then once loaded it calls the itemview page via jquery, with a loader on screen until results are returned. When 'mysql goes away' the loader just stays on screen..
I am using PDO with mysql, with PHP.
I have the ids indexed in all 3 data tables.
any guidance would be appreciated..
My terminology is somewhat lacking, so the title for my question is undoubtedly kind of lame, but I will explain what I mean below.
I have a MySQL table that looks something like the following:
categories:
category_id | parent_id
0 0
1 0
2 1
3 1
4 3
Now, what I want to do is output the category structure like this:
category structure:
0
1 -> 2
3 -> 4
In addition to needing to be able to display the category structure, if a category is selected then I want to find all of the articles in that category and in the subcategories (articles would be another table where each article would have a parent_category_id liking it to the category it's in).
The only way I can think of doing this is:
Get all categories with parent_id equal to the id of the category being viewed
Loop through all of the results and repeat step one
Just keep doing that until all of the results have been checked
Is there a better way to do this?
one way to do it in an efficient way is using nested sets.
it is a little bit tricky, and its a bit more complicated to update.
It works like that:
every node has 2 id's, and a level. all child nodes ids ar between the nodes ids.
example:
category_id | parent_id | low_id | high_id
0 0 1 2
1 0 3 10
2 1 4 5
3 1 6 9
4 3 7 8
now you can say "give me all child nodes of category 1":
select *
from categories
where low_id between 3 /* (low_id node1) */ and 10 /* (high_id node 1) */
but if you have to insert an node, you need an algorithm to move the other nodes in right position.
also it is good to store the level of the nodes, so you don't have to look for the id/parent_id relationship, you only have to sort by low_id and use the level as indicator.
there is a doctrine2 plugin to handle nested sets if you use php as programming language.
edit: i think this is a good point to start: http://mikehillyer.com/articles/managing-hierarchical-data-in-mysql/
I'm building a webpage where the items listed in the webpage are dynamic, (The items are loaded from the database.) Say we have 19 items in the database. The maximum items allowed for a page is 4. Now I have 15 items remaining. I need the rest of the items in another 4 pages.
Like this --> Pg1 -> 4 items displayed
Pg2 -> 4 items displayed
Pg3 -> 4 items displayed
Pg4 -> 3 items displayed
As shown above my php code needs to identify the number of items and the number of pages required. What is the best way you would suggest me to do this.
Get the count of items in the database.
select count(whatever_id) as numberofwhatevers from mytable;
save it (in a variable or a class or however you feel comfortable)
$recordcount = $results["numberofwhatevers"];
$recordsperpage = 4;
$numberofpages = ceil($recordcount / $recordsperpage);
The only complicated bit is querying a subset of the records to match whatever page you're on... for example, if you're on page 3, you want records 9-12.
select * from mytable limit 9,4;
I have a category table with sub-categories
Table Structure is:
ID, Category Name, Parent Category
1, A, 0
2, B, 0
3, C, 0
4, A1, 1
5, A2, 1
6, A12, 4
and so on..
I am able to display this in the form of Tree Structure. But I want to display them as Tree Structure with Paging.
Something like,
Say there are 1000 categories.
Every page shows 20 records (in tree like structure)
So, no. of pages = 50
Now, when user clicks on Page Number 2 then he should be shown records from number 21 from the Hierarchical Tree Structure.
So, what I want is that a Tree with Paging.
Please help me in how to do it.
Thanks.
First get the entries which are in the currently selected branch;
then cull them according to the offset.
(You are obviously somewhere storing the currently selected branch, and have some means of setting the position in the pages. Make an ordered list of the sub-nodes in the branch to be displayed, then get the subset between $offset and $offset+$numberOfEntriesToBeShown-1 and display these entries.)
So, according to it I have to create a hierarchical structure on every page and show only some records (acc. to paging). By this method it takes long time load.
I have 3000 records in table with Id, Name, Parent Id
I now create a tree structure on page 1 (of paging) showing first 10 records from array (of tree structure).
Then user goes on 2nd page and again a tree structure is created but records 11-20 are being shown.
So, here every time whole tree is formed first and then only a part is showing.
For 3000 records it takes time to create a complete tree every time and in future if there are more records say 10000 or even more then it will even more time.
Is there some other solution to it.
Thanks.