开发者

PHP object/variable error

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.


you misstyped variable:

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

should be

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

look at $this->extra


$this->$extra

should be:

$this->extra


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.


$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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜