How to get all data from Category in Codeigniter - codeigniter

I am new in Codeigniter, Please help me.
My Codeigniter url is - http://localhost/framework/CodeIgniter/index.php/restaurant/patna/
I want to get all data.
I set the route -
$route['restaurant(:any)'] = "Restaurant/getRestaurantByCity/$1";
And my function in Controller is
here i want the category id where pass the cat_id in below function for get all the data of that category
public function getRestaurantByCity()
{
$rst_list = $this->Restaurant_model->get_restaurant_by_city();
}

try this
controller
$this->data['RestaurantByCity'] = $this->Restaurant_model->getRestaurantByCity();
echo "<pre>"; print_R($this->data['RestaurantByCity']);
model
public function getRestaurantByCity($ids){
$sql ="select * from categoriestableName where categories_id in($ids) order by categories_id asc";
$rs = $this->db->query($sql);
foreach($rs->result() as $record ){
$result[] = $record;
}
return $result;
}

This should work
<?php
if (!defined('BASEPATH')) exit('No direct script access allowed');
class Restaurant extends CI_Controller
{
public function __construct()
{
parent::__construct();
}
public function getRestaurantByCity($id)
{
$this->load->model('Restaurant_model');
$rst_list = $this->Restaurant_model->get_restaurant_by_city($id);
}
}

Related

Getting remarks of teacher and principal in view

I'm trying to get the comments of teacher and principal to show up in view, all to no avail.
This is a school web app where parents get to view the results of their respective children. Just below the scores is the teacher's and principal comments.
Now, these comments show up in students' view but I'm having a hard time making it show up on the parents' view.
This is the model:
Comment_model.php
<?php
if (!defined('BASEPATH'))
exit('No direct script access allowed');
class Comment_model extends CI_Model {
public function __construct() {
parent::__construct();
$this->current_session = $this->setting_model->getCurrentSession();
$this->current_session_name = $this->setting_model->getCurrentSessionName();
$this->start_month = $this->setting_model->getStartMonth();
}
public function TeacherComment($data)
{
$this->db->insert('teacher_comments', $data);
return $query = $this->db->affected_rows();
}
public function UpdateTeacherComment($data, $id)
{
$this->db->where('id', $id);
$this->db->update('teacher_comments', $data);
return $query = $this->db->affected_rows();
}
public function GetTeacherComment($student_id, $session_id)
{
$this->db->select('*');
$this->db->where('student_id', $student_id);
$this->db->where('session_id', $session_id);
return $this->db->get('teacher_comments')->row();
}
public function PrincipalComment($data)
{
$this->db->insert('principal_comments', $data);
return $this->db->affected_rows();
}
public function UpdatePrincipalComment($data, $id)
{
$this->db->where('id', $id);
$this->db->update('principal_comments', $data);
return $query = $this->db->affected_rows();
}
public function GetPrincipalComment($student_id, $session_id)
{
$this->db->select('*');
$this->db->where('student_id', $student_id);
$this->db->where('session_id', $session_id);
return $this->db->get('principal_comments')->row();
}
}
The Controller I'm battling with:
$data['teacher_comment'] = $this->Comment_model->GetTeacherComment($id, $student_session_id);
$data['principal_comment'] = $this->Comment_model->GetPrincipalComment($id, $student_session_id);
How do I properly put it in function?
The view:
<span> CLASS TEACHER'S REMARK:
<u style="text-transform: uppercase;"><?php echo $teacher_comment->teacher_comment; ?> </u>
</span><br>
<br>
<span>PRINCIPAL REMARK:
<u style="text-transform: uppercase;"><?php echo $principal_comment->principal_comment; ?></u>
</span>
Let suppose this is your controller file.
class customview extends CI_Controller {
public function __construct()
{
parent::__construct();
$this->load->model("Comment_model");
}
public function yourViewFunction()
{
// get your $id and $student_session_id
$data['teacher_comment'] = $this->Comment_model->GetTeacherComment($id, $student_session_id);
$data['principal_comment'] = $this->Comment_model->GetPrincipalComment($id, $student_session_id);
$this->load->view('your_html_view',$data); //this is the view file name without php
}
}
And now in your html view you can call like this:
print_r($teacher_comment); //you can view the array or object get from model
print_r($principal_comment); //you can view the array or object get from model

creating a model and controller to get category list from db using codeigniter

1.models/calists.php // My model file
Model file here i get the category list from database to the controller
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Catlists extends CI_Model
{
public function __construct()
{
$this->load->database(); //load database
}
public function getCategories()
{
$query = $this->db->get_where('category',array('status'=>'Enable'));
if ($query->num_rows() > 0)
{
return $query->result();
}
else
{
return array();
}
}
}
?>
2.controllers/catlist.php // controller file
Controller to get the data from model
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Catlist extends CI_Controller
{
public function __construct()
{
parent::__construct();
$this->load->model('catlists');
}
public function catlist()
{
$data['catlist'] = $this->catlists->getCategories();
$this->load->view('home', $data);
}
}
In header am printng the categories list
print_r($data['catlist']);
I am not sure what error you are getting here, but when you are loading model in controller, first letter should be capital.
$this->load->model('Catlists');
$data['catlist'] = $this->Catlists->getCategories();
In header you have to call
print_r($catlist);
The file name must match the class name.
For example
if your class is this:
class User_model extends CI_Model {
function __construct()
{
parent::__construct();
}
}
Your file will be this:
application/models/User_model.php
Here in you case your model name is
models/Calists.php
So your model file be
Model Calists.php
class Calists extends CI_Model {
function __construct()
{
parent::__construct();
}
public function getCategories()
{
$query = $this->db->get_where('category',array('status'=>'Enable'));
if ($query->num_rows() > 0)
{
return $query->result();
}
else
{
return array();
}
}
}
And you call this model file in you controller like this
Controller
public function __construct()
{
parent::__construct();
$this->load->model('calists');
}
CodeIgniter anatomy of model.

codeigniter help causing error while loading model

I am not sure whats wrong with my code.......it causing an error while loading model.......
please help...........
my controller
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Exams extends CI_Controller {
public function index(){
$ob = new exam_class();
$ob->set_exam(0,'Html exam','1','20');
$ob->create_exam();
echo 'success';
}
}
class exam_class{
private $id;
private $title;
private $catagory;
private $timeLength;
function set_exam($id,$title,$catagory,$timeLength){
$this->id = $id;
$this->title = $title;
$this->catagory = $catagory;
$this->timeLength = $timeLength;
}
function create_exam(){
$this->load->model('examModel');
$this->examModel->create_exams($title,$catagory,$timeLength);
}
}
model
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class ExamModel extends CI_Model {
public function create_exams($title,$catagory,$timeLength){
$data = array(
'title' => $title ,
'catagory' => $catagory ,
'timeLength' => $timeLength
);
$this->db->insert('exams', $data);
}
}
error
A PHP Error was encountered
Severity: Notice
Message: Undefined property: exam_class::$load
Filename: controllers/exams.php
Line Number: 26
Fatal error: Call to a member function model() on a non-object in C:\xampp\htdocs\exam\application\controllers\exams.php on line 26
You shouldn't put more than one class in a file. Controller should be something like this.
class Exams extends CI_Controller {
private $id;
private $title;
private $catagory;
private $timeLength;
public function __construct()
{
parent::__construct();
$this->load->model('examModel');
}
public function index(){
$this->set_exam(0,'Html exam','1','20');
$this->create_exam();
echo 'success';
}
function set_exam($id,$title,$catagory,$timeLength){
$this->id = $id;
$this->title = $title;
$this->catagory = $catagory;
$this->timeLength = $timeLength;
}
function create_exam(){
$this->examModel->create_exams($title,$catagory,$timeLength);
}
}
Along with #shin i want to include that go to this file
....\testproject\application\config\autoload.php
and edit this to add your models
$autoload['model'] = array('modelName1','modelName2');
and to load the models from any time from any controller . This will automatically load your models.No need to add
$this->load->model('modelName');
Tip : Keep it simple
In your controller :
public function __construct()
{
parent::__construct();
$this->load->model('examModel');
}
public function index()
{
$exam_data = $this->process_exam_data(0,'Html exam','1','20');
$insert_status = $this->examModel->create_exams($exam_data);
if($insert_status===TRUE){
echo "Exam Insert Successful!";
}
else{
echo "Exam Insert Failed";
}
}
public function process_exam_data($id, $title, $category, $timelength)
{
// Do whatever you want with the data, calculations etc.
// Prepare your data array same as to be inserted into db
$final_data = array(
'title' => $processed_title,
'catagory' => $processed_category,
'timeLength' => $processed_time
);
return $final_data;
}
And in your model :
public function create_exams($data)
{
$result = $this->db->insert('exams', $data); // Query builder functions return true on success and false on failure
return $result;
}
Your index function is the main function which does the calling, whereas all the processing work is done in the process_exam_data function.
Have a nice day :)

How to pass variables in codeigniter 2

I have following
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Hello extends CI_Controller {
var $name = 'test';
function index() {
$this->name = 'Andy';
$data['name'] = $this->name;
$this->load->view('you_view', $data); // THIS WORKS
}
function you() {
$data['name'] = $this->name;
$this->load->view('you_view', $data); // BUT THIS DOESN'T WORK
}
}
My question is how to I pass the $this->name = 'Andy'; to you() ??
Since it is being set in a different method of the controller, which equates to another request in your code, you will need to store it in a session variable to have it persist across page requests.
function index() {
$this->name = 'Andy';
$data['name'] = $this->name;
$this->session->set_userdata('name', $this->name);
$this->load->view('you_view', $data); // THIS WORKS
}
function you() {
$data['name'] = $this->session->userdata('name');
$this->load->view('you_view', $data); // BUT THIS DOESN'T WORK
}
If its a value that is part of the class you can put it in the constructor
class Hello extends CI_Controller {
public function __construct() {
parent::__construct();
// will be available to any method in the class
$this->name = 'andy';
}

Passing query result to 2nd function in Codeigniter

I'm trying to run two queries in two functions in a model on Codeigniter. The 2nd query runs based on the result of the first by passing the variable between the functions.
The first query gets the row from the 'photos' database based on the URL the user goes to. The second query is run against the 'users' database and the row is selected based on the 'userid' field from the row of the first query.
I need to be able to get all data from the rows of both queries. I get a 404 error at the moment, any ideas as how to get it working?
Code is as follows:
Model:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Viewphoto_model extends CI_Model {
public function get_photo($id)
{
$db_photos = $this->load->database('photos', TRUE);
$db_photos->select('*');
$db_photos->select("DATE_FORMAT(uploaddate, '%d/%m/%y') as uploaddate_formatted", FALSE);
$db_photos->from('photos');
$db_photos->where('approved', '1');
$db_photos->where('id', $id);
$result = $db_photos->get()->row();
$userid = $this->get_user($result->userid);
}
public function get_user($userid)
{
$db_users = $this->load->database('users', TRUE);
$db_users->select('firstname, lastname, email');
$db_users->from('useraccounts');
$db_users->where('id', $userid);
$query = $db_users->get();
return $query->row();
}
}
Controller:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Viewphoto extends CI_Controller {
public function __construct()
{
parent::__construct();
$this->load->model('viewphoto_model');
}
public function view($id)
{
$data['photo'] = $this->viewphoto_model->get_photo($id);
if (empty($data['photo']))
{
show_404();
}
$data['user'] = $this->viewphoto_model->get_user($userid);
if (empty($data['user']))
{
show_404();
}
$data['title'] = $data['photo']->title.' by '.$data['photo']->username;
$data['meta_description'] = $data['photo']->description;
$data['directory'] = 'sub';
$this->load->view('templates/header', $data);
$this->load->view('viewphoto/viewphoto', $data);
$this->load->view('templates/footer', $data);
}
}
You seem to have a slight logic issue. Your get_photo() function should return the photo object, which your controller can then use in the call to get_user()...
public function get_photo($id)
{
$db_photos = $this->load->database('photos', TRUE);
$db_photos->select('*');
$db_photos->select("DATE_FORMAT(uploaddate, '%d/%m/%y') as uploaddate_formatted", FALSE);
$db_photos->from('photos');
$db_photos->where('approved', '1');
$db_photos->where('id', $id);
return $db_photos->get()->row();
}
Then in your controller....
public function view($id)
{
$data['photo'] = $this->viewphoto_model->get_photo($id);
if (empty($data['photo']))
{
show_404();
}
// Pass the photo's userid here
$data['user'] = $this->viewphoto_model->get_user($data['photo']->userid);
if (empty($data['user']))
{
show_404();
}
// ...
}

Resources