pow
Premium Member
Registered: 11th Sep 06
Location: Hazlemere, Buckinghamshire
User status: Offline
|
I am useless with php, I mostly steal code from helpful people on the internet
I have a list of data from a CSV document into a table: (Link Removed for now)
But I want to split that up so it shows 20 results per page with a next/previous button - how would I do this?
Thanks for any help in advance
[Edited on 17-04-2015 by pow]
[Edited on 18-04-2015 by pow]
|
Steve
Premium Member
Registered: 30th Mar 02
Location: Worcestershire Drives: Defender
User status: Offline
|
Easy, put the DB query limit in a variable that is set by a HTTP post and get variable containing a number that relates to the start/end you are interested in, then just dynamically generate the link for the button to contain the correct numbers.
[Edited on 17-04-2015 by Steve]
|
Steve
Premium Member
Registered: 30th Mar 02
Location: Worcestershire Drives: Defender
User status: Offline
|
so the link for the next button may look like
http://www.domain.com/cal.php?start=20&end=40
etc
|
pow
Premium Member
Registered: 11th Sep 06
Location: Hazlemere, Buckinghamshire
User status: Offline
|
It's not a DB backend though, it's a CSV - does that make a difference? I fucking hate websites!
|
ed
Member
Registered: 10th Sep 03
User status: Offline
|
Load the entire CSV into an array, then use array_slice() to get the parts of it you'd like.
i.e.
$paginated = array_slice($myCsvArray, 0, 20);
Will get you the first twenty rows of it and,
$paginated = array_slice($myCsvArray, 20, 20);
Will get you the next twenty. PHP's array handling is super quick; from first hand experience you'd need to be dealing with tens of thousands of rows before you notice a slowdown.
|
DaveyLC
Member
Registered: 8th Oct 08
Location: Berkshire
User status: Offline
|
You'd be better off getting PHP to serve up the Data as JSON then use a JQuery UI paged table plug-in to display it.
|
DaveyLC
Member
Registered: 8th Oct 08
Location: Berkshire
User status: Offline
|
e.g. http://www.jtable.org/Demo/PagingAndSorting
|
ed
Member
Registered: 10th Sep 03
User status: Offline
|
This one's better
https://github.com/edcs/jquery-bootgrid
|
pow
Premium Member
Registered: 11th Sep 06
Location: Hazlemere, Buckinghamshire
User status: Offline
|
Dave help me :cry:
|
pow
Premium Member
Registered: 11th Sep 06
Location: Hazlemere, Buckinghamshire
User status: Offline
|
Actually I'm managing to do it using a variable in the address as Steve suggested which is working nicely. You all may laugh but we gotta start somewhere!
[Edited on 17-04-2015 by pow]
|
pow
Premium Member
Registered: 11th Sep 06
Location: Hazlemere, Buckinghamshire
User status: Offline
|
I've done it
Thanks for you help everyone - Ed that array slice thing was the one
|
Dom
Member
Registered: 13th Sep 03
User status: Offline
|
quote: Originally posted by ed
Load the entire CSV into an array....
That makes me feel a little 'dirty'*
* I know, it depends on use, application etc etc
|
ed
Member
Registered: 10th Sep 03
User status: Offline
|
Yah, but PHP's IO is a bit sucky. You're stuck going through the file recursively no matter what as you can't set the file pointer manually, like you can do in other languages. Once you open the file up, the whole thing will be there in an array so you may as well slice it.
|
Dom
Member
Registered: 13th Sep 03
User status: Offline
|
quote: Originally posted by ed
Yah, but PHP's IO is a bit sucky. You're stuck going through the file recursively no matter what as you can't set the file pointer manually, like you can do in other languages. Once you open the file up, the whole thing will be there in an array so you may as well slice it.
The fseek() function springs to mind (set pointer position, read X bytes; incredibly messy approach though) but I can't think of an elegant solution for CSV files in that you open the file and only grab X many rows from Y offset.
Personally i would have just dumped the CSV into a DB table and gone from there But i know that doesn't fit every scenario.
[Edited on 19-04-2015 by Dom]
|
ed
Member
Registered: 10th Sep 03
User status: Offline
|
fseek() works using bytes and not lines; you'd need to know how many bytes each line contained to use it. There's some OO stuff built which lets you seek to a line number, but it works still works recursively to find the line in the file you want.
That's why databases can be useful
[Edited on 19-04-2015 by ed]
|
Dom
Member
Registered: 13th Sep 03
User status: Offline
|
quote: Originally posted by ed
fseek() works using bytes and not lines; you'd need to know how many bytes each line contained to use it.
Hence the 'incredibly messy approach' comment
Either way, it sounds like Pow is sorted which is the main thing
|
pow
Premium Member
Registered: 11th Sep 06
Location: Hazlemere, Buckinghamshire
User status: Offline
|
There is only maximum 300 lines in the csv, four bits of data (start time end time date and details), the csv is maximum 6kb, it loads just like any other light php page for me so I'm happy as it is. If it was loads of data I'd be doing it a little differently, however I cba to spend years on the school calendar lol
|