PHP Tree Paging - php

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.

Related

How to import data from excel file and identify subcategory

I am making the function of importing data from the excel file with the following structure
i want loop the datas of excel then insert to categories table and determined subcategory by value of first column. Eg: 101 is sub category of 1, 101001 is sub of 101,...
I have been thinking for a long time and looking for it but not yet. Hope the helping.
Use PHP Spreadsheet https://phpspreadsheet.readthedocs.io/en/latest/
Reading from a file: https://phpspreadsheet.readthedocs.io/en/latest/topics/reading-files/#reading-files
However you parse the cat/subcat, you would want to insert it back into the table with the database ID of the parent.
To determine the parent, start with the first row, and keep it in a parents array. When you hit the next row, check the parents array if the value exists. You will have to check char by char. Then add every new category to the parents. This structure is not really ideal, what happens when PHP becomes a parent, and so on. PHP should be 10101 at any rate to follow the structure. Ruby 10102
In all honesty, you don't need second half of the number, only the parent. Just use the row id.
1, , Programming
2,1,OOP
3,2,PHP
What you are missing here is the row id. The last part is only the "sort" and can just be removed. Imagine if the column was a fixed length field which would be "easier" to parse. 3 digit for parent, and 3 digits for the sort. Start parsing from the top ..
000001 Programming
001001 OOP
What is PHP now? You aren't pointing to 001 (OOP) cause 001 is also Programming. So adding a row id, you can now point to the id that will be entered into the DB.
1 null 1 Programming # id parent sort name
2 1 1 OOP
3 2 1 PHP
4 2 1 Ruby
5 1 2 Functional

Pagination with unlimited nesting

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/

PDO/Mysql PHP Recursive category Tree with showing all items

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..

PHP: Generate n No of pages according to the n No of items in the database

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;

MySQL query result split

This is somewhat of a multipart question, but..
I am looking to query a MySQL table to get fields from a event category table.
Each category has a specific calendar assigned to it, in the "calendar" field in the category table.
I am planning to have a HTML list box for each of the different types of calendars (only 4, and they wont change).
Is there a way to query the category table once, and split the results into different arrays?
Ex.
Sports (only categories assigned to the sports calendar appear here):
(in list box):
Basketball
Baseball
Golf
etc.
then,
General:
(only categories assigned to the general calendar appear here)
etc.
etc.
etc.
I thought to do this in one query, instead of querying the whole table for each calendar type, but will there be that much difference in speed?
I am using PHP, by the way.
Thanks for the help.
You can query the table once and use mysql_data_seek to reset the rowset pointer back to the beginning after having read through it - i.e. iterate over the rowset for category 1, reset the pointer, iterate over for category 2, etc. You need only query once, and iterating over the results is very fast vs. querying.
Alternatively, have four strings each containing the HTML for the content of one of the listboxes, and iterate over the rowset once, appending to the relevant string based on the category of the current record.

Categories