views:

49

answers:

1

Hi all, I'm not sure if this is even possible after trying to figure it out for hours but here goes...

I have an class, UserPicture, which has properties for filename, filetype, created etc. (i.e. it doesn't store the actual picture as a blob, rather references it by using $filename.$filetype).

I want to be able to have a page that displays all of a user's pictures, so I need to retrieve all rows from the DB relevant to that user. I've got the associative array out of the DB successfully and have used the following code to create the object, which works as I have tested the output of it by echoing it...

$result=query("SELECT * FROM pictures WHERE user_id=$user_id");
// Returns associative array with numerous succesfully.
$pictures = array();
foreach($result as $row) {
    $pictures = new UserPicture($row);
}

This kinda works but I only get the last row as an object in the array. So I have tried array_push...

foreach($result as $row) {
   array_push($pictures, new UserPicture($row));
}

...and I've tried using $pictures[]=new UserPicture($row), but both just give me the following error...

Catchable fatal error: Object of class UserPicture could not be converted to string in user_picture_manage.php on line 72

If anyone could please shed any light on what I'm doing wrong that would be very helpful!

Many thanks, Steve

+2  A: 

You're overwriting the $pictures variable in your above code. You need to add a new key for each row. The following should do the trick:

$result=query("SELECT * FROM pictures WHERE user_id=$user_id");
// Returns associative array with numerous succesfully.
$pictures = array();
foreach($result as $row) {
    $pictures[] = new UserPicture($row);
}

Note where I've added squared braces ([]). For each iteration in the foreach loop, a new key will be added to the $pictures array containing the new UserPicture class as the value.

You should then be able to iterate over your new $pictures array as follows:

foreach ($pictures as $picture) {
    $src = $picture->filename . "." . $picture->filetype;
    echo '<img src="<?php echo $src; ?>" alt="" />';
}
Martin Bean
Actually, I answered that too but deleted it... But he already said `...and I've tried using $pictures[]=new UserPicture($row)`
Chouchenos
Nice edit ;) But, accessing to object variables directly isn't nice. You should pass them via getters
Chouchenos
How so? I've never really understood the advantage of accessing a property via a getter as opposed to just accessing a public property directly...?
Martin Bean
Fantastic thank you very much Martin - this works perfectly. I should have realised this but I'm still trying to beat the old procedural ways out of me (only just got into OO PHP but been using PHP in general for about 7 years). Chouchenos - you also make a very good point, I need to start doing everything through getters and setters more instead of accessing properties directly.
SirMalius
Martin - I think it's more about enforcing a certain interface with the object, so rather than accessing things directly you have to go through the method. On larger projects I guess this could potentially make it easier to fix bugs.
SirMalius
@SirMalius Glad you got it sorted. OOP is one of those topics worth knowing. I got into PHP about three years ago, but was taught OOP from the start. I'm no expert, but love what OOP tries to enforce.
Martin Bean
Martin - I know what you mean and totally agree with you. This project I'm working on is a big one but I've only just started it really. I can already tell that the OO way is going to save me hours of coding later on though. Now I've just gotta figure out the best way to do getting and setting...might just not bother lol.
SirMalius