Jump to content

[SOLVED] help with class


corillo181

Recommended Posts

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

  • 3 weeks later...

[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?

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;

?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.