Update all table rows on database by using one query in codeigniter - php

I have table in my database named settings
and it have 3 Fields id, key, value
and i need to update all settings in one page using one query
this is my settings model
class Settings_model extends CI_Model{
/*
* Get global settings
*/
public function get_global_settings() {
$query = $this->db->get('settings');
$result = $query->result();
return $result;
}
/* Update setting */
public function update($data, $id) {
$this->db->where('id', $id);
$this->db->update('settings', $data);
return true;
}
}
and this is my controller
class Settings extends MY_Controller{
public function __construct() {
parent::__construct();
//Access control
if(!$this->session->userdata('logged_in')) {
redirect('admin/login');
}
}
public function index(){
// Get Categories
$data['settings'] = $this->Settings_model->get_global_settings();
// Load View
$data['main_content'] = 'admin/settings/index';
$this->load->view('admin/layouts/main',$data);
}
public function edit($id) {
// Calidation Rules
$this->form_validation->set_rules('value','Name','trim|required|xss_clean');
$data['settings'] = $this->Settings_model->get_group($id);
if($this->form_validation->run() == FALSE) {
// View
$data['main_content'] = 'admin/settings/edit';
$this->load->view('admin/layouts/main',$data);
} else {
// Create Articles Data Array
$data = array (
'value' => $this->input->post('value')
);
// Articles Table Insert
$this->Settings_model->update($data, $id);
// Create message
$this->session->set_flashdata('setting_saved', 'Your setting has been saved');
// Redirect page
redirect('admin/settings');
}
}
}
but it only update one row by id
i need to make a foreach loop to update all rows

Related

PHP: can't insert data to database in web hosting, but in local it's working using CI

I'm writing PHP using CI. I inserted data, but got an error like this:
Code:
class Recruit extends CI_Controller {
public function __construct() {
parent::__construct();
$this->load->database();
$this->load->model('Resume_model');
$this->load->helper(array('form','url'));
}
public function index() {
$this->data['posts'] = $this->Resume_model->get_resume();
$this->load->view('recruit', $this->data);
}
public function resume_form() {
echo 'OK';
$test = $this->input->post('type');
echo 'test:',$test;
echo 'KO';
$save = array(
'type' =>$this->input->post('type'),
'fullname' =>$this->input->post('fullname'),
'tel' =>$this->input->post('tel'),
'location' =>$this->input->post('location')
);
$this->Resume_model->saveResume($save);
redirect(base_url()."recruit");
}
Class Resume_model extends CI_Model {
function saveResume($data) {
{
$this->db->insert('resume', $data);
$resume_id = $this->db->insert_id();
}
return $resume_id;
}
function get_resume() {
$this->db->select("fullname,type");
$this->db->from('resume');
$query = $this->db->get();
return $query->result();
}
function getResume() {
$query = $this->db->query('SELECT * FROM resume');
if($query->num_rows() > 0 ) {
return $query->result();
} else {
return array();
}
}
public function deleteResume($id) {
$this -> db -> where('id', $id);
$this -> db -> delete('resume');
}
Make your fields in the DB to allow NULL input.
The reason is simple, your localhost environment and your online environment are different. Different MySQL configurations or different versions causes these errors.
Sometimes there is a need for putting in an empty record into a reference table for future use or because of the order of your coding event but to fix it you have to either put data into your columns upon inserting or setting your column (type) to 'NULL' or 'NULLABLE' from the DB.

Data doesn't display in Codeigniter

I am new learner of CI.
I want to display data from database. but it doesn't work.
Code in controller :
function __construct()
{
parent::__construct();
$this->load->database();
$this->load->model("user_model");
$this->lang->load('basic', $this->config->item('language'));
// redirect if not loggedin
if(!$this->session->userdata('logged_in')){
redirect('login');
}
$logged_in=$this->session->userdata('logged_in');
if($logged_in['base_url'] != base_url()){
$this->session->unset_userdata('logged_in');
redirect('login');
}
}
public function index($limit='0')
{
$logged_in=$this->session->userdata('logged_in');
if($logged_in['su']!='1'){
exit($this->lang->line('permission_denied'));
}
$data['limit']=$limit;
$data['title']=$this->lang->line('userlist');
// fetching user list
$data['result']=$this->user_model->user_list($limit);
$this->load->view('header',$data);
$this->load->view('user_list',$data);
$this->load->view('footer',$data);
}
public function teachercontrol()
{
$logged_in=$this->session->userdata('logged_in');
$sut='2';
$this->db->where('su',$sut);
// fetching user list
$data['teacher']=$this->user_model->teachermodel();
$this->load->view('header',$data);
$this->load->view('user_list',$data);
$this->load->view('footer',$data);
}
my model :
function teachermodel(){
$this->db->order_by('savsoft_users.uid','desc');
$this -> db -> join('savsoft_group', 'savsoft_users.gid=savsoft_group.gid');
$query=$this->db->get('savsoft_users');
return $query->result_array();
}
I try tro print in view, but it doesn't show any data :
<?php print_r($teacher)?>
What is the mistake?
while, the index function in controller (above the teachercontroller function) works well
Thanks
Put where condition in modal from teacher controller:
public function teachercontrol()
{
$logged_in=$this->session->userdata('logged_in');
$sut='2';
// fetching user list
$data['teacher']=$this->user_model->teachermodel($sut);
$this->load->view('header',$data);
$this->load->view('user_list',$data);
$this->load->view('footer',$data);
}
my model :
function teachermodel($sut = ""){
if($sut != "") {
$this->db->where('savsoft_users.su',$sut);
}
$this->db->join('savsoft_group', 'savsoft_users.gid=savsoft_group.gid', 'left');
$this->db->order_by('savsoft_users.uid','desc');
$query=$this->db->get('savsoft_users');
return $query->result_array();
}

codeigniter how to send query->num_rows from model to controller to view

I'm new in codeigniter, I'm having a challenge with num_rows on view page
Here's the sample of the code - Updated
Model
public function __construct()
{
parent::__construct();
}
public function total_referred_in()
{
$query = $this->db->query('SELECT * FROM daily_out_patient');
return $query->num_rows();
}
Controller
public function __construct()
{
parent::__construct();
$this->load->helper('url');
$this->load->model('Referrals_form_model', 'referral'); /* LOADING MODEL * Referral_form_model as referral */
}
public function index()
{
$this->load->helper('url');
$this->load->view('referrals_form_view');
}
public function referral_in()
{
$data['total_referred_in'] = $this->load->referral->total_referred_in(); //get referral data
$this->load->view("referral_form_view", $data);
}
}
View
<span><?php
echo $query->num_rows();
?></span>
when i run the code, it tell me undefined variable "query"
Please help.
Thank you
Please change your code as below.
MODEL:
public function total_referred_in()
{
$query = $this->db->query('SELECT * FROM daily_out_patient');
return $query->num_rows();
}
Controller
public function referral_in()
{
$data['total_referred_in'] = $this->load->referral->total_referred_in(); //get referral data
$this->load->view("referral_form_view", $data);
}
View :
<span>
<?php
echo $total_referred_in;
?>
</span>
As you can see in Controller line :
$this->load->view("referral_form_view", $data);
$data here is the data array you are passing to your view. In your view you can access data in $data variable by using $data variable's KEY as php variable(In our case its "total_referred_in").
You aren't returning anything from your function, you're just echoing. Try this:
public function total_referred_in()
{
$query = $this->db->query('SELECT * FROM daily_out_patient');
return $query->num_rows();
}
Controller:
public function referral_in()
{
$data['num_rows'] = $this->load->referral->total_referred_in(); //get referral data
$this->load->view("referral_form_view", $data);
}
And then in your view:
<span><?=$num_rows?></span>

How to get and display database table value in codeigniter error was coming

I am a new codeigniter developer and have encountered an error. I have stored some value in database and want to display it in view page. Help me please.
Controller
function get_user_value() {
$data['query'] = $this->user_m->user_data_set($query);
$this->load->view('user_settings', $data); }
Model
public function user_details()
{
// $query = $this->db->select('*')->from('user')->get();
$query = $this->db->get('user');
return $query->$result();
}
Try this:
Note call first the model to your controller
add first this function _construct()
function __construct() {
parent::__construct();
$this->load->model("model_file_name", "model_name_alias");
//Example $this->load->model("user_model","user");
//the "user_model" is the file name in your model folder
}
function get_user_value() {
$data['query'] = $this->model_name_alias->user_details();
$this->load->view('user_settings', $data);
}

How do I display active record in codeigniter with multiple controllers?

I am building my comment module and trying to get the active records to display on view. Seems simple however I have a lot going on and using a separate model and controller then what the view is being generated from.
So I have a profile page ( where I'm trying to get the results to display )
Controller:
public function profile()
{
$this->load->helper('url'); //Include this line
$this->load->helper('date');
$this->load->library('session');
$this->load->library('form_validation');
$session_id = $this->session->userdata('id'); //Ensure that this session is valid
//TRYING TO MAKE A VARIABLE WITHT THE $_GET VALUE
$user_get = $this->uri->segment(3); // Modify this line
// LOAD THE CORRECT USER
$this->load->model('account_model');
$user = $this->account_model->user($user_get); //(suggestion) you want to pass the id here to filter your record
$data['user'] = $user;
$data['session_id'] = $session_id;
if($user_get != $user['id'] || !$user_get)
{
$data['main_content'] = 'account/notfound';
$this->load->view('includes/templates/profile_template', $data);
}
else
{
if($user_get == $session_id) //Modify this line also
{
$data['profile_icon'] = 'edit';
}
else
{
$data['profile_icon'] = 'profile';
}
$sharpie = $this->sharpie($user);
$data['sharpie'] = $sharpie;
$data['main_content'] = 'account/profile';
$this->load->view('includes/templates/profile_template', $data);
}
}
Now I have a new controller for my comments I'm trying to display:
public function airwave() {
$this->load->helper('date');
$this->load->library('session');
$airwave_get = $this->uri->segment(3);
$this->load->model('community_model');
$airwave = $this->coummunity_model->airwave($airwave_get);
$data['airwave'] = $airwave;
$data['main_content'] = 'account/profile';
$this->load->view('includes/templates/main_page_template', $data);
}
with this model:
public function airwave($id=null)
{
if(is_null($id)) {
$session = $this->session->userdata('is_logged_in');
$commenter_id = $this->session->userdata('id');
}else{
$commenter_id = intval($id);
}
$query = $this->db->query("SELECT * FROM airwaves_comments WHERE from_id=$commenter_id");
if($query->num_rows()==1)
{
$data = $query->result_array();
return $data[0];
//above returns the single row you need to the controller as an array.
//to the $data['user'] variable.
}
}
and trying to display it on this view ( which is generated by the profile function )
<div class="profile_airwave_comment_text">
<?php echo $airwave['comment'];?>
</div>
I just can't seem to get it to pass the array variable I've created properly to that view?
thanks in advance.
The first glaring thing I see wrong is in you model's airwave function:
if($query->num_rows()==1)
{
$data = $query->result_array();
return $data[0];
You are assuming that there will only be one result in your query, so if there are more or less than one, your $data array will bever be set.
Also, even then you are only returning the first element in the array. I am not seeing anywhere in your question that you want to return only one.
Finally, in your view, if you now return the full array, and not only one element, you will have to do a foreach statement.
EDIT: please see suggested solution.
In your model, don't worry about the check for the amount of data in the result array. This will come later. Simply do:
return $query->result_array();
Now, keep your controller as is, but adapt your view:
<div class="profile_airwave_comment_text">
<?php
if (isset($airwave) && is_array($airwave) && !empty($airwave))
{
foreach ($airwave as $wave)
{
echo $wave['comment'];
}
}
else
{
echo 'No comments found...';
}
?>
</div>
This should work, and if not, at least let you know that no comments were fond, and thus check your database.
HTH!
If your using HMVC, simply call the module from inside the profile view, ie:
echo Modules::run('comments/profileComments', $user->id);
Else:
You would need to create a new method in the loader class, to load in the controller seperate from routing.
If your really after a quick fix until you come up with a better alternative, you could(depending if both controllers live in the same directory) just include the 'comments' controller in your 'profile' controller and try something like this:
{Cons: CI Pagination wont work!}
application/controllers/profile.php
include 'Comments' . EXT;
class Profile extends CI_Controller{
public function __construct ()
{
parent::__construct () ;
}
public function index( $user_id ){
$commentsPartialView = call_user_func_array(array(new Comments(), "getProfileComments"), array($user_id));
return $this->load->view($this->template, array(
'view' => 'profiles/_index',
'comments' => $commentsPartialView
));
}
}
-
application/controllers/Comments.php
class Comments extends CI_Controller{
public function __construct ()
{
parent::__construct () ;
}
public function getProfileComments( $user_id ){
$user_comments = ''; //pull from DB
//set third param to TRUE for partial view, without main template
return $this->load->view('comments/show', array(
'user_comments' => $user_comments
), TRUE);
}
}

Categories