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 Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Working With Arrays

Status
Not open for further replies.

likelylad

IS-IT--Management
Jul 4, 2002
388
GB
Hi All
I am trying to get the following piece of code to work
Code:
while (!$rs->EOF)  //carry on looping through while there are records
{
$count=$fld[1];
$name=$fld[0];
echo $name.$count;
$thecount[$name]=$count;
echo $thecount[$name];

    $rs->MoveNext(); //move on to the next record
}

The Following line from above shows the expected information
Code:
echo $name.$count;

However the following line shows nothing
Code:
echo $thecount[$name];

The Apache error log shows the following

PHP Warning: Illegal offset type in C:\\web\\web\\web.php on line 44
PHP Warning: Illegal offset type in C:\\web\\web\\web.php on line 45


Lines 45 and 45 are
Code:
$thecount[$name]=$count;
echo $thecount[$name];

Thanking in advance for any help received
 
how does the loop know what $fld is? i cannot see it defined or returned anywhere.
 
Hi jpadie

That is only a portion of the code. I know that $fld is OK, as I can echo out the information held in the array.
 
i doubt that you're right. you have plotted a loop that iterates a recordset but you are not, in the loop, using the $rs object to return any data. ie. the $fld array will be static for _every_ row of the recordset.

additionally the error that you are receiving indicates that $thecount is an object and you are trying to address a property of that object as if it were an array. this does not work. to address an object property this is the right syntax
Code:
$object->property
 
I sorted out my issue with the following code. I don't know if its an idea solution but it works.

Code:
for ($i=0; $i < $num_columns; $i++) {
    $fld[$i] = $rs->Fields($i);
}

while (!$rs->EOF)  //carry on looping through while there are records
{
$arrayinfo="\"".$fld[0]."\"=>$fld[1],";
$arrayinfolist=$arrayinfolist.$arrayinfo;

    $rs->MoveNext(); //move on to the next record
}

//close the connection and recordset objects freeing up resources 
$rs->Close();
$conn->Close();

$cleanarraylist=rtrim($arrayinfolist,",");

$buildarray="\$thevar=array($cleanarraylist);";
 
Sorry left a very important bit out at the end
Code:
eval("$buildarray");
 
i can't see how that works as you are never refreshing your field values within the iteration loop.

surely something like this is what you're after?
Code:
$array = array();
$rs->moveFirst();
do{
	$array[] = array(trim($rs->Fields(0)), trim($rs->Fields(1)));
        $rs->moveNext();
} while (!$rs->EOF);

echo "<pre>" . print_r($array, true) . "</pre>";
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top