corsasport.co.uk
 

Corsa Sport » Message Board » Off Day » Geek Day » Need some php gods help please


New Topic

New Poll
  Subscribe | Add to Favourites

You are not logged in and may not post or reply to messages. Please log in or create a new account or mail us about fixing an existing one - register@corsasport.co.uk

There are also many more features available when you are logged in such as private messages, buddy list, location services, post search and more.


Author Need some php gods help please
pow
Premium Member

Avatar

Registered: 11th Sep 06
Location: Hazlemere, Buckinghamshire
User status: Offline
17th Apr 15 at 13:40   View Garage View User's Profile U2U Member Reply With Quote

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

Avatar

Registered: 30th Mar 02
Location: Worcestershire Drives: Defender
User status: Offline
17th Apr 15 at 14:08   View Garage View User's Profile U2U Member Reply With Quote

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

Avatar

Registered: 30th Mar 02
Location: Worcestershire Drives: Defender
User status: Offline
17th Apr 15 at 14:10   View Garage View User's Profile U2U Member Reply With Quote

so the link for the next button may look like

http://www.domain.com/cal.php?start=20&end=40

etc
pow
Premium Member

Avatar

Registered: 11th Sep 06
Location: Hazlemere, Buckinghamshire
User status: Offline
17th Apr 15 at 15:13   View Garage View User's Profile U2U Member Reply With Quote

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
17th Apr 15 at 15:44   View User's Profile U2U Member Reply With Quote

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
17th Apr 15 at 15:49   View User's Profile U2U Member Reply With Quote

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
17th Apr 15 at 15:51   View User's Profile U2U Member Reply With Quote

e.g. http://www.jtable.org/Demo/PagingAndSorting
ed
Member

Registered: 10th Sep 03
User status: Offline
17th Apr 15 at 15:53   View User's Profile U2U Member Reply With Quote

This one's better

https://github.com/edcs/jquery-bootgrid
pow
Premium Member

Avatar

Registered: 11th Sep 06
Location: Hazlemere, Buckinghamshire
User status: Offline
17th Apr 15 at 16:07   View Garage View User's Profile U2U Member Reply With Quote

Dave help me :cry:
pow
Premium Member

Avatar

Registered: 11th Sep 06
Location: Hazlemere, Buckinghamshire
User status: Offline
17th Apr 15 at 16:26   View Garage View User's Profile U2U Member Reply With Quote

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

Avatar

Registered: 11th Sep 06
Location: Hazlemere, Buckinghamshire
User status: Offline
18th Apr 15 at 10:41   View Garage View User's Profile U2U Member Reply With Quote

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
19th Apr 15 at 18:59   View User's Profile U2U Member Reply With Quote

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
19th Apr 15 at 20:31   View User's Profile U2U Member Reply With Quote

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
19th Apr 15 at 20:50   View User's Profile U2U Member Reply With Quote

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
19th Apr 15 at 21:05   View User's Profile U2U Member Reply With Quote

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
19th Apr 15 at 22:18   View User's Profile U2U Member Reply With Quote

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

Avatar

Registered: 11th Sep 06
Location: Hazlemere, Buckinghamshire
User status: Offline
20th Apr 15 at 11:46   View Garage View User's Profile U2U Member Reply With Quote

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

 
New Topic

New Poll

  Related Threads Author Forum Replies Views Last Post
Chat up lines Bonus General Chat 11 1019
4th Apr 03 at 17:32
by Rob_Scarborough
 
My car on fast car! Ellie General Chat 146 3670
22nd Mar 06 at 21:09
by vauxcorsagirl
 
Tesco disabled parking [Pic Added - Page 6] Whittie General Chat 142 4456
29th Apr 07 at 17:19
by Ian S
 
Mod Gods @ Donny Sept 27th & 28th Glenda Yorkshire 9 733
21st Aug 08 at 15:36
by Glenda
 
Old advrnture games. dan_m1les Geek Day 21 650
25th Aug 10 at 13:23
by Cavey
 

Corsa Sport » Message Board » Off Day » Geek Day » Need some php gods help please 28 database queries in 0.0129118 seconds