Array of objects problem.. stuck on last element.

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
redstone18
Forum Newbie
Posts: 3
Joined: Wed May 19, 2010 1:03 pm

Array of objects problem.. stuck on last element.

Post 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.
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

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

Post by AbraCadaver »

The first problem is that you're using i instead of $i.
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
redstone18
Forum Newbie
Posts: 3
Joined: Wed May 19, 2010 1:03 pm

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

Post 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>";
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

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

Post 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.
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
redstone18
Forum Newbie
Posts: 3
Joined: Wed May 19, 2010 1:03 pm

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

Post 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!
Post Reply