I have a select query and then another select query but for another table. I need to run the first query, then if it finds something, show a table with a foreach loop. And then, a second query run and select another table with a foreach loop too.
This is my DB class to run my DB :
class DB {
private static $_instance = null;
private $_pdo,
$_query,
$_error = false,
$_count = 0;
public $_results;
private function __construct() {
try {
$this->_pdo = new PDO(...);
} catch(PDOException $e) {
die($e->getMessage());
}
}
public static function getInstance() {
if(!isset(self::$_instance)) {
self::$_instance = new DB();
}
return self::$_instance;
}
public function query($sql, $params = array()) {
$this->_error = false;
if($this->_query = $this->_pdo->prepare($sql)) {
$x = 1;
if(count($params)) {
foreach($params as $param) {
$this->_query->bindValue($x, $param);
$x++;
}
}
if($this->_query->execute()) {
$this->_results = $this->_query->fetchAll(PDO::FETCH_OBJ);
$this->_count = $this->_query->rowCount();
} else {
$this->_error = true;
}
}
return $this;
}
Then, on a page I have this following :
<?php if ($st = DB::getInstance()->query("SELECT * FROM resum WHERE form_id = ? ORDER by date DESC", array(Input::get('formID')))) {
if ($st->count()) { ?>
<div id="topSeparateurClient">
<div>Date</div>
<div>Concessionnaire</div>
<div>Client</div>
<div>Voiture</div>
<div>Prix</div>
<div>Statut</div>
</div>
<div style="clear:both;"></div>
<?php
foreach($st->_results as $result) {
echo "<div class=\"clientGenInfos\">
<div><b>".substr($result->date, 0, 10)."</b> / ".substr($result->date, 10)."</div>
<div>".$result->concessionnaire."</div>
<div>".$result->client."</div>
<div>".$result->voiture."</div>
<div>".$result->prix."</div>";
if ($result->statut == 'En Attente') {
echo '<div class="enAttente"><span>En Attente</span></div>';
} else if ($result->statut == 'Accepter') {
echo '<div class="accepter"><span>Accepter</span></div>';
} else if ($result->statut == 'Refuser') {
echo '<div class="refuser"><span>Refuser</span></div>';
}
echo " </div>
";
}
} else {
echo '<div class="aucuneDemande">Nothing from now.</div>';
}
}
?>
Then a second block, almost identical but with another table name in his query. The problem now is that my second table have the same values as the first one..I am stuck here, reading stuff on the net and nothing about this situation. I am still new to PDO object! please help!
EDIT ---
This is my second block..
<?php
if ($st = DB::getInstance()->query("SELECT * FROM users WHERE users.group = 3 ORDER by date DESC")) {
if ($st->count()) { ?>
<div id="topSeparateurClientConc">
<div>Prénom et Nom</div>
<div>Adresse</div>
<div>Nom d'utilisateur</div>
<div>Date d'inscription</div>
<div>Demandes reçues</div>
</div>
<div style="clear:both;"></div>
<?php
foreach($st->_results as $result2) {
echo "<div class=\"clientGenInfosConc\">
<div>".$result2->name."</div>
<div>".$result2->adresse."</div>
<div>".$result2->username."</div>
<div><b>".substr($result2->joined, 0, 10)."</b> / ".substr($result2->joined, 10)."</div>
<div>".$result2->concessionnaire."</div>
</div>";
}
} else {
echo '<div class="aucuneDemande">Aucune demande transférable en ce moment</div>';
}
}
?>