|
Posted by Schraalhans Keukenmeester on 04/02/07 13:33
TKirahvi wrote:
>> What values does $db->f('id') return? Are there really two records? Is
>> $db's class doing what it's supposed to?
>>
>> The problem does not lie in the assignment to $galleries[], that's for
>> sure. So the problem is elsewhere in your code, or the concept behind
>> it. Post more details, and test your snippet by echoing intermediate
>> results to screen. Perhaps there's a good clue.
>>
>
> If I echo($db-f('id')); within while loop, it echoes correct id
> numbers (in this case 1 and 2), as so:
>
> while( $db->next_record() )
> {
> $foo = $db->f('id');
> echo($foo);
> }
>
> output: 1 2
>
> But if I create Gallery Objects within the loop, I guess only first
> one is created properly:
>
> while( $db->next_record() )
> {
> $foo = $db->f('id');
> gallery = new Gallery($foo);
> echo($gallery->id);
> }
>
> output: 1
>
> So it seems Arrays has nothing to do with this. I wonder if Gallery
> Object is missing something crucial, I'm not that familiar with PHP's
> Objects. My Gallery Object is:
>
> class Gallery
> {
> var $id;
> var $name;
> var $description;
> var $owner;
> var $authority;
> var $date;
>
> function __construct($id)
> {
> global $db;
>
> $select = 'SELECT * FROM gallery WHERE id = '.$id;
> $result = $db->query( $select );
>
> if ( !$result) {
> echo( 'Query failed');
> }
> else
> {
> $db->next_record();
> $this->id = $db->f("id");
> $this->name = $db->f("name");
> $this->description = $db->f("description");
> $this->owner = $db->f("owner");
> $this->authority = $db->f("authority");
> $this->date = $db->f("date");
> }
> }
>
> function __get($v)
> {
> return $this->$v;
> }
> }
>
I'd think the problem lies in the fact your Gallery constructor itself
interacts with the class $db is an instance of. First of all using a
global var in a class like you did here doesn't seem a thought-through
OOP construction. I'd avoid using globals in a class like the plague
personally.
Inside the constructor you make a call to $db->next_record() again, so
the loop in your calling script makes a THIRD call, not the second.
This undoubtedly causes your problem.
This example definitely doesn't come across as a good design, so you may
help yourself by redesigning the Gallery class and do away with
accessing global objects' methods inside it.
HTH, Sh.
[Back to original message]
|