Nick Brunt Posted January 3, 2011 Share Posted January 3, 2011 Hiya, I'm building a photo album system and I have this same bit of code all over the place. $query = "SELECT * FROM `album` WHERE `albumID` = '" . $albumID . "';"; $result = mysql_query($query); $albumName = mysql_result($result, 0, 'name'); $albumDesc = mysql_result($result, 0, 'description'); $albumDate = mysql_result($result, 0, 'date'); I'd like to make a function which takes in $albumID and spits out $albumName, $albumDesc and $albumDate. I don't want to use an array. Would it be best just to return the result (ie $result in the above example), and then do $albumName = mysql_result($result, 0, 'name'); $albumDesc = mysql_result($result, 0, 'description'); $albumDate = mysql_result($result, 0, 'date'); every time, or is there a better way? Should I use global variables? Cheers Link to comment Share on other sites More sharing options...
0 jimbo11883 Posted January 3, 2011 Share Posted January 3, 2011 An array is the most portable solution. Link to comment Share on other sites More sharing options...
0 Nick Brunt Posted January 3, 2011 Author Share Posted January 3, 2011 An array is the most portable solution. Yeah I know you're right, but I don't really want to rewrite the entire program. I've already used these variable names all over the place and now I'm just trying to make the various different sections that are repeated often into functions. Link to comment Share on other sites More sharing options...
0 jimbo11883 Posted January 3, 2011 Share Posted January 3, 2011 You could also write a class: class Album { public $name; public $description; public $date; function Album($albumID) { $query = "SELECT * FROM `album` WHERE `albumID` = '" . $albumID . "';"; $result = mysql_query($query); $data = mysql_fetch_assoc($result); $this->name = $data['name']; $this->description = $data['description']; $this->date = $data['date']; } } /* Example: */ $al = new Album(1); echo $al->name; Link to comment Share on other sites More sharing options...
0 Nick Brunt Posted January 3, 2011 Author Share Posted January 3, 2011 You could also write a class: class Album { public $name; public $description; public $date; function Album($albumID) { $query = "SELECT * FROM `album` WHERE `albumID` = '" . $albumID . "';"; $result = mysql_query($query); $data = mysql_fetch_assoc($result); $this->name = $data['name']; $this->description = $data['description']; $this->date = $data['date']; } } /* Example: */ $al = new Album(1); echo $al->name; That's pretty neat... but would still require a lot of rewriting... although I guess I could do a find/replace. Link to comment Share on other sites More sharing options...
0 Mouldy Punk Posted January 3, 2011 Share Posted January 3, 2011 (edited) Something like this is pretty much the textbook example of object oriented design. What I would do, is return an Album object. class Album{ private $id; private $name; private $description; private $date; // Getters/Setters static public function getById($id){ $query = "SELECT * FROM `album` WHERE `albumID` = '" . $albumID . "';"; $result = mysql_query($query); $album = new self(); $album->setId($id); $album->setName(mysql_result($result, 0, 'name')); $album->setDesc(mysql_result($result, 0, 'description')); $album->setDate(mysql_result($result, 0, 'date')); return $album; } } You could then do something like; $albumObj = Album::getById(1); and then use the Album class' methods to do whatever you want with $albumObj Edit; I got beaten to it :p Cupcakes and Chester0 2 Share Link to comment Share on other sites More sharing options...
Question
Nick Brunt
Hiya,
I'm building a photo album system and I have this same bit of code all over the place.
I'd like to make a function which takes in $albumID and spits out $albumName, $albumDesc and $albumDate.
I don't want to use an array.
Would it be best just to return the result (ie $result in the above example), and then do
every time, or is there a better way? Should I use global variables?
Cheers
Link to comment
Share on other sites
5 answers to this question
Recommended Posts