1

I have a function that validates input from database. My question is how to validate the input if the current input is less than value in the database?

the view

<?php echo form_open('order/confirm');?>
    <div class="input-control text">
       <input type="text" name="paid_value" value="" placeholder=""/>
       <button class="btn-clear"></button>
    </div>
<?php echo form_close;?>

the model

function payment_validate(){
    $oid = $this->uri->segment(3);
    $paid = $this->input->post('paid_value');

    $this->db->select('*')
    ->from('order')
    ->join('payment','payment.order_id = order.order_id')
    ->where('order.order_id',$oid)
    ->where('order.total_bill >', $paid);

    $query = $this->db->get();

    if($query->num_rows() > 0){
    return $this->db->last_query(); //$query->result();
    }
    else{
    return array();
    }   
}

the controller

function confirm(){ // submit form payment
    if (!$this->ion_auth->logged_in()){ 
    redirect('auth/login');
    }else{
        if($this->nekogear->payment_validate()){ // set error message
            $data['bill'] = $this->nekogear->payment_validate();

            echo "<pre>";
            die(print_r($data, TRUE));
        //$this->session->set_userdata('refered_from', $_SERVER['HTTP_REFERER']);
        //echo "<script language='javascript'>alert('Insufficient fund.');
        //window.location='http://localhost/nekogear/index.php/cart/redirects'</script>";   
        }else{
        $data['goto'] = $this->nekogear->confirm_payment();

            echo "<pre>";
            die(print_r($data, TRUE));
        //echo "<script language='javascript'>alert('Accepted.');
        //window.location='http://localhost/nekogear'</script>";
        }
    }
}

if the inserted value is less than the current value, the print_r result is querying correctly less than current value

    Array
(
    [bill] => SELECT *
FROM (`order`)
WHERE `order`.`order_id` =  '52F89602E6E'
AND `order`.`total_bill` > 10000
)

if more than current value

Array
(
    [bill] => Array
        (
        )

)

so my guess something must be off in my if - else controller logic but I can't figure it out why its always skipping this if

if($this->nekogear->payment_validate()){
  // some error message here
}

any idea why? thanks.


Solution I added form hidden for total bill into view then call it inside my controller.

$total = $this->input->post('total_bill');
$paid = $this->input->post('paid_value');
if($paid < $total){
// error message
}else{
// success message
}

1 Answer 1

1

The problem is in your model function:

if($query->num_rows() > 0){
    return $this->db->last_query(); //$query->result();
    }
    else{
    return array(); <--- you are returning an empty array here.
    }  

Also if you want to validate a form you can use form validation library from your controller. Speaking of your case you could use a callback function.

in your controller:

function confirm(){
    $this->load->library('form_validation');
     $this->form_validation->set_rules('paid_value', 'Paid value', 'required|callback_validate_payment');
     if ($this->form_validation->run() == TRUE){ 
         // yes validation is true do something
        ......
      }
}

And specify your callback function in controller like:

public function validate_payment($str){ //$str holds the input post value
        //query here and check the value,return TRUE OR FALSE based on your condition. 
        // Also if you need you can set a custom error message here
 }

See more info here

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the idea, my current solution is to call the $total_bill into the submit form with hidden form then check whether the value is less than $total_bill value.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.