0

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>';
        }
    }
 ?>
5
  • mybe have a look at that: stackoverflow.com/questions/11271595/pdo-multiple-queries Commented Jun 3, 2014 at 6:12
  • 1
    @Jester may be have a look at question body before commenting Commented Jun 3, 2014 at 6:22
  • perhaps if I use another fetch for my second query it'd work? Commented Jun 3, 2014 at 6:23
  • If you're having trouble with the second block, you need to post it. How are we supposed to figure out what's wrong if you only post the first block? Commented Jun 3, 2014 at 6:25
  • @Barmar all the required code already posted and case is pretty clear Commented Jun 3, 2014 at 6:25

1 Answer 1

1

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">
Sign up to request clarification or add additional context in comments.

7 Comments

Thanks you for your comments and your time. I do appreciate it and always be better at programming. I am testing it, will come back to you for a confirmation!
I know. This is entirely my fault. Instead of answering with explanations I had to write entire code, polish and debug it, and then present it for you on a plate.
lollll I am so sorry, I didn't want to insult you. Your help was worth a lot, right now I think this will be working soon enough :)
You didn't. but I hoped you would read the explatations too, beside just copying the code parts.
I did, and I now I am modyfing it because of some issues!
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.