...

View Full Version : more array problems with this rubbish book!



theexo51
07-12-2005, 12:36 PM
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!

delinear
07-12-2005, 01:04 PM
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 :thumbsup:

theexo51
07-12-2005, 02:14 PM
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

dumpfi
07-12-2005, 07:19 PM
Just by looking i found 2 errors in your script:

1.)

class Table
{
var $table_array = array();
var $headers = array();
var $cols;
} // <- there should not be a brace here

2.)

$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

Nightfire
07-12-2005, 07:43 PM
No, that's right. It should be as it was


$test ->new table (array("a","b","c")); // <- "->" should be "="


It should be:


$test ->new Table (array("a","b","c"));


As the class is called Table, not table



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum