473,466 Members | 1,457 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Help req with arrays

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
5 3436
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: CoolPint | last post by:
I would be grateful if someone could point out if I am understanding correctly and suggest ways to improve. Sorry for the long message and I hope you will kindly bear with it. I have to make it...
4
by: Mingus Tsai | last post by:
Hello- please help with unpickling problem: I am using Python version 2.3.4 with IDLE version 1.0.3 on a Windows XPhome system. My problem is with using cPickle to deserialize my pickled...
8
by: inkexit | last post by:
I am a very amatuer c++ programmer and a somewhat accomplished composer. I am trying to write some code that creates 'self similar' melodies from a base melody the user inputs. This musical idea...
1
by: Geoff | last post by:
I was wondering if anyone could help me with a problem I am having. I am trying to read in a list of numbers from a file and then sort them using pointers and malloc() and free(). I know how to...
2
by: Pasacco | last post by:
dear I want to ask help on this problem. Array a is partitioned into a0 and a1 in main(). Then a1 is partitioned into a2 and a3 in th_partition() function. And I think this problem is something...
9
by: weidongtom | last post by:
Hi, I've written the code that follows, and I use the function add_word(), it seems to work fine *before* increase_arrays() is called that uses realloc() to allocate more memory to words. But...
2
by: Dr Dav | last post by:
Hello all, I'm a physicist whose rewriting a numerical simulation, previously written in IDL, in C with the goal reducing runtime. As you may imagine, my C programming skills are quite poor but I...
5
by: saytri | last post by:
Hi i have this project were i have to do a quiz. i wrote the questions in a textfile and i called them through java. I have also made a menu to choose which type of quiz. But before accessing the...
110
by: fjm | last post by:
For some reason, I have always had a hard time understanding arrays as they pertain to php and databases. I understand associative arrays just fine but when there are multidimensional arrays, I kinda...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.