Page 1 of 1

Array of objects problem.. stuck on last element.

Posted: Wed May 19, 2010 1:26 pm
by redstone18
I am trying to do a simple array of objects built from a query, which I think I have done correctly but am having problems dumping the array of ojects out. Any help is appreciated as I am a php novice and am not sure what I am trying to do is correct.

First my class:

<?php
class item {
/* Member functions and variables go here */
public $descript;
public $seg1;
public $inv_id;
public $web_stat;
}
?>

Next query into array:

oci_execute($s);
$i = 0;
while ($res = oci_fetch_array($s, OCI_BOTH+OCI_RETURN_LOBS)) {
$titem[] = new item();
$titem->descript = $res[1];
$titem->seg1 = $res[2];
$titem->inv_id = $res[3];
$titem->web_stat = $res[4];
$i++;
echo $i . ' '.$titem->descript.' '.$titem->seg1.' '.$titem->inv_id.' ' . "<br>";
}
echo oci_num_rows($s) . ' ' . " Number of rows" . "<br>";
oci_free_statement($s);
oci_close($c);

I get the correct output of 10 rows.

1 APLW6009000 81 PUBLISHED
2 APLW6016000 88 PUBLISHED
3 APLW6015000 87 PUBLISHED
4 APLW6012000 84 PUBLISHED
5 APLW6011000 83 PUBLISHED
6 APLW6010000 82 PUBLISHED
7 APLW6014000 86 PUBLISHED
8 APLW6013000 85 PUBLISHED
9 APLW6007000 79 PUBLISHED
10 APLW6006000 78 PUBLISHED
10 Number of rows
Number in titem: 11

Next I try to output:

foreach ($titem as $curkey => &$value ) {
echo "Starting foreach <br>" ;
$user = (Object)$value;
echo $curkey . "<br>" ;
echo $user->descript . "<br>" ;
}
____
Starting foreach
0
Starting foreach
i
APLW6006000
Starting foreach
1
Starting foreach
2
... OMITTED for brevity....
Starting foreach
9

Question, Why am I STUCK on the last element in the array. Have used reset() first() NOTHING works! Not sure what I am trying to do is legal or a syntax problem. Thanks for looking.

Re: Array of objects problem.. stuck on last element.

Posted: Wed May 19, 2010 2:00 pm
by AbraCadaver
The first problem is that you're using i instead of $i.

Re: Array of objects problem.. stuck on last element.

Posted: Wed May 19, 2010 2:09 pm
by redstone18
THANKS! this worked - I feel foolish for spending many hours on this.

However now my output when loading the array does not work: echo $i . ' '.$titem[$i]->descript.' '.$titem[$i]->seg1.' '.$titem[$i]->inv_id.' ' . "<br>";

Re: Array of objects problem.. stuck on last element.

Posted: Wed May 19, 2010 2:16 pm
by AbraCadaver
redstone18 wrote:THANKS! this worked - I feel foolish for spending many hours on this.

However now my output when loading the array does not work: echo $i . ' '.$titem[$i]->descript.' '.$titem[$i]->seg1.' '.$titem[$i]->inv_id.' ' . "<br>";
That's because you're incrementing $i before you echo.

Re: Array of objects problem.. stuck on last element.

Posted: Wed May 19, 2010 2:21 pm
by redstone18
AbraCadaver wrote:
redstone18 wrote:THANKS! this worked - I feel foolish for spending many hours on this.

However now my output when loading the array does not work: echo $i . ' '.$titem[$i]->descript.' '.$titem[$i]->seg1.' '.$titem[$i]->inv_id.' ' . "<br>";
That's because you're incrementing $i before you echo.

This fixed it - MANY THANKS!