public function addcategory() {
$this->load->model('product_model');
$new_category = $this->product_model->add_category($this->input->post());
if ($new_category) {
$info = array(
'message' => 'Data Saved sucess',
'value' => TRUE
);
$this->index($info);//not working
}
$this->index($info);//working without $info
}
here i need to call $this->index($info); within the if(){} but it is not working... however when i put this code outside if() it works but i cant pass $info variable via $this->index($info);
I call the function($this->index($info);) in side if then function not working at all. But if i used function outside if its get call but giving an error 'Undefined index: info'.
How to call the index() function??
Declare $info outside the if statement should work.
Then use the index function outside the statement.
public function addcategory() {
$info = null;
$this->load->model('product_model');
$new_category = $this->product_model->add_category($this->input->post());
if ($new_category) {
$info = array(
'message' => 'Data Saved sucess',
'value' => TRUE
);
}
$this->index($info);//working without $info
}
Solution1:
You can use Redirect method to redirect on index :
$this->session->set_flashdata('info', $info);
redirect('/controller_name/index');
And get the info on index method :
$info = $this->session->flashdata('info');
Solution2:
Add this Remap Function to your controller :
public function _remap($method)
{
if ($method == 'some_method')
{
$this->$method();
}
else
{
$this->index();
}
}
Your if statement is not doing anything with the variable $new_category
try.
if (isset($new_category) && !empty($new_category)) // check for the existence and value
{
$info = array(
'message' => 'Data Saved sucess',
'value' => TRUE
);
$this->index($info);
}
else
{
$info = array(
'message' => 'unsuccessful data entry',
'value' => FALSE
);
}
Related
I will try to insert & also update data using session in Codeigniter, but data not inserted into the database even its print save successfully.
Here is my controller:
public function save($user_id)
{
$this->load->model('Users');
$code=$this->input->post('code');
$name=$this->input->post('name');
$address=$this->input->post('address');
$user_data= array(
'code' =>$code,
'name'=>$name,
'address'=>$address,
'active'=>1
);
if($this->Users->save($user_data,$user_id))
{
$this->session->set_flashdata('msg',"save sucesss");
}else {
$this->session->set_flashdata('msg',"not save");
}
redirect('home');
}
& this is my model:
public function save($data,$id)
{
if (id=='') {
// code...
$this->db->insert('user',$data);
return true;
}else
{
$this->db->where('id',$id)
->update('user',$data);
return true;
}
return false;
}
Data insert if I removed if in model!
You have the model always returning true no matter the outcome of the database operation. You should use the return value from insert() or update() so the "message" reports what actually happens.
Note that the argument to save has a default value. Now you can call the save URL without an argument and it will automatically do an insert.
public function save($user_id = NULL)
{
$this->load->model('users');
$user_data = array(
'code' => $this->input->post('code'),
'name' => $this->input->post('name'),
'address' => $this->input->post('address'),
'active' => 1
);
if($this->Users->save($user_data, $user_id))
{
$msg = "save sucesss";
}
else
{
$msg = "not save";
}
$this->session->set_flashdata('msg', $msg);
redirect('home');
}
public function save($data, $id)
{
if(empty($id))
{
// code...
// insert returns TRUE on success, FALSE on failure
return $this->db->insert('user', $data);
}
// update() accepts a third argument, a "where" array
// and returns TRUE on success, FALSE on failure
return $this->db->update('user', $data, array('id' => $id));
}
Now have an accurate report on the database operations.
the first check is data is coming in save controller or not if it's not getting the data then fix it. If coming then pass it in a model in the correct format and it will definitely be inserted in the database.
use following printing data
echo $data;
var_dump($data);
print($data);
print_r($data);
First thing is to rename your model calling eg:
$this->load->model('users');
and use this to call your method:
$this->users->save($user_data,$user_id)
your model should look like this then:
public function save($data, $id) {
if ($id) {
$this->db->where('id', $id)
->update('user', $data);
return true;
}
$this->db->insert('user', $data);
return true;
}
if you want to use your flashdata on the next request, use this:
$this->session->keep_flashdata('item');
$this->session->keep_flashdata(array('item1', 'item2', 'item3'));
because flashdata is only for the next request:
CodeIgniter supports “flashdata”, or session data that will only be available for the next request, and is then automatically cleared.
I have two routes.
Route::get('/receiveSignal', 'SignalController#receiveSignal');
Route::get('/sendSignal', 'SignalController#sendSignal');
I want to simulate sending data from sendSignal to receiving signal route.
So, in sending signal function I have this:
public function sendSignal()
{
$data = ['spotid' => '421156', 'name' => 'Test', 'desc' => 'some desc', 'StartofDetection' => '2018-01-17 22:22:22'];
$dataJson = json_encode($data);
return $dataJson;
}
How can I change it to receive in receiveSignal like this:
public function receiveSignal()
{
$test = file_get_contents('php://input');
dd($test);
}
Here I should receive the json to the receiveSignal after I enter the http://localhost:8000/sendSignal. Is this possible at all?
Try something like this:
1. In your route:
Route::post('receiveSignal', 'SignalController#receiveSignal');
Route::get('sendSignal', 'SignalController#sendSignal');
In your sendSignal method
public function sendSignal()
{
$data = ['key' => 'value', 'key2' => 'value2'];
$response = http_post_fields('http://localhost:8000/receiveSignal', $data);
if (!empty($response)) {
return view('success'); // or anything else you want to return
}
else {
return view('failed');
}
}
In your receiveSignal method
public function receiveSignal(Request $request)
{
$key = $request->input('key');
$key1 = $request->input('key2');
//and so on
}
Good luck.
I think the code that I run is correct, but a moment in the run even appear error 'Call to undefined function delete_cookie ()'
Please for correction :)
in Controller Product_Ref.php
public function index()
{
$ref = $this->input->get('id');
$getIdOrder = $this->product_model->getIdOrder($ref);
if ($getIdOrder) {
$this->load->helper('cookie');
$cookie = array(
'name' => 'refProductcookie',
'value' => $ref,
'expire' => '43200'
);
$this->input->set_cookie($cookie);
echo get_cookie('refProductcookie').'<br>';
}else {
echo "Sorry this product has not been registered yet";
delete_cookie('refProductcookie');
}
}
You need to load helper cookie for both of your if-else-cases:
// load BEFORE `if`
$this->load->helper('cookie');
if ($getIdOrder) {
$cookie = array(
'name' => 'refProductcookie',
'value' => $ref,
'expire' => '43200'
);
$this->input->set_cookie($cookie);
echo get_cookie('refProductcookie').'<br>';
}else {
echo "Sorry this product has not been registered yet";
delete_cookie('refProductcookie');
}
problem is in else statement didnt load cookie helper, add this line in else statement or better top of if $this->load->helper('cookie');
I have two servers, the first works using REST and other Webs pages that are built using MVC.
I usually always run REST commands in the Controller layer, however I am with a doubt, assuming that my project (using MVC) does not use database and I'm using Controllers only to send commands to the webservice to send and receive information.
The REST this case would be the like Model?
In this case I should call the Rest within the Controllers and not create Models. eg .:
public function createProductAction() {
$rest = Rest('ws.example.com', 'PUT /items/', array(
'price' => $price,
'description' => $descricao
));
if ($response->status === 200) {
View::show('success.tpl');
} else {
View::show('error.tpl', $rest->error());
}
}
public function viewProductAction() {
$rest = Rest('ws.example.com', 'GET /items/{id}', array(
'id' => $_GET['id']
));
$response = json_decode($rest->getRespose());
if ($response->status() === 200) {
View::show('product.tpl', $response);
} else {
View::show('error.tpl', $rest->error());
}
}
or
I would have to create Models to make the calls to REST?
For example:
class ProductsModel
{
public function putItem($preco, $descricao)
{
$rest = Rest('ws.example.com', 'PUT /items/', array(
'price' => $price,
'description' => $descricao
));
//If status=200 new product is added
return $response->status() === 200;
}
public function deleteItem($id)
{
$rest = Rest('ws.example.com', 'DELETE /items/{id}', array(
'id' => $id
));
//If status=200 product is deleted
return $rest->status() === 200;
}
public function getItem($id)
{
$rest = Rest('ws.example.com', 'GET /items/{id}', array(
'id' => $id
));
if ($rest->status() === 200) {
//If status=200 return data
return json_decode($rest->getRespose());
}
return NULL;
}
}
How should I proceed?
I'm wondering what is the best message to pass back the success or failure message from the model to the controller? The success message is easy because we can pass the data back. However, for failures, we can only pass FALSE and not the callback result of the failure.
What is the best method?
Here is method one:
Here is the model:
function get_pkg_length_by_id($data) {
$this->db->where('id', $data['pkg_length_id']);
$result = $this->db->get('pkg_lengths');
if($result->num_rows() > 0 ) {
return $result->row();
}
else {
return false;
}
}
In the controller, I will do
function show() {
if(get_pkg_length_by_id($data) {
//pass success message to view
}
else {
//Pass failure message to view
}
Here is version 2:
In Model
function get_pkg_length_by_id($data) {
$this->db->where('id', $data['pkg_length_id']);
$result = $this->db->get('pkg_lengths');
if($result->num_rows() > 0 ) {
$result['status'] = array(
'status' => '1',
'status_msg' => 'Record found'
);
return $result->row();
}
else {
$result['status'] = array(
'status' => '0',
'status_msg' => 'cannot find any record.'
);
return $result->row();
}
}
In Controller
function show() {
$result = get_pkg_length_by_id($data);
if($result['status['status']] == 1) {
//pass $result['status'['status_msg']] to view
}
else {
//pass $result['status'['status_msg']] to view
}
I can't say for certain which is best. I can say that I often use choice # 2, where I pass errors from the server, often doing so in a specific form that any subclass of my controller can parse and send to the view.
Also, in your show() function, the else is extraneous, once you return, you will break out of the function , so you can just do :
if($result->num_rows() > 0 ) {
$result['status'] = array(
'status' => '1',
'status_msg' => 'Record found'
);
//the condition is met, this will break out of the function
return $result->row();
}
$result['status'] = array(
'status' => '0',
'status_msg' => 'cannot find any record.'
);
return $result->row();
Its always a good practice to do these kind of stuffs in model page.
I have made few changes on what you have done as follows:
function get_pkg_length_by_id($data)
{
$this->db->where('id', $data['pkg_length_id']);
$query = $this->db->get('pkg_lengths');
/*
Just changed var name from $result to $query
since we have a $result var name as return var
*/
if($result->num_rows() > 0 ) {
$result = $query->row_array();
/*
$result holds the result array.
*/
$result['status'] = array(
'status' => '1',
'status_msg' => 'Record found'
);
//return $result->row();
/*
This will return $result->row() only which
doesn't include your $result['status']
*/
}
else {
$result['status'] = array(
'status' => '0',
'status_msg' => 'cannot find any record.'
);
//return $result->row();
/*
This is not required.
Returning just status message is enough.
*/
}
return $result;
}