corillo181 Posted April 12, 2008 Share Posted April 12, 2008 so i made a class it works fine, the only problem is i want to instead of getting one record i want an array how do i get the select function to return an array of rows? <?php class ChatText{ public $db; private $id; private $user; private $room; private $text = array(); public function __construct(){ $this->db = new DB(); } // set and get id public function set_id($v){ $this->id = (int) $v; } public function get_id(){ return $this->id; } // set and get user public function set_user($v){ $this->user = (int) $v; } public function get_user(){ return $this->user; } // set and get room public function set_room($v){ $this->room = (int) $v; } public function get_room(){ return $this->room; } // set and get text public function set_text($v){ $this->text = (string) $v; } public function get_text(){ return $this->text; } // insert into text users public function insert(){ $sql = "INSERT INTO chat(user_id,room_id,text,date)VALUES('{$this->get_user()}','{$this->get_room()}','{$this->get_text()}',NOW())"; $this->db->query($sql); } // select form tra user public function select($v){ $sql = 'SELECT * FROM chat '.$v; $result = $this->db->query($sql); $obj = mysql_fetch_object($result); $this->set_id($obj->id); $this->set_user($obj->user_id); $this->set_room($obj->room_id); $this->set_text($obj->text); } } ?> this is the out put $text = new ChatText(); $text->select('ORDER BY date ASC'); echo $text->get_text(); Link to comment https://forums.phpfreaks.com/topic/100823-solved-help-with-class/ Share on other sites More sharing options...
Northern Flame Posted April 12, 2008 Share Posted April 12, 2008 well since $text->text is an array, you will need to change: public function get_text(){ return $this->text; } to public function get_text(){ foreach($this->text as $val){ echo $val . "<br />\n"; } } Link to comment https://forums.phpfreaks.com/topic/100823-solved-help-with-class/#findComment-515599 Share on other sites More sharing options...
corillo181 Posted April 29, 2008 Author Share Posted April 29, 2008 [b]that is not what I'm long for I'm looking for something more like this <?php class Poll{ public $db; public $ojb = array(); // constructor public function __construct(){ $this->db = new DB(); } //SELECT public function select($v){ $sql = 'SELECT * FROM tra_poll ' . $v; $this->obj = mysql_fetch_object($this->db->query($sql)); } } $poll = new Poll(); $poll->select('ORDER BY id DESC'); echo $poll->obj->question; ?> but i went to point to a question be doing something like echo $poll->obj[0]->question; i know this wont work, but something similar is it possible? Link to comment https://forums.phpfreaks.com/topic/100823-solved-help-with-class/#findComment-529922 Share on other sites More sharing options...
phorman Posted April 30, 2008 Share Posted April 30, 2008 Replace your select fucntion with this: public function select($v){ $sql = 'SELECT * FROM tra_poll ' . $v; while ($this->obj[] = mysql_fetch_object($this->db->query($sql)) { echo ""; } } this will loop through all results of your query, and store the results into the array obj. To call it, it would be: $poll = new Poll(); $poll->select('ORDER BY id DESC'); echo $poll->obj[0]->question; ?> Link to comment https://forums.phpfreaks.com/topic/100823-solved-help-with-class/#findComment-529963 Share on other sites More sharing options...
corillo181 Posted April 30, 2008 Author Share Posted April 30, 2008 thank you !!! Link to comment https://forums.phpfreaks.com/topic/100823-solved-help-with-class/#findComment-530075 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.