简体   繁体   English

匹配数据库中的值,并使用codeigniter获取该行中的所有数据

[英]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 在模型而不是控制器中检查$query的行数
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 } ?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在数据库中搜索值,并使用Codeigniter获取此行中的所有值 - Search a values in Database and get all values in this single row using Codeigniter 列计数与使用codeigniter插入数据库的第1行的值计数不匹配 - Column count doesn't match value count at row 1 inserting into the database using codeigniter 如何在codeigniter中从数据库中获取所有数据 - How to get all data from database in codeigniter 使用 codeigniter 在数据库上获取具有相同值的列 - get column with the same value on the database using codeigniter 如何从同一卷获取一列中的总和值并使用codeigniter显示所有行? - How to get sum value in a column from same roll and show all row using codeigniter? 如何使用带有邮递员的Codeigniter中的API从具有特定ID的数据库中获取数据,而不是所有数据 - How to get data from database with perticular id not all data using API in codeigniter with postman 如何在Codeigniter中获取单行中的所有行数据 - How to get all rows data in single row in codeigniter 在 CodeIgniter 中使用 AJAX 从数据库中获取单行? - Get a single row from the database using AJAX in CodeIgniter? 使用CodeIgniter删除数据库中的行 - Delete row in database using CodeIgniter 在preg_match_all之后,如果值在另一行中,如何获取该值 - after preg_match_all, how to get the value if it is in another row
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM