![]() |
Paginated Flat File DB
I found this old article on this forum, about paginated txt file databases.
I've set up a site that pulls data from a single-dimensional txt file database, but at the moment it's not paginated. The text file... Code:
<!-- Comment1 --><div class="class"><a href="http://www.url.com/1" target="_blank"><img src="http://www.url.com/image1.jpg" border="0"></a></div>PHP Code:
Is there a way of using foreach() and explode() to paginate the data? I tried doing this but it's not quite working. A few of the lines become truncated... PHP Code:
|
I'm not quite sure why you're attempting to explode on a delimiter that doesn't exist. That just puts the same string that's already held in $line_of_text into the 0 index of an array. You could omit that and gain the same result in less code;
What you may want to do is use file() as opposed to the method you've employed. It reads files into an array, which you can then work off of. php Code:
You can drop the above into its own file and run it to see how it works. Then modify and incorporate as you see fit. ;) |
Thanks for this :-) Yes, this method is a big improvement. A lot simpler. This code pulls all of the data from the txt file using foreach(). However, for some reason the data is preceded by...
PHP Code:
PHP Code:
|
I would like to introduce another approach to your problem, one which you're probably not aware of so would've have considered yet. To so do, you will have to think a little out of the box and if you're new to using classes/objects then things might seem a bit weird.
Another concept in there is filtering user input using the filter extension. It just makes the common task of filtering input a bit easier by allowing us to quickly validate the input type (in this case, integers only) and optionally limit the values to a specific range (1…1000) or specify a default value if none is present (1). PHP Code:
|
Thanks for this. This looks like exactly what I'm looking for. I've copied this code and tested it on my site but it's not working. It's just not pulling anything through to the site.
Any ideas why this could be? I had a look for missing quotes and stuff but it appear to be fine. Humm.. it's a mistery |
I have tried using a query string too but it's just not working. For example:
test.html?page=1 |
Do you have error reporting/displaying set sufficiently high to see errors/warnings/notices?
At the top of your script try setting: PHP Code:
|
Not to hijack the thread but Salathe is there an approximation of when every object will be properly documented? The SPL is extremely useful and I make use of atleast one or two objects on every project now.
|
Enfernikus, see your PM inbox. :-)
|
I added the error lines and this is the error message..
Fatal error: Call to undefined function filter_input() in /vhost/*thesitepath*/www/test.html on line 12 Any ideas? (I have set my website htaccess file to allow php from within html files and it works perfectly on all of the other pages so this is not the problem.) |
That's funny, because as soon as Salathe posted that I was going to throw in a mention that the filter_* functions weren't added until 5.2.0. I've ran into this problem twice in the past little while with a few public domain projects I've released. First time was using htmlentities double_encode argument, and the second was with filter_var.
Upgrade PHP and you'll be good to go. `aptitude update | aptitude safe-upgrade` and if you're not on Debian, upgrade to that while you're at it. :D |
I'm using a shared hosting program (Pick N Mix, Easyspace.com) so I'm not in control of which version of PHP I am using.
Bit of a shame because the filter function looks really useful. Any easy ways of getting around this? |
Seeing they use PHP 5.1.6 (after a long long page load o_O)
You could ask them if they could upgrade to 5.2 (since changing your host isn't a option I guess =P) or you will have to make your own function for the filter_input() function. |
A ha, thanks for letting em know about this. I'll give them a call tomorrow. If they can't upgrade, is there a way to achieve pagination with my txt file using PHP 5.1.6?
|
The use of the filter extension bears little relation to the chunk of code which paginates the file's contents; it's just there as an easy way to get, filter, validate the provided page number.
You can always fall back on the old style: PHP Code:
|
This is fantastic. I'm getting really close to finishing this project now. I have tested this with a test file that looks a bit like this..
PHP Code:
I'm afraid, I do still have a problem though :-/. This code doesn't appear to be working with my real txt file. Maybe because it's a lot bigger, with much longer lines. Here is an example of thee lines in my txt file... PHP Code:
(Also Salathe, what does the number 1000 do in the previous reply?) |
I'm guessing the 1000 in Salathe's post means as an example for the maximum of what your page number could be.
*$_GET['page'] <= max page number and $_GET['page'] >= min page number* About all the lines always getting returned: Are you sure your text file contains multiple lines and not just 1 really big one ? Because if it is just 1 long line, the code would set the page to 1 and always get the 1 line. |
{Sorry I haven't updated this since last weekend, I can't use the internet on weekdays}
Ah right, so that's what the 1000 means. I have checked and I am sure that I do definitely have line breaks at the end of the lines. I have about 350 line approximately but I need a system that will allow for the txt file to be potentially thousands of lines long! Is this possible? I know that users have to load the whole txt file every time they view a page don't they? I think this is still going to be quicker than loading many images though. Any more feedback on this issue would be greatly appreciated. |
I've had a play around with this again today but I'm still really struggling. I would love to crack this soon. Any more advice anyone?
|
Without being able to work with the same input (the file) as you, any further help would be guess-work.
|
| All times are GMT. The time now is 03:34 AM. |
Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0