Search a values in Database and get all values in

2019-09-03 21:13发布

问题:

i want to search coupon number in a row if coupon number is match given by user get all values in this single row using Codeigniter. Here is My Code.

Model

public function ven_coupon($coupon)
    {
        $where = array(
            'number' => $coupon,
        );
        $this->db->select()
                 ->from('vendor_coupons')
                 ->where($where);
        $data = $this->db->get();
        return $data->result_array();
    }

Controller

public function ven_coupon()
    {
        $this->load->model('front');
        if ($_POST) {
            $number = $_POST['coupon'];
            if (!$this->front->ven_coupon($number)) {
                echo "Not Valid";
            }
            $payment = $this->cart->total();
            $discount['discount'] = ($payment*10)/100;
            $this->load->view('checkout',$discount);
        }
    }

View

<form action="<?=base_url();?>home/ven_coupon" method="post">
    Coupon Number: <input type="text" name="coupon">
    <input type="submit" name="submit" value="Apply Coupon">
</form>

回答1:

You question is not clear what actually you need. But if you want all values from database than you need to modify your controller as:

$data = $this->front->ven_coupon($number);
if(count($data) <= 0){
  echo "not valid"; // print error
}
else{
  print_r($data); // print all data
}

This is basic example you can store them into variable and pass into load view.