Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

foreach problems

Status
Not open for further replies.

NevG

Programmer
Oct 10, 2000
162
GB
AAAAAAAAAGGGGGGGHHHHHHHH!!!!!!!!!!!!!!!!

**** Warning: Invalid argument supplied for foreach() (line 31,44,50)***

Whats this mean please and how can I fix it....

Heres my code.

<?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] = &quot; &quot;;
$row[] = $row_assoc[$header];
}
array_push($this->table_array, $ow);
return true;
}

function output()
{
print &quot;<PRE>&quot;;
foreach( $this->headers as $header )
print &quot;<B>$header</B>&quot;;
print &quot;\n&quot;;

foreach( $this->table_array as $y )
{
foreach( $y as $xcell )
print &quot;$xcell &quot;;
print &quot;\n&quot;;
}
print &quot;</PRE>&quot;;
}

}

$test = new table(&quot;a&quot;,&quot;b&quot;,&quot;c&quot;);
$test->addRow(array(1,2,3));
$test->addRow(array(4,5,6));
//$test->addrowAssocArray(array(b=>0,a=>6,c=>3));
$test->output();


?>
 
function output()
{
print &quot;<PRE>&quot;;
foreach( $this->headers as $header )
{
print &quot;<B>$header</B>&quot;;
print &quot;\n&quot;;
}
foreach ***************************************
Party on, dudes!
[cannon]
 
Thanks but that didnt actually do anything. I still get warning msgs for all three lines. I cannot figure this out. If you have atheory I would be grateful to hear it.

Thanks again
 
well, i never use foreach. I prefer each instead

while(list($key,$value)=each($array)){

// here i can use $key and $value ... for each element of the array
} Anikin
Hugo Alexandre Dias
Web-Programmer
anikin_jedi@hotmail.com
 
Thanks Anakin

Can you expand on that and put it to work in my code shown above. Im kinda new to PHP so need as much help as possible
Thanks again

Nev G
 
instead of:
foreach( $this->headers as $header )
print &quot;<B>$header</B>&quot;;

use

while(list(,$header)=each($this->headers)){
print &quot;<b>$header</b>&quot;;

This is how to use the arrays.

if you use it more than once, i advise you to do a reset before another use

reset($this->header);

Anikin
Hugo Alexandre Dias
Web-Programmer
anikin_jedi@hotmail.com
 
You know, when I look at your code, it seems that your Table constructor function takes 1 parameter, but in fact you pass it 3 parameter. I know you mean an array of values. You know you mean an array of values. But I don't see that PHP has any way of knowing you mean an array of values.

$test = new table(&quot;a&quot;,&quot;b&quot;,&quot;c&quot;);

Try adding print_r($this->headers) and print_r($this->cols) in the constructor after the assignment to find where the problem lies.

foreach() DOES work!! It is a fudge to swap it for 'while'.


Hugh Prior

Your local search engine
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top