matt_bois
matt_bois

Reputation: 43

How to run multiple queries in one php page?

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&eacute;nom et Nom</div>
            <div>Adresse</div>
            <div>Nom d'utilisateur</div>
            <div>Date d'inscription</div>
            <div>Demandes re&ccedil;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&eacute;rable en ce moment</div>';
        }
    }
 ?>

Upvotes: 0

Views: 4298

Answers (1)

Your Common Sense
Your Common Sense

Reputation: 158007

Do not write your own PDO wrapper. Period.

PDO is already a wrapper. Written by the professionals. It has some drawbacks, but at least it has no harmful features which newbie programmers introduce in hundreds.

If you want static method to get instance - all right, have it. But leave the rest for raw PDO. Saving yourself one function call doesn't worth struggling against fallacies of your own wrapper.

Do not save on calls at all! Look what are you doing:

<?php if ($st = DB::getInstance()->query("SELECT * FROM resum WHERE form_id = ? ORDER by date DESC", array(Input::get('formID')))) 

It cannot be even read without scrolling. Are you fined for every extra line or what?
This one single line contains almost dozen operators!

This is called "write-only" style.

You are saving yourself a linefeed to write faster, but when it come to reading, you'll run here, crying "read my code for me!".

Always code as if the person who ends up maintaining your code is a violent psychopath who knows where you live.

Especially if it's you who have to maintain. Look:

<?php 
$sql = "SELECT * FROM resum WHERE form_id = ? ORDER by date DESC";
$st = DB::getInstance()->query($sql, array(Input::get('formID')));
if ($st) 

One can read and comprehend it. And noone died for splitting this call in four lines. Now add a couple:

$sql = "SELECT * FROM resum WHERE form_id = ? ORDER by date DESC";
$st = DB::getInstance()->prepare($sql);
$st->execute(array(Input::get('formID')));
$data = $st->fetchAll();
if ($data) 

assuming getInstance() returns raw PDO instance. And your code will be perfectly fine working.

All right, I can understand a programmer's desire to avoid repetitions. Here is a solution that solves this problem without drawbacks:

$sql = "SELECT * FROM resum WHERE form_id = ? ORDER by date DESC";
$data = DB::prepare($sql)->execute(array(Input::get('formID')))->fetchAll();

And quit that idea of writing SQL calls right in the template. Fetch all your data first and then include a file with markup.

<?php if ($data): ?>
        <div id="topSeparateurClientConc">
        <div>Pr&eacute;nom et Nom</div>
        <div>Adresse</div>
        <div>Nom d'utilisateur</div>
        <div>Date d'inscription</div>
        <div>Demandes re&ccedil;ues</div>
    </div>
    <div style="clear:both;"></div>
<?php foreach($data as $result2): ?>
    <div class="clientGenInfosConc">

Upvotes: 1

Related Questions