简体   繁体   中英

How to check if row exists on database CodeIgniter

So I'm having a bit of troubles with this query as it is the first time in which instead of checking for a value in a row, I'm checking if a row itself exists on database.

This is the problem that I'm facing:

UPDATE:

public function individualDiscountVerification($id){

        // Get postID
        $data['item']           =   $this->PublicStore_model->readPostID($id);
        $data['relationship']   =   $this->PublicStore_model->getPostRelationship($id);

        // Get Last Post ID
        $postID         =   $id;
        $activityTitle  =   $data['item']->title;

        // Verify Data
        $itemInCart = $this->PublicCart_model->verifyUserCartItem($postID);

        // Redirect if row does not exist -- HERE IS WHERE I NEED HELP
        if(!$itemInCart){

            // Set message
            $this->session->set_flashdata('error', 'You first need to add it to you cart');

            // Redirect
            redirect('store/read/'.$id);


        // Redirect if seller is the same as the current userID
        } elseif($this->session->userdata('user_id') == $data['relationship']->user_id) {

            // Set message
            $this->session->set_flashdata('error', 'You can not add discounts to your own products');

            // Redirect
            redirect('store/read/'.$id);

        } else {

            // Verify discount exists
            $discount       =   $this->input->post('discount_code');
            $discountCode   =   $this->PublicCart_model->verifySingleDiscount($discount);

            if(!$discountCode){

                // Set message
                $this->session->set_flashdata('error', 'The discount has expired or is invalid');

                // Redirect
                redirect('store/read/'.$id);

            } else {

                // Get Last ID Data
                $discountID     =   $discountCode->discount_id;

                // Get Discount Type
                $discountType   =   $this->PublicCart_model->getIndividualDiscountID($discountID);

                // Verify data
                $verifyItemDiscount =   $this->PublicCart_model->verifySingleItemDiscountCode($postID, $discountID);

                if($discountType->type == 'percent' && $verifyItemDiscount != NULL?:'' && $discountID == $data['relationship']->discount_id){

                    // Update Post Array
                    $postData   =   array(
                        'price'         =>  round($data['relationship']->price * ((100 - $discountCode->amount) / 100), 2),
                        'discount_id'   =>  $discountID,
                    );
                    // Update Post Array
                    $this->PublicCart_model->updateUserCartItem($postID, $postData);

                    // Set message
                    $this->session->set_flashdata('success', 'A percent discount type has been applied to your final price');

                    // Redirect
                    redirect('store/read/'.$data['item']->post_id);

                } elseif($discountID != $data['relationship']->discount_id){

                    // Set message
                    $this->session->set_flashdata('error', 'Percent discount is not attached to this product');

                    // Redirect
                    redirect('store/read/'.$data['item']->post_id);

                } elseif($discountType->type == 'float' && $verifyItemDiscount != NULL?:'' && $discountID == $data['relationship']->discount_id){

                    // Update Post Array
                    $originalprice  =   $data['relationship']->price;
                    $amount = $discountCode->amount;
                    $postData   =   array(
                        'price'         => $originalprice - $amount,
                        'discount_id'   =>  $discountID,
                    );
                    // Update Post Array
                    $this->PublicCart_model->updateUserCartItem($postID, $postData);

                    // Set message
                    $this->session->set_flashdata('success', 'A float discount type has been applied to your final price');

                    // Redirect
                    redirect('store/read/'.$data['item']->post_id);

                } elseif($discountID != $data['relationship']->discount_id){

                    // Set message
                    $this->session->set_flashdata('error', 'Float discount is not attached to this product');

                    // Redirect
                    redirect('store/read/'.$data['item']->post_id);
                }

                // Activity Array
                $activityData   =   array();
                // Insert Activity
                $this->Activity_model->add($activityData);

            }
        }
    }

and here is the method in the model:

UPDATE:

  /*
  *
  * IS IN USER'S CART? -- FROM HERE AND BELOW IS FOR THE INDIVIDUAL ITEMS DISCOUNT
  *
  */
  public function verifyUserCartItem($postID){
    $query = $this->db->get($this->relationship, array(
      'post_id'   =>  $postID,
      'friend_id' =>  $this->session->userdata('user_id'),
      'type'      =>  $this->cartType,
    ));

    if($query->num_rows() > 0){
      return $query->row_array();
    } else {
      return null;
    }

  }
  /*
  *
  * DOES THE DISCOUNT EXISTS?; IF YES, WHAT TYPE OF DISCOUNT IT IS?
  *
  */
  public function verifySingleDiscount($discount){
    $query  =  $this->db->get_where($this->discounts, array(
      'code'  =>  $discount,
    ));
    return $query->row();
  }

  public function getIndividualDiscountID($discountID){
    $query  = $this->db->get_where($this->relationship, array(
      'discount_id' =>  $discountID,
      'status'      =>  $this->published,
    ));
    return $query->row();
  }
  /*
  *
  * IS THE DISCOUNT ATTACHED TO THE POST?; IF YES, UPDATE IT
  *
  */
  public function verifySingleItemDiscountCode($postID, $discountID){
    $query  = $this->db->get_where($this->relationship, array(
      'post_id'     =>  $postID,
      'discount_id' =>  $discountID,
      'status'      =>  $this->published,
    ));
    return $query->row();
  }

  public function updateUserCartItem($postID, $postData){
    $this->db->select('*');
    $this->db->where('friend_id', $this->session->userdata('user_id'));
    $this->db->where('post_id', $postID);
    $this->db->where('type', $this->cartType);
    $this->db->update($this->relationship, $postData);
  }

Note: As you can see I'm already using isset but that messes up the following blocks of code(I'll put the whole function if requested) which are not posted here; I already used if empty or if var === false but I'm still getting the same error.

try below in verifyUserCartItem function :

if($query->num_rows() > 0){
  return $query->row_array();
} else {
  return null;
}

and if(!$itemInCart){ instead of if(isset($itemInCart)){

例如:

var_dump($this->db->where('id', $id)->get('TABLE_NAME')->num_rows());

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM