简体   繁体   中英

Match a value in database and get all data in this row using codeigniter

i want if user input a number or username if value is matched in column get all data in this single row. Here is My Code but show Error.

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()
    {
        if ($_POST) {
            $number = $_POST['coupon'];
            $query = $this->front->ven_coupon($number);
            if (count($query) <= 0 ) {
                echo "Not Valid";
            }
            $payment = $this->cart->total();
            $discount['discount'] = ($payment*10)/100;

            $discount['data'] = $this->front->ven_coupon($number);
            $this->load->view('checkout',$discount);
        }
    }

View

<form action="<?=base_url();?>home/ven_coupon" method="post">
            Coupon: <input type="text" name="coupon">
            <input type="submit" name="submit" value="Apply Coupon">
        </form>
        <?php if($discount) { ?>
            <p>Discount: 10%</p>
            <p>Total: <?=$discount;?></p>
            <?=$data['discount'];?>
            <?=$data['number'];?>
        <?php } ?>

check the number of rows of $query in model not controller
try this :

model

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

controller

public function ven_coupon()
    {
        if ($_POST) {
            // $number = $_POST['coupon'];
            $number = $this->input->post('coupon');
            $query = $this->front->ven_coupon($number);
            // if (count($query) <= 0 ) {
            //     echo "Not Valid";
            // }

            if ($query == FALSE) {
                echo "Not valid"; // this is bad format.
                /*use this */
                $data['message'] = 'Not valid';
                $this->load->view('yourview',$data);
            }
            $payment = $this->cart->total();
            $discount['discount'] = ($payment*10)/100;

            $discount['data'] = $this->front->ven_coupon($number);
            $this->load->view('checkout',$discount);
        }
    }

view

<?php 
    if (isset($message)) {
        echo $message;
    }
?>
 <form action="<?=base_url();?>home/ven_coupon" method="post">
        Coupon: <input type="text" name="coupon">
        <input type="submit" name="submit" value="Apply Coupon">
 </form>
 <?php if($discount) { ?>
    <p>Discount: 10%</p>
     <p>Total: <?=$discount;?></p>
     <?php=$data['discount'];?>
     <?=$data['number'];?>
   <?php } ?>

Model

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

Controller

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

View

<form action="<?=base_url();?>home/ven_coupon" method="post">
            Coupon: <input type="text" name="coupon">
            <input type="submit" name="submit" value="Apply Coupon">
        </form>
        <?php if($discount) { ?>
            <p>Discount: <?=$data;?>%</p>
            <p>Total: <?=$discount;?></p>
        <?php } ?>

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