By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,665 Members | 2,334 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,665 IT Pros & Developers. It's quick & easy.

Help req with arrays

P: n/a
I want to use arrays in my website (flat file for a guestbook), but despite
having read through countless online tutorials on the topic, I just can't
get my code to work.

I know there are guestbook scripts out there - but that doesn't help me
learn how to programme arrays !!!

The following is the code for the PHP (called externally), which does
execute...
<?php
// Setup of guestbook data to open and read from

$filename = "test.db";
$fh = fopen ($filename, "r") or die("Could not open guestbook file at this
time");

for($i = 0; $i < count($Date); $i++)
{
echo($Date[$i]."<br>");
}

echo('Number of arrays:'.count($Date).'<BR>');
echo('Size of array:'.sizeof($Date).'<BR>');

fclose($fh);

?>

The last two echo statements are to prove to me that the script is actually
running. The script is supposed to access the following file (test.db)
which contains the data....

$Date[0] = "Friday, March 14th 2003 - 04:28:45 AM";
$Date[1] = "Saturday, November 24th 2001 - 11:24:30 AM";

It doesn't matter if I leave it as $Date[] in the db file, the problem is
the same.

When the PHP script executes in the webpage, the two echo statements that
prove the script executed echos that the number of arrays is zero, and the
size of the array is also zero. I cannot get it to print out the contents
of the arrays into the webpage as I want it too.

It was suggested to me to use an include("test.db") statement instead of
the whole fopen() fclose() stuff, but that jsut outputs the entire contents
of the file and performs no array checking (the file would have other
different arrays in it).

I can't understand why it is not working, after all it's very simple code.
Count how many $Date[] arrays there are in total in the file, then find the
first $Date[] array, print it to screen, then increment the count and
repeat the process to the next one until it gets to the last one. It
should work, but it is not.

Anyone help, it's really perplexing me.

Dariusz
Jul 16 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
Dariusz <ng@lycaus.plusYOURSHIT.com> wrote:

Hi Darius,
I want to use arrays in my website (flat file for a guestbook), but
despite having read through countless online tutorials on the topic, I
just can't get my code to work.

I know there are guestbook scripts out there - but that doesn't help me
learn how to programme arrays !!!

The following is the code for the PHP (called externally), which does
execute...

<?php
// Setup of guestbook data to open and read from

$filename = "test.db";
$fh = fopen ($filename, "r") or die("Could not open guestbook file at this
time");
You are opening the file with fopen, which returns a file handle you can use
to read the contents of the file with fgets() or fgetcsv().
for($i = 0; $i < count($Date); $i++)
{
echo($Date[$i]."<br>");
}
This doesn't do anything. $Date is just an empty array.
The last two echo statements are to prove to me that the script is
actually running. The script is supposed to access the following file
(test.db) which contains the data....

$Date[0] = "Friday, March 14th 2003 - 04:28:45 AM";
$Date[1] = "Saturday, November 24th 2001 - 11:24:30 AM";


My suggestion is:

Have the entries in your test.db as follows:

Friday, March 14th 2003 - 04:28:45 AM
Saturday, November 24th 2001 - 11:24:30 AM
Then in php:

$filename = "test.db";
$Date = file($filename) or die("Could not open ".$filename." at this time");

for($i = 0; $i < count($Date); $i++)
{
echo($Date[$i]."<br>");
}

HTH;
JOn
Jul 16 '05 #2

P: n/a
In article <bj************@ID-175424.news.uni-berlin.de>, Jon Kraft <jo*@jonux.co.uk> wrote:

Hi Jon,
$filename = "test.db";
$Date = file($filename) or die("Could not open ".$filename." at this time");

for($i = 0; $i < count($Date); $i++)
{
echo($Date[$i]."<br>");
}


The code you wrote does work, but I want to extend the test.db
file.

What I wanted to try is to assign each entry their own array (because
sometime in the future I'll to further processing to individual arrays and
not just print them to screen).

I also want the ability in the future to delete spam entries from the
"guestbook", but if the guestbook has been written to a number of times
since the spam entry - it would be easier to delete a numbered array.
Anyway, right now I'm just trying to read the entiries, I can try working
the rest out myself later.

So the test.db file would look something like:

$Date[0] = 'Saturday, November 24th 2001 - 11:24:30 AM';
$IP[0] = ''127.0.0.1';
$Website[0] = 'http://';
$Name[0] = 'Bob'
$Comment[0] = 'This is a great site.';
$Date[1] = 'Saturday, November 24th 2001 - 02:18:44 PM';
$IP[1] = '127.0.0.1';
$Website[1] = 'http://';
$Name[1] = 'Nicky';
$Comment[1] = 'Great job, this is such an improvement, it really looks
awesome ! Well done !';

So this would require a loop where it would check the number of the array
and print it out before incrementing to the next array number, a-la..

find out how many arrays there are in total
echo Date[];
echo 'IP logged';
echo Website[];
echo Name[];
echo comment[];
repeat until all arrarys are printed.

Thanks.

Dariusz
Jul 16 '05 #3

P: n/a
Dariusz <ng@lycaus.plusYOURSHIT.com> wrote:
Jon Kraft <jo*@jonux.co.uk> wrote:

Hi Jon,
$filename = "test.db";
$Date = file($filename) or die("Could not open ".$filename." at this
time");

for($i = 0; $i < count($Date); $i++)
{
echo($Date[$i]."<br>");
}
The code you wrote does work, but I want to extend the test.db
file.

What I wanted to try is to assign each entry their own array (because
sometime in the future I'll to further processing to individual arrays and
not just print them to screen).

I also want the ability in the future to delete spam entries from the
"guestbook", but if the guestbook has been written to a number of times
since the spam entry - it would be easier to delete a numbered array.
Anyway, right now I'm just trying to read the entiries, I can try working
the rest out myself later.


First of all I strongly suggest you use a database to do this.
So the test.db file would look something like:

$Date[0] = 'Saturday, November 24th 2001 - 11:24:30 AM';
$IP[0] = ''127.0.0.1';
$Website[0] = 'http://';
$Name[0] = 'Bob'
$Comment[0] = 'This is a great site.';
$Date[1] = 'Saturday, November 24th 2001 - 02:18:44 PM';
$IP[1] = '127.0.0.1';
$Website[1] = 'http://';
$Name[1] = 'Nicky';
$Comment[1] = 'Great job, this is such an improvement, it really looks
awesome ! Well done !';
In my opinion this is the wrong approach. Better to store every record in
it's own line (csv format), e.g.:
"Saturday, November 24th 2001 - 11:24:30
AM","127.0.0.1","http://","Bob","This is a great site."
"Saturday, November 24th 2001 - 02:18:44
PM","127.0.0.1","http://","Nicky","Great job, this is such an improvement,
it really looks awesome ! Well done !"
So this would require a loop where it would check the number of the array
and print it out before incrementing to the next array number, a-la..

find out how many arrays there are in total
echo Date[];
echo 'IP logged';
echo Website[];
echo Name[];
echo comment[];
repeat until all arrarys are printed.


Now you fopen the file and fgetcsv() each line - which produces an array,
e.g.:

$FP = fopen("test.db") or die ("Couldn't open test.db");

$totalRecords = 0;

while (list($Date, $IP, $URL, $Name, $Comment) = fgetcsv($FP, 2048)){
echo $Date."<br>";
echo $IP."<br>";
// etc..
$totalRecords++;
}

echo "Total records: ".$totalRecords."<br>";

fclose($FP);

HTH;
JOn
Jul 16 '05 #4

P: n/a
In article <bj************@ID-175424.news.uni-berlin.de>, Jon Kraft <jo*@jonux.co.uk> wrote:
Hi Jon,
First of all I strongly suggest you use a database to do this.


Any reason why you would recomend that route instead of a text file
approach - considering the text file would not be that large?!?

I tried your code, it works. Thanks for your help.

Dariusz
Jul 16 '05 #5

P: n/a
Dariusz <ng@lycaus.plusYOURSHIT.com> wrote:
Jon Kraft <jo*@jonux.co.uk> wrote: Hi Jon,
First of all I strongly suggest you use a database to do this.


Any reason why you would recomend that route instead of a text file
approach - considering the text file would not be that large?!?


Because only one request at a time can write to that textfile (I presume you
have a script that adds entries to that textfile). A database is much
easier to handle and much faster. It's much easier to add, edit or delete
single entries.

HTH;
JOn
Jul 16 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.