Nick Brunt Posted January 5, 2011 Share Posted January 5, 2011 Hiya, I'm writing a photo album in PHP and I'm trying to set up a class which handles getting the pictures from the database. This is what I have so far: class Pictures { public $p = array(); function Pictures($albumID) { if (albumExists($albumID)) { $query = "SELECT * FROM `pictures` WHERE `albumID` = '" . $albumID . "';"; $result = mysql_query($query); $count = 0; while ($row = mysql_fetch_assoc($result)) { $p[$count++] = new Picture($row); } } } } class Picture { public $pid; public $bigsrc; public $thumbsrc; public $cover; public $albumID; function Picture($data) { $this->pid = $data['pid']; $this->bigsrc = $data['bigsrc']; $this->thumbsrc = $data['thumbsrc']; $this->cover = $data['cover']; $this->albumID = $data['albumID']; } } The idea is that you set up a new Pictures object and then get the data for a specific picture like this: $pictures = new Pictures(40); echo $pictures->p[0]->pid; This doesn't work and I'm not sure of the best way to do it. Help? Link to comment Share on other sites More sharing options...
0 Nick Brunt Posted January 5, 2011 Author Share Posted January 5, 2011 Bump? Link to comment Share on other sites More sharing options...
0 Mouldy Punk Posted January 6, 2011 Share Posted January 6, 2011 I can't remember for sure, but I don't think PHP likes using -> after a []. It might work if you did something like; $pictures = new Pictures(40); $picture = $pictures->p[0]; echo $picture->pid; Link to comment Share on other sites More sharing options...
0 Chris Crawford Posted January 6, 2011 Share Posted January 6, 2011 try this.... class PhotoAlbum { function Pictures($albumID) { $a=array(); $query = "SELECT * FROM `pictures` WHERE `albumID` = '" . $albumID . "';"; $result = mysql_query($query); $returned=mysql_num_rows($result); if ($returned>0) { while($row=mysql_fetch_array($result)) { $a[] = $row; } } return $a; } } then you can call it like so... $PhotoAlbum = new PhotoAlbum(); $Pictures = $PhotoAlbum->pictures(40); if(count($Pictures)>0){ //first result echo $pictures[0]['pid']; echo "<br>"; echo $Pictures[0]['cover']; echo "<hr>"; //loop all results foreach($Pictures as $Picture){ echo $Picture['pid']; echo "<br>"; echo $Picture['cover']; } }else{ echo "album empty"; Link to comment Share on other sites More sharing options...
Question
Nick Brunt
Hiya,
I'm writing a photo album in PHP and I'm trying to set up a class which handles getting the pictures from the database.
This is what I have so far:
class Pictures { public $p = array(); function Pictures($albumID) { if (albumExists($albumID)) { $query = "SELECT * FROM `pictures` WHERE `albumID` = '" . $albumID . "';"; $result = mysql_query($query); $count = 0; while ($row = mysql_fetch_assoc($result)) { $p[$count++] = new Picture($row); } } } } class Picture { public $pid; public $bigsrc; public $thumbsrc; public $cover; public $albumID; function Picture($data) { $this->pid = $data['pid']; $this->bigsrc = $data['bigsrc']; $this->thumbsrc = $data['thumbsrc']; $this->cover = $data['cover']; $this->albumID = $data['albumID']; } }The idea is that you set up a new Pictures object and then get the data for a specific picture like this:
This doesn't work and I'm not sure of the best way to do it.
Help?
Link to comment
Share on other sites
3 answers to this question
Recommended Posts