Help req with arrays

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Dariusz

    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.d b") 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
  • Jon Kraft

    #2
    Re: Help req with arrays

    Dariusz <ng@lycaus.plus YOURSHIT.com> wrote:

    Hi Darius,
    [color=blue]
    > 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");[/color]

    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().
    [color=blue]
    > for($i = 0; $i < count($Date); $i++)
    > {
    > echo($Date[$i]."<br>");
    > }[/color]

    This doesn't do anything. $Date is just an empty array.
    [color=blue]
    > 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";[/color]

    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

    Comment

    • Dariusz

      #3
      Re: Help req with arrays

      In article <bj9k81$g9dqn$1 @ID-175424.news.uni-berlin.de>, Jon Kraft <jon@jonux.co.u k> wrote:

      Hi Jon,
      [color=blue]
      >$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>");
      >}[/color]

      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

      Comment

      • Jon Kraft

        #4
        Re: Help req with arrays

        Dariusz <ng@lycaus.plus YOURSHIT.com> wrote:
        [color=blue]
        > Jon Kraft <jon@jonux.co.u k> wrote:
        >
        > Hi Jon,
        >[color=green]
        >>$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>");
        >>}[/color]
        >
        > 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.[/color]

        First of all I strongly suggest you use a database to do this.
        [color=blue]
        > 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 !';[/color]

        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","Th is is a great site."
        "Saturday, November 24th 2001 - 02:18:44
        PM","127.0.0.1" ,"http://","Nicky","Grea t job, this is such an improvement,
        it really looks awesome ! Well done !"
        [color=blue]
        > 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.[/color]

        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

        Comment

        • Dariusz

          #5
          Re: Help req with arrays

          In article <bj9phf$go1de$1 @ID-175424.news.uni-berlin.de>, Jon Kraft <jon@jonux.co.u k> wrote:
          Hi Jon,
          [color=blue]
          >First of all I strongly suggest you use a database to do this.[/color]

          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

          Comment

          • Jon Kraft

            #6
            Re: Help req with arrays

            Dariusz <ng@lycaus.plus YOURSHIT.com> wrote:
            [color=blue]
            > Jon Kraft <jon@jonux.co.u k> wrote:[/color]
            Hi Jon,[color=blue]
            >[color=green]
            >>First of all I strongly suggest you use a database to do this.[/color]
            >
            > Any reason why you would recomend that route instead of a text file
            > approach - considering the text file would not be that large?!?[/color]

            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

            Comment

            Working...