Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    Regular Coder
    Join Date
    Jul 2005
    Location
    Guildford, UK
    Posts
    109
    Thanks
    0
    Thanked 0 Times in 0 Posts

    more array problems with this rubbish book!

    ok, so heres my code;
    <html>
    <head>
    </head>
    <body>
    <?php

    class Table
    {
    var $table_array = array();
    var $headers = array();
    var $cols;
    }
    function Table ($headers)
    {
    $this->headers = $headers;
    $this->cols = count($headers);
    }
    function addRow($row)
    {
    if (count($row)!=$this->cols)
    return false;
    array_push($this->table_array,$row);
    return true;
    }
    function addRowAssocArray($row_assoc)
    {
    $row=array();
    foreach ($this->headers as $header)
    {
    if (! isset($row_assoc[$header]))
    $row_assoc[$header]="";
    $row[]=$row_assoc[$header];
    }
    array_push($this->table_array, $row);
    return true;
    }
    function output()
    {
    print "<pre>";
    foreach ($this->headers as $header)
    print "<B>$header</B>";
    print "\n";
    foreach ($this->table_array as $y)
    {
    foreach ($y as $xcell)
    print "$xcell";
    print "\n";
    }
    print "</pre>";
    }
    }
    $test ->new table (array("a","b","c"));
    $test ->addRow(array(1,2,3));
    $test ->addRow(array(4,5,6));
    $test ->addRowAssocArray(array(b=>0,a=>6,c=>3));
    $test ->output();
    ?>

    the error is on line 51, a parsing error

    PS: how do i put my code in here in a funky scroll box?
    PS: i am not using this book anymore!

  • #2
    Regular Coder
    Join Date
    Feb 2005
    Location
    West Midlands, UK
    Posts
    623
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I've not tested it but it looks like you have one too many closing braces - if you remove the last } that appears just before the line $test ->new table (array("a","b","c")); I think that'll get rid of your parse error.

    To put your code in blocks, either put an opening [ php ] tag at the beginning and a closing [ /php ] tag at the end (I've left spaces so this will show up for you, but you should type the tags without any spaces). Alternatively just select your code block with the mouse and click the little php icon on the message task bar, it should add the tags for you

  • #3
    Regular Coder
    Join Date
    Jul 2005
    Location
    Guildford, UK
    Posts
    109
    Thanks
    0
    Thanked 0 Times in 0 Posts
    yup, figured it, cheers!

    tried removing the } but didnt do much, i have counted em and it seems like it should be there, or somewhere anyway. nevermind, i am going to try and use a different book to learn from, that one was rubbish

  • #4
    Regular Coder
    Join Date
    Jun 2004
    Posts
    565
    Thanks
    0
    Thanked 18 Times in 18 Posts
    Just by looking i found 2 errors in your script:

    1.)
    PHP Code:
    class Table
    {
    var 
    $table_array = array();
    var 
    $headers = array();
    var 
    $cols;
    // <- there should not be a brace here 
    2.)
    PHP Code:
    $test ->new table (array("a","b","c")); // <- "->" should be "=" 
    Oh, and your script uses many deprecated language features like "Table()" instead of "__construct()" or "var" instead of "public/protected/private".

    dumpfi

  • #5
    Senior Coder Nightfire's Avatar
    Join Date
    Jun 2002
    Posts
    4,266
    Thanks
    6
    Thanked 48 Times in 48 Posts
    No, that's right. It should be as it was
    PHP Code:
    $test ->new table (array("a","b","c")); // <- "->" should be "=" 
    It should be:
    PHP Code:
    $test ->new Table (array("a","b","c")); 
    As the class is called Table, not table


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •