Display 2 tables on codeigniter - php

im new to codeigniter and php, need some enlightment to display 2 tables with mvc method. just displaying two tables in one page (camera and visitor table). here is my code
Model :
function m_report() {
$camera = $this->db->get('camera');
return $camera->result();
$report = $this->db->get('visitor');
return $report->result();
}
View:
<?php foreach($data1 as $report){ ?>
<tr>
<td><?php echo $report->Country; ?></td>
<td><?php echo $report->Days; ?></td>
</tr>
<?php } ?>
<?php foreach($data_camera as $camera){ ?>
<tr>
<td><?php echo $camera->cameratype; ?></td>
</tr>
<?php } ?>
Controller :
function report(){
$data['data_camera']=$this->m_data->m_report();
$data1['data1']=$this->m_data->m_report();
$this->load->view('v_report',$data,$data1);
}
the problem is, i can display camera table but visitor got error Message: Undefined variable: data1
Can anyone help me to figure it out? Much appreciate

You can only return ONE thing from a method - once you return something, execution of code stops.
function m_report() {
$camera = $this->db->get('camera')->result();
$report = $this->db->get('visitor')->result();
return array_merge($camera, $report);
}
Now you get an array with all the results from both "camera" and "visitor". You can specify it out if you'd like with an associative array.
function m_report() {
$data['camera'] = $this->db->get('camera')->result();
$data['visitor'] = $this->db->get('visitor')->result();
return $data;
}

you can not make 2 returns in one method
function m_report() {
$camera = $this->db->get('camera');
return $camera->result();
$report = $this->db->get('visitor');
return $report->result();
}
i think it is better to make each query in single function
function m_camera_report() {
$camera = $this->db->get('camera');
return $camera->result();
}
function m_visitor_report() {
$report = $this->db->get('visitor');
return $report->result();
}
then call them separately in controller
function report(){
$data['data_camera']=$this->m_data->m_camera_report();
$data['data_visitor']=$this->m_data->m_visitor_report();
$data1['data1']=$this->m_data->m_report();
$this->load->view('v_report',$data,$data1);
}

Related

Message: Illegal String Offset Upgrade from Codeigniter2x Version to Codeigniter 3x version

I recently switched my project package from Codeigniter 2X version to Codeigniter 3X version.
Earlier all my code worked fine but now with new package its showing some minute errors where I am not able to run my code.
The following is the error:
Message: Illegal String Offset:
My Controller:
public function proflist(){
$data = "";
$this->load->model('feedbackmodel');
$data['teachers'] = $this->feedbackmodel->getFaculty();
$this->load->view('feedback/proflist',$data);
}
My Model:
public function getFaculty(){
$query = $this->db->query('SELECT * FROM teacher');
return $query->result_array();
}
My View:
<?php
if(!empty($teachers)) {
foreach($teachers as $y){
?>
<tr>
<td><?php echo $y->fid; ?></td>
<td><?php echo $y->fname." ". $y->lname; ?></td>
<td><?php echo $y->email; ?> </td>
<td>
...
Is there any mistake ? How shall i change it. Please let me know:) Thank You.
The reason for this is most likely the version of PHP you're using. Before PHP 7.1 it was only a Notice thrown for the use of an illegal offset. 7.1+ will now throw a Warning. You can avoid it by wrapping an 'if not, isset' in your controller around the offending array. Be sure to cast the array also.
You'll find a good explanation for it here.
public function proflist() {
$data = "";
$this->load->model('feedbackmodel');
$data['teachers'] = (array) $this->feedbackmodel->getFaculty();
if(!isset($data['teachers'])) {
$data['teachers'] = '';
}
$this->load->view('feedback/proflist',$data);
}
To be certain of the version of PHP you're running, you can use phpinfo();.
You need to be more aware of what your variable types are.
In your controller
public function proflist(){
$data = "";
$this->load->model('feedbackmodel');
$data['teachers'] = $this->feedbackmodel->getFaculty();
$this->load->view('feedback/proflist',$data);
}
You declare $data as a STRING by way of $data = "";,
Then you decide it's going to be an array by way of
$data['teachers'] = $this->feedbackmodel->getFaculty();
To be correct $data should be declared as an empty array, not an emtpy string.
public function proflist(){
$data = array(); // Using the older style of declaring an array.
$this->load->model('feedbackmodel');
$data['teachers'] = $this->feedbackmodel->getFaculty();
$this->load->view('feedback/proflist',$data);
}
Now in your Model your returned data is created via
return $query->result_array();
If you look up the codeigniter documentation on this, it will tell you that the resulting data is an Associative Array i.e $data['teachers'] will be of the form
$data['teachers']['fid'] etc.
Your View requires it to be an object.
So you can change your view which requires $teachers to be an object,from...
<?php
// $teachers should be returned from result() which is an object.
if(!empty($teachers)) {
foreach($teachers as $y){
?>
<tr>
<td><?php echo $y->fid; ?></td>
<td><?php echo $y->fname." ". $y->lname; ?></td>
<td><?php echo $y->email; ?> </td>
<td>
...
To
<?php
// $teachers is returned from result_array() which is an associative array.
if(!empty($teachers)) {
foreach($teachers as $y){
?>
<tr>
<td><?php echo $y['fid']; ?></td>
<td><?php echo $y['fname']." ". $y['name']; ?></td>
<td><?php echo $y['email']; ?> </td>
<td>
...
OR simply change the return on your model which is a one liner. So change your Model from
public function getFaculty(){
$query = $this->db->query('SELECT * FROM teacher');
return $query->result_array(); // Return an associative array
}
To
public function getFaculty(){
$query = $this->db->query('SELECT * FROM teacher');
return $query->result; // Return an object
}
So the short version is:
Check your returned array structure. You should use var_dump() for this.
Change your code to suit.
Either change the return on your model from result_array() to result() or
Change your views from addressing objects to associative arrays.
I'm pushed for time to go into any more detail at this stage, but this should help you see what is going on.

codeigniter Undefined variable: finmasuk and if it disapear, datatable wont show

i know this is common question, bet i've tried everything on here and my problem not solved yet. im new at codeigniter and maybe PHP.
like what i've mention above. i have a problem with my code. error show when i try to get data from my database table and show them on html table.
1st error is, when i try to use "foreach" error says "Undefined variable: finmasuk".
2nd error is, when "Undefined variable: finmasuk" disapear, there's some error on my table.
this is my code..
controller
public function finmasuk(){
$this->load->model('m_input');
$data["finmasuk"] = $this->m_input->finmasuk();
$this->load->view("input/standar", $data);
}
model
public function finmasuk(){
$this->db->select("*");
$this->db->from("pemasukan");
$query = $this->db->get();
return $query;
}
view
<?php if($finmasuk->num_rows() > 0){
foreach($finmasuk->result() as $row) { ?>
<tr>
<td><?php echo $row->nomor;?></td>
<td><?php echo $row->tanggal_input;?></td>
<td><?php echo $row->jenis_pemasukan;?></td>
<td><?php echo $row->nominal;?></td>
<td><?php echo $row->keterangan;?></td>
</tr>
<?php }}
else
{ ?>
<tr>
<td colspan="5">No Data Found</td>
</tr>
<?php }?>
error says that "Undefined variable: finmasuk" but i think i have
$data["finmasuk"]
try this:
model
public function finmasuk(){
$this->db->select("*");
$this->db->from("pemasukan");
$query = $this->db->get();
return $query->result();
}
View
<?php if(!empty($finmasuk)){
foreach($finmasuk as $row) { ?>
<tr>
<td><?php echo $row->nomor;?></td>
<td><?php echo $row->tanggal_input;?></td>
<td><?php echo $row->jenis_pemasukan;?></td>
<td><?php echo $row->nominal;?></td>
<td><?php echo $row->keterangan;?></td>
</tr>
<?php }}
else
{ ?>
<tr>
<td colspan="5">No Data Found</td>
</tr>
<?php }?>
You could try a few debugging tricks to check if your query is returning any rows at all.
Controller
public function finmasuk(){
$this->load->model('m_input');
$rowsReturned = $this->m_input->finmasuk()->num_rows();
echo $rowsReturned;
}
Model
public function finmasuk(){
return $this->db->get('pemasukan');
}
Another Option - This checks for query results as an array
Controller
public function finmasuk(){
$this->load->model('m_input');
$resultArray = $this->m_input->finmasuk()->result_array();
var_dump($resultArray);
echo "<br>";
echo "Count - ".count($resultArray); //Get number of rows in
}
Model
public function finmasuk(){
return $this->db->get('pemasukan');
}

not able to retrieve data from database using codeigniter

I am new to CodeIgniter. I have retrieved data from one table but I'm not able to retrieve the data from another table. The view part is not working. Due to some reason the PDF is displaying blank (using tcpdf). Can we fetch it as array? If yes, then how can we fetch it as array?Some of the data appears to be printed below the button that I press.It does not display on the pdf that is generated after the we click on the button.
code :
controller
index(){
$this->load->database();
//load the model
$this->load->model('Order_model');
//load the method of model
$data['h']=$this->Order_model->select();
//return the data in view
$this->load->view('includes/orderPdf', $data);
}
model
public function select()
{
//data is retrive from this query
$query = $this->db->get('master_user');
return $query;
}
view
<?php
foreach ($h->result() as $row)
{
?><tr>
<td><?php echo $row->mobile_no ?></td>
<td><?php echo $row->country ?></td>
</tr>
<?php }
?>
Best way to set up is probably similar to:
Controller:
$this->load->model('Order_model', 'Order');
$data['orders'] = $this->Order->getOrder();
Model:
public function getOrder() {
$q = $this->db->get('master_user');
return $q->num_rows() ? $q->result_array() : [];
}
View:
<?php foreach($orders as $order) { ?>
<tr>
<td><?=$order['mobile_no']?></td>
<td><?=$order['country']?></td>
</tr>
<?php } ?>
Now you can also add an if(empty($orders)) and put in "no orders to show" or similar.

I cant pass variable from controller to view in Codeigniter

I get error saying:
A PHP Error was encountered
Severity: Notice
Message: Undefined variable: data
Filename: views/businessinfo_view.php
My model:
function getPosts(){
$query = $this->db->query("SELECT * FROM customer WHERE
customer_id=12;");
return $query->result_array();
}
My controller:
function ind() {
$data['customerinfo'] = $this->user_model->getPosts();
$this->load->view('businesssinfo_view', $data);
}
MY view:
<?php foreach($data as $d){?>
<tr>
<td><?php echo $d->customer_id;?></td>
<td><?php echo $d->first_name);?></td>
</tr>
<?php }?>
I tried 100 different ways, still can pass the variables, I know my query is correct, I can fetch and echo data in controller, I can't just pass it into view. someone please help me!
function controller() {
$data['customerinfo'] = $this->user_model->getPosts();
$this->load->view('businesssinfo_view', $data);
}
Whenever you pass variable in view, try to access it with key inside view.
This $customerinfo variable will have all your data.
For EX. $customerinfo because your actual variable is $data['customerinfo'].
if var name is $data['extraVal'], in view access through $extraVal.
Try if you have the $data['customerinfo'] then on the view you would use like $customerinfo
http://www.codeigniter.com/user_guide/general/views.html#creating-loops
Controller
function ind() {
$data['customerinfo'] = array();
$data['customerinfo'] = $this->user_model->getPosts();
$this->load->view('businesssinfo_view', $data);
}
Model
function getPosts(){
$this->db->where('customer_id', '12');
$query = $this->db->get('customer');
return $query->result_array();
}
View
<?php foreach($customerinfo as $d){?>
<tr>
<td><?php echo $d['customer_id'];?></td>
<td><?php echo $d['first_name'];?></td>
</tr>
<?php }?>
When model function returns result_array() <?php echo $d['first_name'];?>
When model function returns result() <?php echo $d->first_name;?>
Example found here
Try exactly this :
My model:
function getPosts(){
$query = $this->db->query("SELECT * FROM customer WHERE
customer_id = '12' ");
return $query->result();
}
My controller:
function ind() {
$data['customerinfo'] = $this->user_model->getPosts();
$this->load->view('businesssinfo_view', $data);
}
MY view:
<?php
if(!empty($customerinfo))
{
foreach($customerinfo as $d){?>
<tr>
<td><?php echo $d->customer_id;?></td>
<td><?php echo $d->first_name;?></td>
</tr>
<?php }
}
else{
echo 'Some problem with the variable !! :(';
}
?>

how to delete a specific row in codeigniter?

I am new in codeigniter.In my view page I am showing the data from database in a table where I have two anchor tags for update and delete. I want to delete a specific row from database through id.
my view page is
<?php foreach($query as $row){ ?>
<tr>
<td><?php echo $row->name ?></td>
<td><?php echo $row->testi ?></td>
<td><?php echo anchor('textarea/delete_row', 'DELETE', 'id="$row->id"'); ?></td>
<td><i class="icon-trash"></i></td>
</tr>
<?php } ?>
</table>
my controller page is
function delete_row()
{
$this->load->model('mod1');
$this->mod1->row_delete();
redirect($_SERVER['HTTP_REFERER']);
}
and my model page is
function row_delete()
{
$this->db->where('id', $id);
$this->db->delete('testimonials');
}
I want to delete the row by catching the respective id. Please dont be harsh. thank you
You are using an $id variable in your model, but your are plucking it from nowhere. You need to pass the $id variable from your controller to your model.
Controller
Lets pass the $id to the model via a parameter of the row_delete() method.
function delete_row()
{
$this->load->model('mod1');
// Pass the $id to the row_delete() method
$this->mod1->row_delete($id);
redirect($_SERVER['HTTP_REFERER']);
}
Model
Add the $id to the Model methods parameters.
function row_delete($id)
{
$this->db->where('id', $id);
$this->db->delete('testimonials');
}
The problem now is that your passing the $id variable from your controller, but it's not declared anywhere in your controller.
a simple way:
in view(pass the id value):
<td><?php echo anchor('textarea/delete_row?id='.$row->id, 'DELETE', 'id="$row->id"'); ?></td>
in controller(receive the id):
$id = $this->input->get('id');
$this->load->model('mod1');
$this->mod1->row_delete($id);
in model(get the passed args):
function row_delete($id){}
Actually, you should use the ajax to POST the id value to controller and delete the row, not the GET.
**multiple delete not working**
function delete_selection()
{
$id_array = array();
$selection = $this->input->post("selection", TRUE);
$id_array = explode("|", $selection);
foreach ($id_array as $item):
if ($item != ''):
//DELETE ROW
$this->db->where('entry_id', $item);
$this->db->delete('helpline_entry');
endif;
endforeach;
}
It will come in the url so you can get it by two ways.
Fist one
<td><?php echo anchor('textarea/delete_row', 'DELETE', 'id="$row->id"'); ?></td>
$id = $this->input->get('id');
2nd one.
$id = $this->uri->segment(3);
But in the second method you have to count the no. of segments in the url that on which no. your id come. 2,3,4 etc. then you have to pass. then in the ();
My controller
public function delete_category() //Created a controller class //
{
$this->load->model('Managecat'); //Load model Managecat here
$id=$this->input->get('id'); // get the requested in a variable
$sql_del=$this->Managecat->deleteRecord($id); //send the parameter $id in Managecat there I have created a function name deleteRecord
if($sql_del){
$data['success'] = "Category Have been deleted Successfully!!"; //success message goes here
}
}
My Model
public function deleteRecord($id) {
$this->db->where('cat_id', $id);
$del=$this->db->delete('category');
return $del;
}

Categories