Mario.C
Mario.C

Reputation: 65

Generate Unique Order ID

I'm trying to generate one unique order ID. I already have some code but I can't verify if the order already exist.

<?php
$exists = 1;   
while($exists > 0) {

    $store = "TLP"; //This one is just to appear before the random number Those are like the initials of the shop
    $orderid = substr(str_shuffle('0123456789'), 0, 3);
    $exists = $this->count_rows('tblorders', "WHERE oder_orderid=" . $store . $orderid);

    $value8 = $store . $orderid;
    echo $value8;

}?>

But im not getting anything from echo $value8;.

I just need one way to get the result $value8 verified and if there is one equal generate another one, and if there is no one equal exit the loop and keep the $value8

$store = "TLP";
$orderid = substr(str_shuffle('0123456789'), 0, 3);
$value8 = $store . $orderid;

the result of the code above would be something like TLP123 but I need one way to verify if TLP123 is already in my column and if it is in the column generate another one.

EDIT: Also tested with this one but got nothing too.

      function check_number(){

    $store = "TLP";
    $orderid = substr(str_shuffle('0123456789'), 0, 3);
    $value8 = $store . $orderid;
    $exists = $this->count_rows('tblorders', "WHERE oder_orderid='" . $store . $orderid."'");



    if ($exists >0){
        $results = check_number();
    }
     else{
            $results = $value8;
        return $results;
     }
echo $value8;

}

Upvotes: 0

Views: 1701

Answers (2)

Jack Hales
Jack Hales

Reputation: 1654

Sorry for the late reply, was at school and I didn't wanna get caught, but if you're still having the problem, you can use PDO in this way:

$statement = "SELECT * FROM table WHERE column = 'what_id_to_search_for'";
$query     = $pdo->query($statement, PDO::FETCH_ASSOC); # FETCH_ASSOC just returns an assosiative array.
if ($query->rowCount())
{
  # The row exists! Do again! (re-call function, etc...)
} else {
  # The row doesn't exist! Woo! We can insert!
}

If you're using MySQLi, etc... please let me know I'll delete my answer cause I don't like that connection language, and if that doesn't make sense I can rewrite it to make it simpler for you,

Also, I don't see why you don't just use an AUTO_INCREMENT type and then just set a type like TLP for example. :-)

Best of luck!

Upvotes: 1

Change:

$exists = $this->count_rows('tblorders', "WHERE oder_orderid=" . $store . $orderid);

to:

$exists = $this->count_rows('tblorders', "WHERE oder_orderid='" . $store . $orderid."'");

Upvotes: 1

Related Questions