Count # Of Entries In Flat File Database?
10-23-2009, 04:40 PM
|
Count # Of Entries In Flat File Database?
|
Posts: 920
Name: Scott Kaye
Location: Ontario
|
Hello Webmasters,
I know about strlen, and how it counts the number of characters in a string, but how can I get it to count only a certain character in a flat file database?
I need it to open the database, count how many delimiters (|) there are, and then echo the last two numbers to the page. For example, if there are 20 entries in the database, the script will simply echo the numbers 19 and 20.
However, I have no idea on how to do this. Any help at all is more than I have now!
Thanks! 
|
|
|
|
10-23-2009, 04:47 PM
|
Re: Count # Of Entries In Flat File Database?
|
Posts: 2,918
Name: Keith Marshall
Location: Connecticut
|
$count = preg_match_all('|', $string);
__________________
<mgraphic /> - I don't have a solution but I admire the problem.
|
|
|
|
10-23-2009, 04:49 PM
|
Re: Count # Of Entries In Flat File Database?
|
Posts: 920
Name: Scott Kaye
Location: Ontario
|
Thanks!
I actually just found another, better way to do this.
PHP Code:
$countdelims = file_get_contents("ffdatabase.txt"); echo strlen($countdelims); //echoes how many characters there are in all in the file echo "<br />"; echo substr_count($countdelims, '|'); //echoes how many |s there are.
|
|
|
|
10-24-2009, 04:46 AM
|
Re: Count # Of Entries In Flat File Database?
|
Posts: 206
Name: vikas
|
preg_match_all involves single step execution whereas the solution provided by you is two steps ... any way both will work
__________________
Please login or register to view this content. Registration is FREE Collection of free online books and free ebooks Please login or register to view this content. Registration is FREE - Free online pdf books and free pdf eBooks
|
|
|
|
10-24-2009, 07:47 AM
|
Re: Count # Of Entries In Flat File Database?
|
Posts: 920
Name: Scott Kaye
Location: Ontario
|
Ohhh, you mean the first part where you echo the complete number. That's not needed, I just put it there to see what's happening.
|
|
|
|
10-24-2009, 11:43 AM
|
Re: Count # Of Entries In Flat File Database?
|
Posts: 807
Name: Mattias Nordahl
Location: Sweden
|
With your solution, basically you go through the whole file to count how many characters there are, then go through it again to take out the wanted inforamtion. With Keith's solution you only go through the file once.
Unless your file is VERY big it won't make any difference, performance wise. But, depending on who you ask of course, it may be better to have one line of code than to have two lines of code :P
__________________
Your answers will only be as good as your question. Formulate it well and give all the necessary information.
|
|
|
|
10-24-2009, 01:29 PM
|
Re: Count # Of Entries In Flat File Database?
|
Posts: 920
Name: Scott Kaye
Location: Ontario
|
My text file will be HUGE. Gigantic. Maybe I'll use his solution after all
Thanks, lizciz to the rescue! I've thanked you in my special thanks section of my program!
|
|
|
|
10-24-2009, 02:23 PM
|
Re: Count # Of Entries In Flat File Database?
|
Posts: 920
Name: Scott Kaye
Location: Ontario
|
Wait, no, I have it as this:
$delimnum = substr_count($countdelims, '|');
Which is similar to mrgraphic's solution. Will that ^^ work?
|
|
|
|
10-24-2009, 03:16 PM
|
Re: Count # Of Entries In Flat File Database?
|
Posts: 2,918
Name: Keith Marshall
Location: Connecticut
|
Quote:
Originally Posted by Physicsguy
Will that ^^ work?
|
Try it! 
__________________
<mgraphic /> - I don't have a solution but I admire the problem.
|
|
|
|
10-24-2009, 08:32 PM
|
Re: Count # Of Entries In Flat File Database?
|
Posts: 807
Name: Mattias Nordahl
Location: Sweden
|
Quote:
Originally Posted by Physicsguy
Wait, no, I have it as this:
$delimnum = substr_count($countdelims, '|');
Which is similar to mrgraphic's solution. Will that ^^ work?
|
Probably 
mgraphic just got the parameters swapped, it should be
substr_count($your_string, $search);
There are also two optional parameters, check php.net.
__________________
Your answers will only be as good as your question. Formulate it well and give all the necessary information.
|
|
|
|
11-29-2009, 01:31 PM
|
Re: Count # Of Entries In Flat File Database?
|
Posts: 4
Name: mark
|
Hello!
This discussion has been very useful to me. However I still have one question. How do I take it a step further and paginate the data. Here is the code I am working with. I am trying to make a tiny blog script and only show X amount of posts per page. Any tips would be hugely appreciated!
<?
$mode = 0;
if ($mode == 0) { $opFile = "blogfile.txt"; }
$fp = fopen($opFile,"r") or die("Error Reading File");
$data = fread($fp, filesize($opFile));
fclose($fp);
$line = explode("\n", $data);
$i=count($line);
for ($n=0 ; $n < $i-1 ; $n++ ) {
$blog = explode("|", $line[$n]);
if (isset($blog[0]))
{
echo "<h1>$blog[0]</h1>";
echo "<p>$blog[1]</p>";
echo "Posted by : " .$blog[2]."<br>";
echo "Date : " .$blog[3]."<br>";
echo "<br><br>";
}
}
$countdelims = file_get_contents("blogfile.txt");
echo substr_count($countdelims, '|');
?>
Thanks in advance for any help you can give 
|
|
|
|
11-29-2009, 02:46 PM
|
Re: Count # Of Entries In Flat File Database?
|
Posts: 3,621
Name: Thierry
Location: I'm the uber Spaminator !
|
Why do you use a flat file for a blog ?
A db is a natural fit for this, and even a sqlite db file would be more than enough.
Keeping your blog in a flat file will only complicate things.
__________________
Only a biker knows why a dog sticks his head out the window.
|
|
|
|
11-29-2009, 05:47 PM
|
Re: Count # Of Entries In Flat File Database?
|
Posts: 4
Name: mark
|
Obviously a DB would be optimal but... Its part of a package that needs to be database free. So it can be easily dropped onto clients websites who don't always have access to mysql. Its sometimes hard enough to get FTP info from them let alone full admin access. Hence, I am trying to make a few tiny, DB free utilities.
Also, I am not really familiar with SQLite and I have the code working fine except for pagination. Its for VERY simple blogs on mom and pop stores or small non-profit org sites.
Last edited by markf77; 11-29-2009 at 05:54 PM..
|
|
|
|
11-29-2009, 07:12 PM
|
Re: Count # Of Entries In Flat File Database?
|
Posts: 3,621
Name: Thierry
Location: I'm the uber Spaminator !
|
I was not thinking about mysql only...
Did you know that PHP embeds a file based database system: sqlite
http://www.php.net/manual/en/book.sqlite.php
It's enabled by default in every PHP5 installation.
So, except if erver manager deactivate it (which I don't see why, but who knows) you have a full db engine at disposal, who can run from a file.
But, if I look at your question, I'd recommand you to not make the file a simple ascii file, but a serialized PHP object.
http://www.php.net/manual/en/function.serialize.php
You could have a excessively simple structure like:
PHP Code:
class blog{ public $posts=array("post 1","post 2","post 3","post 4","chiggi chiggi wa wa"); }
If you have this serialized in a file, then your pagination would only be to fetch $blog->posts between x and y.
Each time you modify something in the file, re-serialize it to the disk.
The only thing to remember, is that you need to have the class you serialized defined BEFORE loading the serialized object, otherwise you end up with an "anonymous" class.
What do you think of this ?
__________________
Only a biker knows why a dog sticks his head out the window.
Last edited by tripy; 11-29-2009 at 07:13 PM..
|
|
|
|
11-30-2009, 10:26 AM
|
Re: Count # Of Entries In Flat File Database?
|
Posts: 4
Name: mark
|
I did not know that SQLite is built into php 5. I will have to play around with that. Thanks for the info.
|
|
|
|
11-30-2009, 07:14 PM
|
Re: Count # Of Entries In Flat File Database?
|
Posts: 920
Name: Scott Kaye
Location: Ontario
|
I'll try to edit your code to do what you want without SQLite, but I don't make no promises
OK, it certainly doesn't work, but it's all I can do tonight, I have a project
PHP Code:
<?php $PageName = "blogtest.php"; //Enter what the file is called that this script goes into. $DelimNum = "7"; //A weird number to see how it works. $PostCount = $DelimNum - ($DelimNum - 1); //Using Order Of Operations, we set PostCount as the number of delimiters minus the number of the delimiters minus 1, easier to understand if you know OOO, which, hopefully, you do ;) - Just so it doesn't start at 0.
if (isset($_GET["bpn"])) { $ViewNum = $_GET["bpn"] + 10; } else { $ViewNum = 10; }
$PostsPerPage = $ViewNum; //Enter how many posts you want per page
while ($PostCount <= $PostsPerPage): echo "<h3>Post #$PostCount:</h3><p>Post content</p>"; $PostCount++; endwhile;
if ($DelimNum > $PostsPerPage) { $ExtraDelims = $DelimNum - $PostsPerPage; //In this example, this is 13. //echo "<p>There are $ExtraDelims more posts not viewed yet.</p>"; //Uncomment this line to see how many more posts there are that aren't displaying. $NewBNP = $ViewNum + 10; echo "ViewNum = $ViewNum"; echo "<p>You are currently viewing the $ViewNum most recent posts. <a href='$PageName?bpn=$NewBNP'>View More...</a></p>"; }
?>
At least it's a start, I made it from scratch... 
Last edited by Physicsguy; 11-30-2009 at 07:55 PM..
|
|
|
|
12-01-2009, 02:23 PM
|
Re: Count # Of Entries In Flat File Database?
|
Posts: 4
Name: mark
|
Physicsguy, thanks for that  Unfortunately, it does not seem to work. I will play around with it and see if i can get it to work somehow.
I have given up on sqlite... I have spent quite a bit of time trying to find some decent tutorials on how to use it with php and gotten nowhere. I also looked for good book on the subject, but there is only about 2 books written and they are either very outdated or got poor reviews.
|
|
|
|
12-01-2009, 03:16 PM
|
Re: Count # Of Entries In Flat File Database?
|
Posts: 920
Name: Scott Kaye
Location: Ontario
|
Youre welcome! Thank you!
If you do get this to work, can you please tell me? I'm making the exact same thing for my cms... Thanks :$
|
|
|
|
12-01-2009, 03:38 PM
|
Re: Count # Of Entries In Flat File Database?
|
Posts: 3,621
Name: Thierry
Location: I'm the uber Spaminator !
|
Quote:
|
I have given up on sqlite... I have spent quite a bit of time trying to find some decent tutorials on how to use it with php and gotten nowhere. I also looked for good book on the subject, but there is only about 2 books written and they are either very outdated or got poor reviews.
|
sqlite is pretty simple to use:
PHP Code:
<?php //open the db $db = sqlite_open($_SERVER['DOCUMENT_ROOT']."/../db/mysqlitedb.sqlite", 0666, $sqliteerror); //make a select $q="Select val1, val2, val3 from table1 where idx='whatever'"; $res=sqlite_query($q); while($o=sqlite_fetch_object($res)){ print "the value of val1 is {$o->val1}<br/>"; } //make an insert $q="insert into tbl (val) values ('some')"; $res=sqlite_query($q); $lastId=sqlite_lat_insert_rowid();
I don't see where the difficulty is.
It's like mysql, but with the functions being prefixed sqlite_something
__________________
Only a biker knows why a dog sticks his head out the window.
Last edited by tripy; 12-01-2009 at 03:42 PM..
|
|
|
|
|
« Reply to Count # Of Entries In Flat File Database?
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|