1

Hye there how do i prioritize if there are same date and time. For example i have 2 users press a button on a same date and time. Therefore in my database i have two values with the same date and time. Now what i want is to prioritize which is the first and which is the second. Then i will only accept value for the first and show and error message "Not Available" to the second

For Example:

User 1 press a button = 2014-07-07 03:22:00

User 2 press a button = 2014-07-07 03:22:00

Now from this two i want to prioritize it which one is the first and which one is the second

Below Is My Code To Save Data In Table where i created it using Yii framework

public function actionAdd($bookId ) {

    $book = Book::model()->findByPk($bookId);

    //check if booking is allowed
    if (!Helper::allowBooking($bookId)) {
        throw new CHttpException(400,'Book is not available for booking');
    }

    //if booking allowed, save data into table
    $model = new Booking;
    $model->user_id = Yii::app()->user->id;
    $model->book_id = $bookId;

    $model->booked = date('Y-m-d H:i:s');

    if ($model->save()) {
        Yii::app()->user->setFlash('success', $book->title . ' successfully added to your booked list.<br>Kindly find your book at Rack: <b>' . $book->rack . '</b> and display ID: <b>' . $model->id . '</b> when you want to check out the book from the library');
        $this->redirect(array('/users/history'));


    }
}

This Is My Code For Allow Booking

public static function allowBooking($id) {
    $bookings = Booking::model()->findAllByAttributes(array('user_id' => Yii::app()->user->id));    

    //check global limit for borrowing
    $limit = Booking::model()->findBySql("SELECT count(*) as qty FROM bookings WHERE user_id = '" . Yii::app()->user->id . "' AND returned IS null AND ((time_to_sec(timediff(DATE_ADD(booked, INTERVAL 2 HOUR), NOW())) / 3600) > 0 AND (time_to_sec(timediff(DATE_ADD(booked, INTERVAL 2 HOUR), NOW())) / 3600) < 2)");

       //Only Limit For 5 Booking
       if ($limit->qty >= 5)  
       {
       return false;
       }        

    $book = Book::model()->findByPk($id);
    //don't take old booking into account, check for within 2 hours
    $booked = Booking::model()->findBySql("SELECT count(*) as qty FROM bookings WHERE book_id = '$id' AND returned IS null");

    $availableQty = $book->stock - $booked->qty;

        if ($availableQty > 0)
        {
        return true;
        }

        return false;
 }
AsySyah
  • 21
  • 1
  • 6
  • what do you mean which is first and which is second? are you validating after the records are inserted? if so, why not check which id is lowest (with an auto incremental id) to find out which record was inserted first? – deacs Jul 06 '14 at 23:47
  • if i validating using id therefore it is not fair because if you registered first then you will alwayls become the first... – AsySyah Jul 07 '14 at 05:11
  • well how do you want to differentiate between the records? not first come first serve? – deacs Jul 07 '14 at 07:05
  • If you add one more field for microtime(), I think it will help you to prioritize – sarvesh Jul 07 '14 at 07:27
  • I read about it where u can put u after the s $model->booked = date('Y-m-d H:i:s'); But first u need to wrap it. Can u show me partial code on how to implement the microtime – AsySyah Jul 07 '14 at 08:38

0 Answers0