I have this class that pulls people out of a database. I want it to be a little prettier and function better.
<?php
class comps extends db_config {
public $output = array();
private $arr = array();
public $area;
public $me;
public $options = array();
public function __construct() {
$this->pdo = new PDO('mysql:host='.$this->_host.';dbname='.$this->_db.';charset='.$this->_charset, $this->_user, $this->_pass);
}
public function __toString() {
return var_dump($this->result());
}
public function result() {
return $this->output;
}
public function setoption($option, $value) {
array_push($this->options, array($option=>$value));
}
public function run() {
$where = array();
$limit = '';
$showMe = '';
if (isset($this->options['admin'])) {
$where[] = "admin = '".$this->options['admin']."'";
}
if (isset($this->options['area'])) {
$where[] = "area = '".$this->area."'";
}
if (isset($this->options['showMe']) && $this->options['showMe'] = true) {
$showMe = "AND missionary <> '".$this->me."'";
}
if (isset($this->options['limit'])) {
$limit = " LIMIT ".$this->options['admin'];
}
if (count($where) > 0) {
$where = implode(' AND ', $where);
} else {
$where = '';
}
$area = isset($this->area) ? $this->area : '';
if ($area != '') {
$sth = $this->pdo->prepare("SELECT missionary FROM missionary_areas WHERE area_uid = :area AND missionary_released = '0' ".$showMe);
$sth->bindParam(':area', $area);
$sth->execute();
$sth->setFetchMode(PDO::FETCH_OBJ);
while ($mis = $sth->fetch()) {
$sth2 = $this->pdo->prepare("SELECT * FROM missionarios WHERE ".$where." mid = :mid LIMIT 1");
$sth2->bindParam(':mid', $mis->missionary);
$sth2->execute();
$this->arr[] = $sth2->fetch(PDO::FETCH_ASSOC);
}
foreach($this->arr as $a) {
array_push($this->output, $a);
}
} else {
$sth = $this->pdo->prepare("SELECT * FROM missionarios ".$where.$limit);
$sth->execute();
$res = $sth->fetchAll(PDO::FETCH_ASSOC);
foreach($res as $a) {
array_push($this->output, $a);
}
}
}
}
... && $this->options['showMe'] = true
, I believe you meant to use==
. \$\endgroup\$ – meagar May 23 '13 at 20:34