如何检查数据库CodeIgniter上是否存在行

时间:2018-10-24 11:26:13

标签: php codeigniter codeigniter-3

因此,我对此查询有点麻烦,因为这是第一次,而不是检查行中的值,而是检查数据库中是否存在行本身。

这是我面临的问题:

更新:

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);

            }
        }
    }

这是模型中的方法:

更新:

  /*
  *
  * 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);
  }

注意:如您所见,我已经在使用isset,但是这弄乱了下面的代码块(如果需要的话,我将整个函数放到这里),这些代码块未在此处发布;我已经使用过if emptyif var === false,但仍然遇到相同的错误。

1 个答案:

答案 0 :(得分:0)

verifyUserCartItem函数中尝试以下操作:

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

if(!$itemInCart){而不是if(isset($itemInCart)){