Я пытаюсь передать переменную (ans) из моего контроллера (Add) в мое представление (add), но получаю ошибку: undefined variable.
A PHP Error was encountered
Severity: Notice
Message: Undefined variable: ans
Filename: views/add.php
Line Number: 45
Вот мой контроллер (Add.php)
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Add extends CI_Controller {
public function __construct()
{
parent::__construct();
$this->load->library('form_validation');
$this->load->helper('form', 'date(%d-%m-%y)');
$this->load->database();
$this->load->model('add_model');
}
function index()
{
$this->load->view('add');
}
function date()
{
$datestring = '%Y %m %d - %h:%i %a';
$time = time();
echo mdate($datestring, $time);
}
function calculation()
{
$datestring = 'Day: %d Month: %m Year: %Y - %h:%i %a';
$time = time();
$date=mdate($datestring, $time);
//'user_registered' => mdate('%Y-%m-%d %H:%i:%s', now())
//echo mdate($datestring, $time);
if (isset($_POST['add']))
{
//$ans = array('ans' => $ans);
$number2=200;
$ans =$_POST['number1'] * $number2;
$data=array( 'number1'=>$_POST['number1'],
'number2'=>$number2,
'ans'=>$ans,
'date'=>mdate('%Y-%m-%d %H:%i:%s', now())
);
//$ans = array('ans' => $ans);
}
$this->load->library('form_validation');
$this->form_validation->set_rules('number1', 'Number1', 'required|trim' );
//$this->form_validation->set_rules('number2', 'Number2', 'required|trim' );
if ($this->form_validation->run()) {
$data = array('number1' => $this->input->post('number1'),
'answer' => $ans,
'date' => mdate('%Y-%m-%d %H:%i:%s', now())
);
$id=$this->add_model->insert($data);
if($id > 0)
{
$this->session->set_flashdata('message',' stored in db');
echo $ans;
}else{
$this->session->set_flashdata('message',' not stored in db');
//$this->load->view('add');
}
}
$this->load->view('add', $data);
}
}
Мой взгляд (add.php)
<!DOCTYPE html>
<html>
<head>
<title>CustomerRegistrationForm</title>
<link rel="stylesheet" type="text/css" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/css/bootstrap.min.css">
<!--<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/js/bootstrap.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>-->
</head>
<body>
<div class="container">
<br />
<h3 align="center">Order With Us Now</h3>
<br />
<div class="panel panel-default">
<div class="panel-heading">Order Form
</div>
<div class="panel-body">
<?php
if($this->session->flashdata('message'))
{
echo '
<div class="alert alert-success">'.$this->session->flashdata("message").'
</div>';
}
?>
<form method="post" action="<?php echo base_url(); ?>add/calculation">
<div class="form-group">
<label>Enter quantity</label>
<input type="number" name="number1" id="number1" class="form-control" placeholder="Enter the quantity(minimium 50 kgs)" value="<?php echo set_value('number1'); ?>" />
<span class="text-danger"><?php echo form_error('number1'); ?></span>
</div>
<div>
<?php echo set_value('number2'); ?>
</div>
<div class="form-group">
<label>Answer</label>
<p class="text-success">
<?php echo $ans; ?>
</p>
</div>
<div class="form-group">
<input type="submit" name="add" value="Add" class="btn btn-info" /> <a href="<?php echo base_url(); ?>welcome">HOME</a>
</div>
</form>
</div>
</div>
</div>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/js/bootstrap.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
</body>
</html>
Моя модель (Add_model.php)
<?php
class Add_model extends CI_Model
{
function insert($data)
{
$this->db->insert('calculation', $data);
return $this->db->insert_id();
//return $this->db->insert('users', $data);
}
}
?>
любезно помогите мне с этимошибка.