I got an error like this :
A PHP Error was encountered
Severity: Notice
Message: Undefined variable: tasks
controller
public function show() {
$data['tasks'] = array();
$data['tasks'] = $this->Tasks_model->show_task()->result();
$this->load->view('pages/all', $data);
}
view
<?php
if( isset($tasks) && ( is_array($tasks) && count($tasks)>0 ) ) {
//echo"<pre>"; print_r($tasks); die();
for($i=0;$i<count($tasks);$i++)
{ ?>
<span><?php echo $task[0]['job']; ?></span><br />
<?php } ?>
<?php }
else { ?>
<span>No tasks records found.</span>
<?php } ?>
Model
public function show_task() {
return $this->db->get('task');
}
what's wrong with my code?
Controller
public function show() {
$this->$data['tasks'] = array();
$this->$data['tasks'] = $this->Tasks_model->show_task();
$this->load->view('pages/all', $data);
}
View
//Here, you can also use for loop to display data.
<?php
if( isset($tasks) && ( is_array($tasks) && count($tasks)>0 ) )
{
//echo"<pre>"; print_r($tasks); die();
for($i=0;$i<count($tasks);$i++)
{ ?>
<span><?php echo $task[0]['job']; ?></span><br />
<?php } ?>
<?php }
else { ?>
<span>No tasks records found.</span>
<?php } ?>
EDIT =>
Model
public function show_task() {
$query = $this->db->get('task');
if( $query->num_rows() > 0 ) //Always check you are getting result or not.
{
return $query->result_array(); //result_array() returns the query result as a pure array
}
else
{
return array();
}
}
Here you are returning return $this->db->get('task'); directly. So you are not getting anythingin View.
First you have to assign $this->db->get('task') to variable e.g. $query and pass result array to controller return $query->result_array();.
Reference => https://www.codeigniter.com/userguide3/database/results.html#result-arrays
Controller:
public function show() {
$data['tasks'] = array();
$data['tasks'] = $this->Tasks_model->show_task();
$this->load->view('pages/all', $data);
}
Model:
public function show_task() {
return $this->db->get('task')->result();
}
View Page:
<?php
if( isset($tasks) && ( is_array($tasks) && count($tasks)>0 ) )
{
//echo"<pre>"; print_r($tasks); die();
for($i=0;$i<count($tasks);$i++)
{ ?>
<span><?php echo $task[0]['job']; ?></span><br />
<?php } ?>
<?php }
else { ?>
<span>No tasks records found.</span>
<?php } ?>
Related
I want to create pagination in a search bar in CodeIgniter, but I don't get actual pagination.
Controller code:
public function compatabilty(){
$this->load->model('user_model');
$this->load->model('admin_model');
$data['result'] = $this->user_model->get_compatibility();
$this->load->view('compatabilty',$data);
}
public function compatabilty_search()
{
$submit = $this->input->post('searchcompatible');
if(isset($submit)){
$new = $this->input->post('search_text');
$datas['result'] =$this->admin_model->fetch_data($new);
$this->load->view('compatabilty_search',$datas);
}
}
Model:
function fetch_data($new)
{
$this->db->select("*");
$this->db->from("au_compatible_products");
if($new != '')
{
$this->db->like('Device', $new);
}
$this->db->order_by('id', 'DESC');
$query = $this->db->get();
return $query->result();
}
View:
<div class="search-result" >
<ul><font>Serch Results for</font>
<?php foreach ($result as $key ) { ?>
<li><?php echo $key->Model_Number;?> </li>
<?php } ?>
</ul>
</div>
There is an error message when my view is being loaded. "Call to a member function result() on a non-object"
How do I fix this?
This is the loop in my View: discussion/view.php
<?php foreach ($query->result() as $result) : ?>
<tr>
<td>
<?php echo anchor('comments/index/'.$result->ds_id,$result->ds_title) . ' '
. $this->lang->line('comments_created_by') . $result->usr_name; ?>
<?php echo anchor('discussions/flag/'.$result->ds_id,
$this->lang->line('discussion_flag')) ; ?>
<br />
<?php echo $result->ds_body ; ?>
</td>
</tr>
<?php endforeach ; ?>
and this is the function index in my Comments Controller:
public function index() {
if ($this->input->post()) {
$ds_id = $this->input->post('ds_id');
} else {
$ds_id = $this->uri->segment(3);
}
$page_data['discussion_query'] = $this->Discussions_model->fetch_discussion($ds_id);
$page_data['comment_query'] = $this->Comments_model->fetch_comments($ds_id);
$page_data['ds_id'] = $ds_id;
$this->form_validation->set_rules('ds_id', $this->lang->line('comments_comment_hidden_id'), 'required|min_length[1]|max_length[11]');
$this->form_validation->set_rules('comment_name', $this->lang->line('comments_comment_name'), 'required|min_length[1]|max_length[25]');
$this->form_validation->set_rules('comment_email', $this->lang->line('comments_comment_email'), 'required|min_length[1]|max_length[255]');
$this->form_validation->set_rules('comment_body', $this->lang->line('comments_comment_body'), 'required|min_length[1]|max_length[5000]');
if ($this->form_validation->run() == FALSE) {
$this->load->view('common/header');
$this->load->view('nav/top_nav');
$this->load->view('comments/view', $page_data);
$this->load->view('common/footer');
} else {
$data = array('cm_body' => $this->input->post('comment_body'),
'usr_email' => $this->input->post('comment_email'),
'usr_name' => $this->input->post('comment_name'),
'ds_id' => $this->input->post('ds_id')
);
if ($this->Comments_model->new_comment($data)) {
redirect('comments/index/'.$ds_id);
} else {
// error
$this->data['message'] = 'Error!';
}
}
}
Hello I have created an update query using a concept that if the id is valid then it should update the data otherwise it will not do so but when any of the conditions meet it redirects and says page is redirecting to the loop why is this happening so ?
Controller
public function confirm() {
if($this->session->userdata('is_logged_in') == true) {
$invc = "#".$this->input->get('id');
if($invc == true) {
$this->data_update->update_trans($invc);
} else {
redirect('home/confirm');
}
} else {
redirect('user/login');
}
}
Here is my model file
public function update_trans($invc_id) {
$get_trans = $this->db->get_where('transaction', array("invc_id" => $invc_id));
$user_id = $get_trans->row()->userid;
$amount = $get_trans->row()->ammount;
if(!empty($user_id)) {
$check = $this->db->get_where('account', array("user_id" => $user_id));
if($check->row()->balance == true) {
$new_amount = ($amount + $check->row()->balance);
$dataupdate = array(
'user_id' => $user_id,
'balance' => $new_amount
);
$this->db->where('id', $user_id);
$this->db->update('account', $dataupdate);
$this->db->where('invc_id', $invc_id);
$this->db->update('transaction', array('status' => 'Confirmed'));
redirect('home/confirm?id='.$invc_id);
} else {
$data1 = array(
'user_id' => $user_id,
'balance' => $amount
);
$this->db->insert('account', $data1);
$this->db->where('invc_id', $invc_id);
$this->db->update('transaction', array('status' => 'Confirmed'));
redirect('home/confirm?id='.$invc_id);
}
} else {
redirect('home/confirm');
}
}
And this is my view file
<?php
require('sidebar.php');
?>
<!-- Body Content -->
<div id="body">
<?php
if($this->input->get('id') == true) {
?>
<div class="msg">
<h4>Your Payment Has been confirmed successfully</h4>
</div>
<?php } else { ?>
<div class="msg">
<h4>Payment Not confirmed</h4>
</div>
<?php } ?>
</div>
<div id="break"> </div>
</div>
I dont know why but it is giving me the same error This webpage has a redirect loop can anyone help me out with this please
In your function its going every time in else part (redirect('home/confirm');) so its showing error. Your function should be like this..
public function confirm() {
if($this->session->userdata('is_logged_in') == true) {
$invc = "#".$this->input->get('id');
if($this->input->get('id')) {
$this->data_update->update_trans($invc);
} else {
redirect('home/confirm');
}
} else {
redirect('user/login');
}
}
Hope this will solve your issue.
Try your confirm() function as like that:
public function confirm()
{
if($this->session->userdata('is_logged_in') == true) {
$id = intval($this->input->get('id'));
$invc = "#".$id;
if($id > 0) {
$this->data_update->update_trans($invc);
} else {
redirect('home/confirm');
}
} else {
redirect('user/login');
}
}
You are expecting this "#".$this->input->get('id') will return you true or false.
And more if still facing same issue, than check what are you getting in $_GET like:
if($this->session->userdata('is_logged_in') == true) {
print_r($_GET); // will return you an array
}
How do I call the static method execute and use the $result array ?
i have this code:
<?php
$input_ = [
'object_id' => $id,
];
$results_wp_temp = ArticleLoadTagsData::execute($input_);
if (
is_array($results_wp_temp) &&
!empty($results_wp_temp)
) {
echo 'tags';
foreach ($results_wp_temp as $result_wp_temp) {
?>
<span><?php echo $result_wp_temp->name; ?></span>
<?php
}
} else {
echo 'no tags';
}
?>
and this class:
<?php
class ArticleLoadTagsData {
public static function execute($input = []) {
$result = [0=>1];
return $result;
}
}
I'm trying to build a easy booking system in codeIgniter and using a database with a table called conference_rooms. I'm calling for this in my Booking_Model.phplike this:
public function __construct()
{
$this->load->database();
}
public function get_room() {
$query = $this->db->get('conference_rooms');
return $query->result_array();
}
}
To display it I'm using my Booking.php class looking like this:
public function view()
{
$data['conference_rooms'] = $this->booking_model->get_room();
if (empty($data['conference_rooms'])) {
show_404();
}
$data['title'] = $data['conference_rooms']['title'];
$this->load->view('templates/header', $data);
$this->load->view('view', $data);
$this->load->view('templates/footer');
}
And my view.php:
<h3><?php echo $conference_rooms['title']; ?></h3>
<div class="main">
<?php echo $conference_rooms['seats']; ?>
</div>
It won't find $room. What am I doing wrong?
UPDATE:
Basically changed the whole code, my view class now looks like this (changed to index in my Booking controller)
public function index() {
$this->load->helper('form');
$this->load->view('bootstrap/header');
$this->load->model('Booking_Model');
$rooms = $this->Booking_Model->get();
$rooms_form_options = array();
foreach ($rooms as $id => $room) {
$rooms_form_options[$id] = $room->title;
}
$this->load->model('Package_Model');
$packages = $this->Package_Model->get();
$packages_form_options = array();
foreach ($packages as $id => $package) {
$packages_form_options[$id] = $package->package_name;
}
$this->load->view('booking', array(
'rooms_form_options' => $rooms_form_options,
'packages_form_options' => $packages_form_options,
));
$this->load->view('bootstrap/footer');
}
And my booking.php;
<div>
<?php echo form_label('Conference Room', 'id') ; ?>
<?php echo form_dropdown('id', $rooms_form_options, set_value('id')); ?>
</div>
<div>
<?php echo form_label('Package type', 'package_id') ; ?>
<?php echo form_dropdown('package_id', $packages_form_options, set_value('package_id')); ?>
</div>
<div>
<?php echo form_label('Antal deltagare', 'number_people') ; ?>
<?php echo form_input('number_people', set_value('number_people')) ; ?>
</div>
<div>
<?php echo form_submit('preview', 'Book'); ?>
</div>
Use this
In Model
<?
function __construct()
{
$this->load->database();
}
function get_room() {
$query = $this->db->get('conference_rooms');
$result = $query->result_array();
return $result;
}
?>
In controller
function view()
{
$data['conference_rooms'] = $this->booking_model->get_room();
if (empty($data['conference_rooms']))
{
show_404();
}
else
{
$this->load->view('templates/header', $data);
$this->load->view('view', $data);
$this->load->view('templates/footer');
}
}
?>
in view
<h3><?php echo $conference_rooms[0]['title']; ?></h3>
<div class="main">
<?php echo $conference_rooms[0]['seats']; ?>
</div>
cz of $conference_rooms[0] pointing 0 is we passing data with Objective array. so we need to point data. check with print_r
MODEL
You need to row_array(); to return an array
public function get_room() {
$query = $this->db->get('conference_rooms');
$rowcount = $query->num_rows();
if( $rowcount > 0 ){
return $row = $query->row_array();
} else {
return FALSE;
}
}
CONTROLLER
public function view()
{
$conference_rooms = $this->booking_model->get_room();// asing your array to variable
if (empty($conference_rooms)) {
show_404();
} else {
$data['conference_rooms']=$conference_rooms;// pass your variable to data array to pass into view
$this->load->view('templates/header', $data);
$this->load->view('view', $data);
$this->load->view('templates/footer');
}
}
VIEW
<h3><?php echo $conference_rooms['title']; ?></h3>
<div class="main">
<?php echo $conference_rooms['seats']; ?>
</div>