1

Disclaimer: I'm new to object-oriented PHP and I'm generally terrible at the language.

So I've got a class called "Page" in a file (class_lib.php) and I've created this variable and written a while/for loop that processes it when its set:

public $extra= "";
if (isset($this->extra)) {
echo "<table id=\"tab\">";
while (list($counter) = each($this->$extra)) {
    for ($i=0;$i<3;$i++) {
    echo "<tr class=\"tl\">";
    echo "<td>$counter[$i]</td>";
    echo "</tr>";
    }
}
}

On one page, I need a table printed out. The data for this is in an array:

$page->extra = array( array('Track no.', 'Track title', 'Track length'),
                      array('01', 'Value1', 'No1'),
                      array('02', 'Value2', 'No2'),
                      array('03', 'Value3', 'No3'),
                      array('04', 'Value4', 'No4'),
                      array('05', 'Value5', 'No5'),
                      array('06', 'Value6', 'No6'),
                      array('07', 'Value7', 'No7'),
                      array('08', 'Value8', 'No8'),
                      array('09', 'Value9', 'No9'),
                      array('10', 'Value10', 'No10'),
                      array('11', 'Value11', 'No11'),
                      array('12', 'Value12', 'No12')
                     );

The errors I'm getting are:

Notice: Undefined variable: extra in C:\wamp\www\test\class_lib.php on line 47

...and:

Fatal error: Cannot access empty property in C:\wamp\www\test\class_lib.php on line 47

Here's the full class_lib.php file if needed: http://pastebin.com/7XRjDKVU

...and the index.php: http://pastebin.com/yHBYpNxd

I hope I've given enough information there - I'd appreciate any help.

4 Answers 4

2

you misstyped variable:

while (list($counter) = each($this->$extra)) {

should be

while (list($counter) = each($this->extra)) {

look at $this->extra

Sign up to request clarification or add additional context in comments.

4 Comments

Okay thanks, that removed the error, now I get a table with a very small width of about 2px without any values in it. Must be something wrong with the loop...
One thing though: now on every other page where I don't have a table and I haven't re-set $extra, the error: Warning: Invalid argument supplied for foreach() in C:\wamp\www\test\class_lib.php on line 47 is displayed. Is there anyway to get around this? Can I add an else and get it to continue on without inserting anything for $extra?
change if (isset($this->extra)) to if (count($this->extra))
Oh, it's good - I just removed the $extra variable from the class_lib.php so that it was only set on the pages I needed it to be set on. I though you had to create the variable and then overwrite it you see. Thanks for the help :)
1
$this->$extra

should be:

$this->extra

2 Comments

Okay thanks, that removed the error, now I get a table with a very small width of about 2px without any values in it. Must be something wrong with the loop...
According to your posted code, you are attempting to access $this->extra before it has been populated with data. You will need to insert your arra of data first, then access it.
1

In addition to what others have written about $this->extra, you're mis-using list() and $counter.

According to the PHP docs, list() is used to assign multiple variables from 1 array object.

The problem is with this line:

list($counter) = each($this->extra)

What happens is that each() pulls the next key->value pair from your $this->extra data. Then list() populates $counter with the first value from the pair, which is the Key value. Since $counter is now holding the int value of the Key, it can't be used as an array (and silently fails, echoing an empty character).

Try rearranging it like this:

echo '<table id="tab">';
foreach( $this->extra as $value) // ignores the Key and just gets the Value as $row
{
    list( $number, $name, $length ) = $value;  //populates $number, $name, $length from $row
    echo '<tr class="tl">';
    echo '<td>' . $number . '</td>';
    echo '<td>' . $name. '</td>';
    echo '<td>' . $length . '</td>';
    echo '</tr>';
}
echo '</table>';

You could also use foreach( $this->extra as $key=>value ) if you need the Key value.

Comments

0

$counter gets the index. you must put

while (list($i,$counter) = each($this->extra))

or

foreach($this->extra as $counter) {

this is because each returns key value pair:

Returns the current key and value pair from the array array.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.