In the home page, all subjects should be displayed. When the subject is clicked, their respective chapters has to be displayed. For the first time, that is when the subject is not choosen, then chapters of subject id 1 has to be displayed. In the below code, should i have to call get_chapters() from index and make get_chapters() to return chapters or any other better way of doing it?
class Home extends CI_Controller {
private $data = array();
public function index()
{
$this->load->model('subjects_model');
if($query = $this->subjects_model->get_all()) {
$data['subjects'] = $query->result_array();
}
$this->load->view('home_view', $data);
}
public function get_chapters($id=null) {
$this->load->model("chapters_model");
if(!is_null($id)) {
$query = $this->chapters_model->get_where('chapters', array('subject_id'=>$id));
} else {
$query = $this->chapters_model->get('chapters');
}
if($query) {
$data['chapters'] = $query->result_array();
}
$this->load->view('home_view', $data);
}
}
Try like
public function get_chapters($id=null) {
$this->load->model("chapters_model");
if(is_null($id)) {
$id = 1;
}
$query = $this->chapters_model->get_where('chapters', array('subject_id'=>$id));
if($query) {
$data['chapters'] = $query->result_array();
}
$this->load->view('home_view', $data);
}
If the $id is null then defaltly subject id will become 1 orelse it will search with the existing subject id
public function index($id=null)
{
$this->load->model("chapters_model");
if(is_null($id)) {
$id = 1;
}
$query = $this->chapters_model->get_where('chapters', array('subject_id'=>$id));
if($query) {
$data['chapters'] = $query->result_array();
}
$this->load->view('home_view', $data);
}
Related
i have this code in my update function, when i try other value the table updates but when i used that value($field8) the table don't update.
public function edit_lead() {
$id = "3";
$field8 ="http://38.102.225.87/RECORDINGS/MP3/20170201-094849_7188352209-all.mp3";
$this->db->set('field8', $field8);
$this->db->where('id', $id);
$query = $this->db->update('table');
if($query) {
return true;
} else {
return false;
}
}
Simply send the id of the user as a parameter of this function..
public function edit_lead($ID) {
}
Hello any one can tell that how to load two function from model class in controller one method. I want to run multiple select quires in one page using codeigniter:
Controller
public function property_detail( $id )
{
$this->load->model('insertmodel');
$select1 = $this->insertmodel->find($id);
$select2 = $this->insertmodel->detail_list();
$data = array();
$this->load->view('home/property_detail', ['select1'=>$select1], ['select2'=>$select2]);
//$this->load->view('home/property_detail', ['select2'=>$select2]);
}
Model
public function find( $id )
{
$query = $this->db->from('article')->where(['id'=> $id])->get();
if( $query->num_rows() )
return $query->row();
return false;
}
public function detail_list(){
$query1 = $this->db->query("select * from article");
return $query1->result();
}
In Controller
public function property_detail( $id )
{
$this->load->model('insertmodel');
$data['select1'] = $this->insertmodel->find($id);
$data['select2'] = $this->insertmodel->detail_list();
$this->load->view('home/property_detail', $data);
}
In Model
public function find($id)
{
$query = $this->db->get_where('article', array('id' => $id), 0, 0)->get();
if( $query->num_rows() > 0 )
{
$result = $query->result_array();
return $result;
}
else
{
return false;
}
}
public function detail_list()
{
$query1 = $this->db->query("select * from article");
$result = $query1->result_array();
return $result;
}
In View
foreach ($select2 as $item) {
# your foreach lop goes here
}
As well check empty() before passing it to the foreach loop
As an alternative to #Rijin's useful answer newMethod() can make calls to the existing model methods. This might be useful if you don't want to break the interface already created for the model because you are using find($id) and detail_list() in other code.
Model:
public function find($id)
{
$query = $this->db->from('article')->where(['id' => $id])->get();
if($query->num_rows())
return $query->row();
return false;
}
public function detail_list()
{
$query1 = $this->db->query("select * from article");
return $query1->result();
}
public function newMethod($id)
{
$result['select1'] = $this->find($id);
if($result['select1'] !== FALSE)
{
$result['select2'] = $this->detail_list();
return $result;
}
return FALSE;
}
Controller:
public function property_detail($id)
{
$this->load->model('insertmodel');
$data = $this->insertmodel->newMethod($id);
$this->load->view('home/property_detail', $data);
}
Model :
public function find( $id )
{
$query = $this->db->from('article')->where(['id'=> $id])->get();
if( $query->num_rows() )
return $query->row();
return false;
}
public function detail_list()
{
$query1 = $this->db->query("select * from article");
return $query1->result();
}
Controller :
public function property_detail( $id )
{
$this->load->model('insertmodel');
$data['select1'] = $this->insertmodel->newMethod($id);
$data['select2'] = $this->insertmodel->detail_list();
$this->load->view('home/property_detail', $data);
}
I know it has already been asked, but I'm not able to solve this. Below is my control module
public function ahome()
{
$this->load->model('model');
$data['user'] = $this->model->selall('user');
$this->load->view('ahome', $data);
if ($this->input->post('del'))
{
$did = $this->input->post('chk');
// print_r($did);
for ($i = 0; $i < count($did); $i++)
{
$multi = $did[$i];
print_r($multi);
$this->model->delall("user", $multi);
// redirect("control/ahome");
}
}
}
and this is my model module
public function delall($tb,$wh)
{
$this->db->query("delete from $tb where uid in('$wh')");
}
now problem is that it's only delete single row not multiple rows
In your query string, try removing the quotes around the $wh variable.
public function delall($tb,$wh)
{
$this->db->query("delete from $tb where uid in ($wh)");
}
That should get it to work; however, you should be using CodeIgniter's query bindings. Something like this:
public function delall($tb,$wh)
{
$this->db->query("delete from $tb where uid in (?)", array($wh));
}
Refer to this answer for more details.
Try this,
controller,
public function ahome()
{
$this->load->model('model');
$data['user'] = $this->model->selall('user');
$this->load->view('ahome', $data);
if ($this->input->post('del'))
{
$did = $this->input->post('chk');
// print_r($did);
foreach ($did as $d_id)
{
$this->model->delall("user", $d_id);
// redirect("control/ahome");
}
}
}
Model:
public function delall($tb,$wh)
{
$this->db->where('id',$wh);
$this->db->delete($tb);
return true;
}
I have two functions in my model as
class Jobseeker_model extends CI_Model
{
public function __construct()
{
parent::__construct();
$this->load->database();
}
public function result_getall($id)
{
$this->db->select('*');
$this->db->from('tbl_jobseeker');
$this->db->where('tbl_jobseeker.User_id',$id);
$this->db->join('tbl_work_exp', 'tbl_jobseeker.User_id = tbl_work_exp.User_id','left');
$query = $this->db->get();
return $query->row();
}
public function select($id)
{
$this->db->select('*');
$this->db->from('tbl_qualification');
$this->db->where('tbl_qualification.User_id',$id);
$query = $this->db->get();
return $query->result();
}
}
And in my controller I have a function as
public function display()
{
$id = $this->session->userdata('user_id');
$data['row'] = $this->jobseeker_model->result_getall($id);
$res['a'] = $this->jobseeker_model->select($id);
$this->load->view('jobseeker_display.php', $data,$res);
}
It is not possible to display the view page.. I could pass two variables into my view page.right?
You can pass your any number of variables/arrays using a single array.
In Controller:
public function display() {
$id = $this->session->userdata('user_id');
$data['var1'] = $this->jobseeker_model->result_getall($id);
$data['var2'] = $this->jobseeker_model->select($id);
$this->load->view('jobseeker_display.php', $data);
}
In View:
`$var1` and `$var2` will be available.
You can pass your two variable using single srray
public function display()
{
$id = $this->session->userdata('user_id');
$data['row'] = $this->jobseeker_model->result_getall($id);
$data['a'] = $this->jobseeker_model->select($id);
$this->load->view('jobseeker_display.php', $data);
}
Views
foreach($a as $data){
// your code
}
echo $row->column_name;
Try this
public function display()
{
$id = $this->session->userdata('user_id');
$data['row'] = $this->jobseeker_model->result_getall($id);
$data['a'] = $this->jobseeker_model->select($id);
$this->load->view('jobseeker_display.php', $data);
}
The below code seems to be showing in the "view" and not via $data as it should be (I have not echoed the $data['companyName'] into my view yet but $data['pageTitle'] works fine).
Issue:
Model:
function companyName()
{
$companyName = $this->db->query("SELECT company_name FROM core");
if ($companyName->num_rows() > 0)
{
foreach ($companyName->result() as $row)
{
echo $row->company_name;
}
}
Controller:
public function index()
{
$data['companyName'] = $this->core_model->companyName();
$data['pageTitle'] = "Admin Login";
$this->load->view('admin/assets/header', $data);
$this->load->view('admin/login.php');
$this->load->view('admin/assets/footer');
}
By echoing something in the model, that echo happens when the model is processed, which is way before the view is even a twinkle in its controller's eye.
Instead, you should do something like this in your model:
function companyName()
{
$companyName = $this->db->query("SELECT company_name FROM core");
if ($companyName->num_rows() > 0)
{
$company_names = '';
foreach ($companyName->result() as $row)
{
$company_names .= $row->company_name;
}
}
return $company_names;
}
Then you can pass $data into the view and the string will be ready to echo out like you want.