I am fairly new to codeigniter.
I have the following code in my views/check_availability/index.php file:
<?php $type = $_POST['type']; ?>
Create Reservation
<?php foreach ($check_availability as $check_availability_item): ?>
<h2><?php echo $check_availability_item['room_type'] ?></h2>
<div id="main">
<?php echo $check_availability_item['room_description'] ?>
</div>
<p>View article</p>
<?php endforeach ?>
This is my code under models/check_availabiliy_model.php file
public function check_availability($type = FALSE)
{
if ($type === FALSE)
{
$query = $this->db->get('lf_rooms');
return $query->result_array();
}
$query = $this->db->get_where('lf_rooms', array('type' => $type));
return $query->row_array();
}
This is the code in my controllers/check_availability.php file
public function index()
{
$data['check_availability'] = $this->check_availability_model->check_availability();
$data['title'] = 'Available Roooms';
$this->load->view('templates/header', $data);
$this->load->view('check_availability/index', $data);
$this->load->view('templates/footer');
}
Under views/check_availability/index.php file I want to filter the room_type field when I call the check_availability function based on the value from POST method like $type = $_POST['type'].
Currently this is the code that I used to query all the record from lf_rooms table:
<?php foreach ($check_availability as $check_availability_item): ?>
but how can I pass the value of $type variable to check_availability function?
You should get the POST parameter in the controller, query the database accordingly and only display the results in the view. The view should not hold any logic.
public function index()
{
$type = $this->input->post('type', TRUE);
$data['check_availability'] = $this->check_availability_model->
check_availability($type);
}
Related
controller.php
$data["category_products"] = $this->product_model->get_products_by_category_id();
model.php
//get products by category ID
public function get_products_by_category_id()
{
$lc_key = get_location_cache_key($this);
$key = "special_offers_lang_" . $this->selected_lang->id;
$result_cache = get_cached_data($this, $key, "pr");
if (!empty($result_cache)) {
if (!empty($result_cache[$lc_key])) {
return $result_cache[$lc_key];
}
} else {
$result_cache = array();
}
$this->build_query();
$this->db->where('products.category_id', 1);
$this->db->order_by('products.created_at', 'DESC')->limit(20);
$result = $this->db->get('products')->result();
$result_cache[$lc_key] = $result;
set_cache_data($this, $key, $result_cache, "pr");
return $result;
}
view.php
<?php foreach ($category_products as $product): ?>
<?php endforeach ?>
with this function I can get products from category ID 1.
is any way use this one function and pass from view to model variable which category I need to get in foreach?
or anyother way?
example some like:
<?php foreach ($category_products[1] as $product): ?>
<?php endforeach ?>
or
<?php foreach ($category_products[2] as $product): ?>
<?php endforeach ?>
#update:
view
<?php foreach ($this->product_model->get_products_by_category_id(1) as $product): ?>
controller:
$data["category_products"] = $this->product_model->get_products_by_category_id($id);
model:
//get products by category ID
public function get_products_by_category_id($id)
{
$lc_key = get_location_cache_key($this);
$key = "special_offers_lang_" . $this->selected_lang->id;
$result_cache = get_cached_data($this, $key, "pr");
if (!empty($result_cache)) {
if (!empty($result_cache[$lc_key])) {
return $result_cache[$lc_key];
}
} else {
$result_cache = array();
}
$this->build_query();
$this->db->where('products.category_id', $id);
$this->db->order_by('products.created_at', 'DESC')->limit(20);
$result = $this->db->get('products')->result();
$result_cache[$lc_key] = $result;
set_cache_data($this, $key, $result_cache, "pr");
return $result;
}
this working correct and give me correct output. But also I get issue:
Message: Undefined variable: id
Filename: controllers/Home_controller.php
when I delete ID from :
$data["category_products"] = $this->product_model->get_products_by_category_id();
then website down:
Type: ArgumentCountError
Message: Too few arguments to function Product_model::get_products_by_category_id(), 0 passed in /home/design/domains/public_html/application/controllers/Home_controller.php on line 34 and exactly 1 expected
I wouldn't advise passing an id from the view to the model (the view should not interact with the model directly), but you can pass an id from the controller to the model to specify which category you want, and then pass the result to the view.
Change the get_products_by_category_id function to accept an $id argument, and use that $id in the WHERE clause:
model.php
//get products by category ID
public function get_products_by_category_id($id)
{
$lc_key = get_location_cache_key($this);
$key = "special_offers_lang_" . $this->selected_lang->id;
$result_cache = get_cached_data($this, $key, "pr");
if (!empty($result_cache)) {
if (!empty($result_cache[$lc_key])) {
return $result_cache[$lc_key];
}
} else {
$result_cache = array();
}
$this->build_query();
$this->db->where('products.category_id', $id);
$this->db->order_by('products.created_at', 'DESC')->limit(20);
$result = $this->db->get('products')->result();
$result_cache[$lc_key] = $result;
set_cache_data($this, $key, $result_cache, "pr");
return $result;
}
Then in the controller, pass the id of the category you want to the function:
controller.php
$data["category_products"] = $this->product_model->get_products_by_category_id(1); // for category ID 1
or
$data["category_products"] = $this->product_model->get_products_by_category_id(2); // for category ID 2
The view stays the same.
I have a web application built using codeigniter. What I'm trying to do is show a summary of results on my view. In my controller I have the below code
function index() {
$data['summery'] = $this->Main_model->get_summery();
foreach ($data['summery'] as $d) {
$data['total_week'] = $this->Main_model->get_total_week($d['i_id']);
$data['total_month'] = $this->Main_model->get_total_month($d['i_id']);
}
}
The problem that im having is each summery record has total weekly count and a monthly count based on the i_id
in my view i loop through the summery and it display the summery but when i echo $total_week or echo $total_month its always returns NULL
$data['total_week'] = $this->Main_model->get_total_week($d['i_id']);
$data['total_month'] = $this->Main_model->get_total_month($d['i_id']);
When i do a var_dump on the above variables it shows the number, but when i try to echo it on the view it returns NULL
I hope I under stood correct Try something like this
function index() {
$this->load->model('main_model');
$data['summery'] = array();
$summery = $this->main_model->get_summery();
foreach ($summery as $d) {
$data['summery'][] = array(
'total_week' => $this->main_model->get_total_week($d['i_id']),
'total_month' => $this->main_model->get_total_month($d['i_id'])
);
}
$this->load->view('the_view', $data);
}
View
<?php foreach ($summery as $something) {?>
<?php echo $something['total_week'];?>
<?php echo $something['total_month'];?>
<?php }?>
I've been working on a method to email the users data to themselves and I'm having trouble printing data in the view. I can access all the data in bulk but I would like to format it so it displays within a HTML table for the user.
Model:
function getEmailData($usersid){
$this->db->select('*');
$this->db->from('symptom');
$this->db->where('userid', $usersid);
$symptomState = $this->db->get();
$result = $symptomState->result_array();
return $result;
}
function getEmailBMData($usersid){
$this->db->select('*');
$this->db->from('bowelmovement');
$this->db->where('userid', $usersid);
$bmState = $this->db->get();
$result = $bmState->result_array();
return $result;
}
Controller:
function sendDataAsEmail(){
$uid = $this->session->userdata('id');
$uname = $this->session->userdata('username');
$uemail = $this->input->post('email_data');
$data = array(
'userName'=> $uname,
);
$data['symptomData'] = $this->poomonitormodel->getEmailData($uid);
$data['bmData'] = $this->poomonitormodel->getEmailBMData($uid);
$data['symptomCount'] = $this->poomonitormodel->getTotalSymptomCount($uid);
$data['bmCount'] = $this->poomonitormodel->getTotalBMCount($uid);
var_dump($data);
$contents = $this->load->view('pooemail.php',$data,TRUE);
$this->email
->from('#', '#')
->to($uemail)
->subject('#')
->message($contents)
->set_newline("\r\n")
->set_mailtype('html');
$this->email->send();
}
View:
<p>
<?php foreach($symptomData as $data){
foreach($data as $nestdata){
echo $nestdata;
};
};?>
</p>
<p>
<?php foreach($bmData as $bmdata){
foreach($bmdata as $nestbmdata){
echo $nestbmdata;
};
};?>
</p>
Currently echo $nestdata outputs the data as a complete string like this, but I would like to access a single attribute like $nestdata['symptomdate']:
462016-04-1402:00burningOesophagus7vomitting 562016-04-1405:00tinglingGallBladder3vomitting 662016-04-1410:00shootingSmallIntenstine8vomitting 1362016-04-2016:47crampGallBladder1Gurgling Noise 1462016-04-2016:58tinglingRectum1Strange tingling sensation in the raer 1962016-04-2017:41crampIleum2Cramping 2062016-04-2017:42crampAnus7It whistles 2162016-04-2017:42crampRectum7It also whistles
But I would like to access a specific value so that I can put each bit of data into a table data cell so it's easier to read for the user.
Thanks!
The keys in $data will be the name of the var in the view. So, $data['symptomCount'] in the controller will be accessed as $symptomCount in the view.
What you do with it depends on its type. If $symptomCount is an object (class) then $x = $symptomCount->some_property. If it is an array - $symptomCount['some_index']
Iterate them like so
foreach($symptomData as $symptom){
echo $symptom['symptomdate'];
}
So im running a query and trying to return a result set back into my view however when I do <?php foreach ($form_records as $form_record) : ?><?php echo $form_record['fname']; ?> <?php endforeach; ?> I get Undefined variable: form_records. What am i doing wrong when I run the query (Select fname, lname from form_manager where username = "tester") I get a row returned successfully so the query is working.
Model
function retrieve_form_record($username) {
$this->db->select('fname,lname');
$this->db->from('form_manager');
$this->db->where('username', $username);
$query = $this->db->get();
if ($query->num_rows() > 0)
{
$form_records= $query->result_array();
return $form_records;
}
else {
return false;
}
}
}
Controller
function load_saved_forms() {
$username = $this->tank_auth->get_username();
$form_records = $this->Form_model->retrieve_form_record($username);
$this->load->view('welcome',$form_records);
}
The problem is how you pass parameter to view, change this line:
$this->load->view('welcome',$form_records);
to :
$data['form_records'] = $form_records; //<<--pass data to view right way.
$this->load->view('welcome',$data);
and the then you can perform this inside view:
<?php foreach ($form_records as $form_record) : ?>
<?php echo $form_record['fname']; ?>
<?php endforeach; ?>
Note that $form_records in view is an index of array $datayou passed at $this->load->view('welcome',$data);
The $form_records is probably an array and looking at the codeigniter's doc it will turn those into array elements when it is passed to the view.
See this link: http://ellislab.com/codeigniter/user-guide/general/views.html
But it is saying if you did this in your controller:
$data['todo_list'] = array('Clean House', 'Call Mom', 'Run Errands');
$this->load->view('blogview', $data);
You can use that "todo_list" like this in your view:
<?php foreach ($todo_list as $item):?>
<li><?php echo $item;?></li>
<?php endforeach;?>
I am trying to call a model from my controller, which in turn generates data from a view, saves it as content and returns it to the controller, which then adds it to a template I have.
Whats happening is that $content = $this->load->view('left/profile', $c_data); is printing the data instead of saving it in variable $content
Here is my code:
Controller:
function index($page = '1-Welcome to')
{
if (!$this->tank_auth->is_logged_in()) {
//display non-login page
redirect('/auth/login/');
} else {
//user information
$user['user_id'] = $this->tank_auth->get_user_id();
$user['username'] = $this->tank_auth->get_username();
//general page data
$main['title'] = $this->page_model->make_title(ucfirst($page));
//template data
$template['head'] = $this->load->view('templates/head', $main, TRUE);
//get left content
$c_data['make_profile'] = $this->left_model->make_profile($user);
//combine into one variable
$data['template'] = $template;
$data['page'] = $page;
$data['user'] = $user;
$data['left'] = $c_data;
print_r($data);
$this->load->view('main_template', $data);
}
}
Focus on $c_data['make_profile'] = $this->left_model->make_profile($user);
Here is make_profile
public function make_profile($user)
{
$user_id = $user['user_id'];
$query = $this->db->query(" SELECT location FROM user_profiles AS up
INNER JOIN avatars AS a
ON up.avatar_id = a.id
WHERE user_id='$user_id'");
$c_data['avatar_loc'] = $query->row_array();
$content = $this->load->view('left/profile', $c_data);
$content .= "hello";
return $content;
}
And here is my profile view:
<div id="profile">
<div id='avatar'><img src="<?php echo $avatar_loc['location'] ?>" alt="avatar_user"/></div>
<div id="profile_pop"></div>
</div>
Any idea why it isn't working? Thanks
Return the data from the view as a string:
$this->load->view('left/profile', $c_data, TRUE);
Read here (bottom of the page).