Forum Moderators: coopster
I had an account here a few years ago. I forgot my username/password. Oh well, i just made a new account. Anyway, I am having a problem with a php class based script that i am working on that reads a flat text file database and pulls information from the text based database.
Here is the code....
class Inventory
{
var $File;
function TotalNumberOfBooks()
{
$Contents = file_get_contents($this->File);
$Contents = str_replace("\nBOOK¦","\n{!@#}\nBOOK¦",$Contents);
$Books = explode("{!@#}\n",$Contents);
$NumberOfBooks = count($Books);
return $NumberOfBooks;
}
function GetBookValue($BookId, $DetailName) That is the class, and here is the file that people actually would execute (or navigate to on the browser) include "admin_class.php"; And here is the text based database <snip> SO WHAT it does, is when you execute this <snip> The first line is the total number of books that are inside the text based database and then the rest are the book numbers pulled from the text based database. It uses a for loop, and the thing that I don't understand is why every time you refresh the page, the total number of printed books changes (refresh it for yourself and see). This is really really confusing me, for loops aren't that hard, and I must be overseeing something or misusing a function. If anyone could help me out, that would be awesome! Thanks! [1][edited by: dreamcatcher at 6:16 pm (utc) on May 17, 2008]
{
$Contents = file_get_contents($this->File);
$Contents = str_replace("\nBOOK¦","\n{!@#}\nBOOK¦",$Contents);
$Books = explode("{!@#}\n",$Contents);
$NumberOfBooks = count($Books);
if($BookId == 0)
{
$ForK = 0;
}
else
{
$ForK = $BookId-1;
}
for($K=$ForK; $K<=$BookId; $K++)
{
$Book = $Books[$K];
$BookDetails = explode("\n",$Book);
$NumberOfDetails = count($BookDetails);
for($J=0; $J<($NumberOfDetails-1);$J++)
{
$Property = explode("¦",$BookDetails[$J]);
$TAG = $Property[0];
$VALUE = $Property;
if($TAG == $DetailName)
{
return $VALUE;
}
}
}
}
}
$Book = new Inventory;
$Book->File = "/#*$!XX/#*$!XX/#*$!X/textdb.txt";
// DETAIL Detail Name
/*
1 BOOK¦1339B
2TRAN¦A
3 ANAM¦Squire, J. C.
4 TNAM¦Poems in One Volume
5 PBLS¦Wm. Heinemann Ltd.
6 DESC¦1st ed. [xi] 244 pp. "This collection contains all Mr. Squire's published poems which he wishes to reprint, and a considerable number of new poems which have appeared only in periodicals or have not previously been printed at all. The earliest verses included date from 1905, the latest from 1925." Light foxing, otherwise clean and tight. Dustjacket is edge worn, with spine sunned.
7 PRIC¦10
8JACK¦J
9BIND¦H
10EDTN¦F
11UPDT¦1/30/2008
12UPTM¦3:19:25 PM
13BCAT¦Poetry
14ABLE¦1
15UBID¦6
16EDNT¦First Edition
17JCKC¦GVG
18BNDC¦Hard Cover
19PBPL¦London
20PBYR¦1926
21BOOC¦Very Good+
22STAT¦For Sale
23PPRC¦0
24PDSC¦1 of 3; 2 in UK; G@7.89; VG/G @13.80 5/18/05 price lowered from $18.00/from 12.50
25LOCA¦Back Room
26BOOE¦
27BOOS¦
*/
$Total = $Book->TotalNumberOfBooks();
echo $Total;
echo "<br>";
for($i=0; $i<=$Total; $i++)
{
echo $i .". ". $Book->GetBookValue($i,"BOOK");
echo "<br>";
}
Mike
[edit reason] No urls please! [/edit]
Did you scroll down to the bottom of the page? It's the number that I am questioning...
0.
1.
2.
...
503.
^^That should be 2117 at the very bottom of the page.
It changes every time you refresh- or it does for me. At the very top, the 2117 does not change for me. Maybe I wasn't clear on what the problem was.
Thanks for anyone who may be able to help me!
Mike
Did you scroll down to the bottom of the page? It's the number that I am questioning...
Ah, no sorry didn't scroll to the bottom. I've just tried your example again (having dug it out of my browser history since it's been <snipped> above) but it now seems to do something different to before (unless I've gotten the wrong page)? Alas, a problem with example pages and one reason why it's been removed from your initial post I'm afraid - they are not examples for very long.
Although why any 'static' script should produce different results upon refreshing the page is indeed a puzzle. It should be the same, unless there was some kind of caching issue (which doesn't seem likely - try Ctrl+F5) or the content hadn't fully loaded in the browser or the server is playing silly buggers?!
I have toyed with the code a lot tonight and nothing seems to be working.
For the categories... this is my class
function CountBooksInCategory($InputCategory)
{
$Categories = array();
$Contents = str_replace("\nBOOK¦","\n{!@#}\nBOOK¦", $this->FILE);
$Books = explode("{!@#}\n",$Contents);
$NumberOfBooks = count($Books);
$TOTAL = 0;
for($K=1; $K<=$NumberOfBooks; $K++)
{
$Book = $Books[$K];
$BookDetails = explode("\n",$Book);
foreach($BookDetails as $Val)
{
$Property = explode("¦",$Val);
$TAG = $Property[0];
$VALUE = $Property[1];
if(($TAG == "BCAT") && ($VALUE == $InputCategory))
{
$TOTAL = $TOTAL + 1;
}
}
}
return $TOTAL;
}
and this is how it is being used:
foreach($Book->GetCategories() as $Val)
{
echo $Val ." (". $Book->CountBooksInCategory($Val) .") <br>";
}
The GetCategories() function is here
function GetCategories()
{
$Categories = array();
$Contents = str_replace("\nBOOK¦","\n{!@#}\nBOOK¦", $this->FILE);
$Books = explode("{!@#}\n",$Contents);
$NumberOfBooks = count($Books);
for($K=1; $K<=$NumberOfBooks; $K++)
{
$Book = $Books[$K];
$BookDetails = explode("\n",$Book);
foreach($BookDetails as $Key => $Val)
{
$Property = explode("¦",$Val);
$TAG = $Property[0];
$VALUE = $Property[1];
if($TAG == "BCAT")
{
if(!in_array($VALUE, $Categories))
{
array_push($Categories, $VALUE);
}
}
}
}
sort($Categories);
return $Categories;
}
and the $this->FILE is just a file_get_contents("textdb,txt");
The textdb.txt is 0777 permissions and 1.34MB.
----
What I did to the code was this:
1 I threw in print_r(ARRAY) items randomly throughout each function
EVERYTHING PRINTS OUT piece by piece EXCEPT when I have the totals here
if(($TAG == "BCAT") && ($VALUE == $InputCategory))
{
$TOTAL = $TOTAL + 1;
}
If I add an
echo $TAG ."¦". $VALUE ."<br>" immediately above the $TOTAL = $TOTAL +1; it will print out every category name up to the part where it dies. If I go back one nested loop to
for($K=1; $K<=$NumberOfBooks; $K++)
{
It will print out all of the books in the database; up to BookNumber 4265 (there are only 2117 entries though, some of the BookNumbers skip around)
When I move onto the next for loop
foreach($BookDetails as $Val)
{
And place a print_r it will print out everything that I need, EVERY book EVERY VALUE.
So I know where its dying, its dying at an IF statement. But why!?!?! For the longest time it was printing all of the categories out and just putting 0 in for the totals. like this
CAT NAME (0)
CAT 2 NAME (0)
Then when I fixed the if/else statement to
if(($TAG == "BCAT") && ($VALUE == $InputCategory))
{
$TOTAL = $TOTAL + 1;
}
Then it started printing out the value but this is where it randomly dies. I am so lost and confused. I think there is a problem with $TOTAL = $TOTAL+1;
OH AND BTW I tried $TOTAL++; that just prints 0 for every category
Thanks so much to anyone who could help me out on this!
Michael