How to pass an array to the main view in Codeigniter - php

It seems that I just can't pass an array in another function into main view.
My index() function is =>
public function index()
{
$this->load->view('anasayfa');
}
My aforementioned function is =>
public function pozisyon_tutma()
{...
....
print_r(array_values($pozisyon));
$GLOBALS['pozisyong'] = $pozisyon;
}
which works...
I am seeking something like =>
public function index()
{
$this->pozisyon_tutma();
$data['hoppa']=$GLOBALS['pozisyong'];
$this->load->view('anasayfa', $data);
}
and I am seeking in view 'anasayfa' (which is the main view) something like this code to work=>
<?php
print_r(array_values($hoppa));
?>
However this code gives the following error=>
A PHP Error was encountered
Severity: Notice
Message: Undefined variable: hoppa
Filename: views/anasayfa.php
Line Number: 19
Backtrace:
File: D:\wamp\www\proje\application\views\anasayfa.php
Line: 19
Function: _error_handler
File: D:\wamp\www\proje\application\controllers\Welcome.php
Line: 24
Function: view
File: D:\wamp\www\proje\index.php
Line: 292
Function: require_once
A PHP Error was encountered
Severity: Warning
Message: array_values() expects parameter 1 to be array, null given
Filename: views/anasayfa.php
Line Number: 19
Backtrace:
File: D:\wamp\www\proje\application\views\anasayfa.php
Line: 19
Function: array_values
File: D:\wamp\www\proje\application\controllers\Welcome.php
Line: 24
Function: view
File: D:\wamp\www\proje\index.php
Line: 292
Function: require_once
All help will be much appreciated. I checked all other threads in stackoverflaw and could not make any of them work in my case... Thank you all

You should use in your controller:
$data = array('key1'=>$value1, 'key2'=>$value2, 'key3'=>functionX()....);
$this->load->view('yourview.php',$data);
And in the view to access to the array of values:
echo $key1; // prints $value1.
echo $key2; // prints $value2.
Also, you could use $_SESSION to store some variables in CI. Hope it helps.

I did it in the following way=>
public function index()
{
require('Hesaplama.php');
$Hesaplama = new Hesaplama();
$data['hoppa']=$Hesaplama->pozisyon_tutma();
$this->load->view('anasayfa', $data);
}
It works. I returned $pozisyon which is an array as #mic suggested. I will keep the suggestions of #JP. Aulet in case I need them in the near future. Thank you all...

Related

Class 'MpdfException' not found

I am using CodeIgniter on a project that Generates PDF. it work once in single PDF now I tried another it got an error. Here is the code:
class Checkout extends CI_Controller {
public function __construct(){
parent::__construct();
$this->load->model('PurchaserModel', 'purchaser');
....
$this->load->library('M_pdf');
}
public function save(){
.....
$data['orders'] = $this->order->get_order_details($logged_in_id);
if($data['orders']){
$details = $this->purchaser->get_purchaser_details($this->session->userdata['logged_in']['username']);
$data['details'] = convertvalue($details);
$order_id = $data['orders'][0]['order_id'];
$data['order_id'] = $order_id;
}
// 1st PDF work fine if the other generate PDF is commented out
$purchaser_sheet_html = $this->load->view('pdf/purchaser_sheet', $data, true);
$purchaser_sheet_html2 = $this->load->view('pdf/purchaser_sheet_page_2', $data, true);
$pdf1 = $this->m_pdf->load();
$pdfFilePath1 = uniqid(rand(), true);
$pdfFilePath = "purchase-order-summary-".date('Ymdhis')."-".$pdfFilePath1.".pdf";
$pdf1->WriteHTML($purchaser_sheet_html);
$pdf1->AddPage();
$pdf1->WriteHTML($purchaser_sheet_html2);
$pdf1->Output("./uploads/".$pdfFilePath, "F"); // this is line 425 and got error
// 2nd PDF when this code is up, it got an error
$construction_sheet_summary = $this->load->view('pdf/construction_sheet_summary', $data, true);
$pdf2 = $this->m_pdf->load();
$pdfFilePath2 = uniqid(rand(), true);
$pdfFilePath_2 = "construction-sheet-summary-".date('Ymdhis')."-".$pdfFilePath2.".pdf";
$pdf2->WriteHTML($construction_sheet_summary); // this is line 433 and got error
$pdf2->Output("./uploads/".$pdfFilePath_2, "F");
$this->db->update($this->db->dbprefix('order'), array('purchaser_sheet' => $pdfFilePath, 'construction_sheet' => $pdfFilePath_2), array('ID' => $insert_id));
$email_settings = $this->settings->email_settings();
}
and this is the complete error message:
A PHP Error was encountered
Severity: Warning
Message: count(): Parameter must be an array or an object that implements Countable
Filename: mpdf/mpdf.php
Line Number: 1770
Backtrace:
File: C:\xampp1\htdocs\dev.app.com\public\application\third_party\mpdf\mpdf.php
Line: 1770
Function: _error_handler
File: C:\xampp1\htdocs\dev.app.com\public\application\third_party\mpdf\mpdf.php
Line: 7591
Function: Close
File: C:\xampp1\htdocs\dev.app.com\public\application\controllers\Checkout.php
Line: 425
Function: Output
File: C:\xampp1\htdocs\dev.app.com\public\index.php
Line: 315
Function: require_once
A PHP Error was encountered
Severity: 8192
Message: The each() function is deprecated. This message will be suppressed on further calls
Filename: mpdf/mpdf.php
Line Number: 9143
Backtrace:
File: C:\xampp1\htdocs\dev.app.com\public\application\third_party\mpdf\mpdf.php
Line: 9143
Function: each
File: C:\xampp1\htdocs\dev.app.com\public\application\third_party\mpdf\mpdf.php
Line: 27927
Function: _putimages
File: C:\xampp1\htdocs\dev.app.com\public\application\third_party\mpdf\mpdf.php
Line: 9508
Function: _putresources
File: C:\xampp1\htdocs\dev.app.com\public\application\third_party\mpdf\mpdf.php
Line: 1797
Function: _enddoc
File: C:\xampp1\htdocs\dev.app.com\public\application\third_party\mpdf\mpdf.php
Line: 7591
Function: Close
File: C:\xampp1\htdocs\dev.app.com\public\application\controllers\Checkout.php
Line: 425
Function: Output
File: C:\xampp1\htdocs\dev.app.com\public\index.php
Line: 315
Function: require_once
An uncaught Exception was encountered
Type: Error
Message: Class 'MpdfException' not found
Filename: C:\xampp1\htdocs\dev.app.com\public\application\third_party\mpdf\mpdf.php
Line Number: 32456
Backtrace:
File: C:\xampp1\htdocs\dev.app.com\public\application\third_party\mpdf\mpdf.php
Line: 22328
Function: ConvertSize
File: C:\xampp1\htdocs\dev.app.com\public\application\third_party\mpdf\mpdf.php
Line: 15440
Function: setCSS
File: C:\xampp1\htdocs\dev.app.com\public\application\third_party\mpdf\mpdf.php
Line: 13614
Function: OpenTag
File: C:\xampp1\htdocs\dev.app.com\public\application\controllers\Checkout.php
Line: 433
Function: WriteHTML
File: C:\xampp1\htdocs\dev.app.com\public\index.php
Line: 315
Function: require_once
I tried to debug the code and I found out this line got an isseu
$construction_sheet_summary = $this->load->view('pdf/construction_sheet_summary', $data, true);
I construction_sheet_summary.php is present, I tried to remove all php code on that file but got the same error.
do anyone have an idea about my case?
As #Viney recommends in the comment, upgrade to the (preferably) last version of mPDF using composer - this will solve both dependencies and the each() deprecation warning.

Passing array data to view from controller in CodeIgniter

Here is my controller function code:
public function dispcp(){
$data = array(
'company' => $this->input->post("company"),
'fyfrom' => $this->input->post("fyfrom"),
'fyto' => $this->input->post("fyto"),
);
$this->load->view('view',$data);
}
Here the data comes from a form from another view.
this is what my code in view.php looks like :
<?php print_r ($data[0]->fyfrom); ?>
Error:Message: Undefined variable: data
Filename: views/view.php
Line Number: 14
Backtrace:
File: C:\wamp\www\admin\application\views\view.php
Line: 14
Function: _error_handler
File: C:\wamp\www\admin\application\controllers\Dashboard.php
Line: 586
Function: view
File: C:\wamp\www\admin\index.php
Line: 263
Function: require_once
Works correctly in Controller, not in view.
Once the data is passed to the view you need to use the array keys are variables directly :
<?php print_r($fyfrom); ?>

Codeigniter undefined property on core/Model.php

Controller
public function editItem(){
$this->load->helper('form');
$this->load->model('ItemModel');
$data['items'] = $this->ItemModel->itemlist();
$item_details = $this->ItemModel->edititem($this->input->get('id'));
$data2['item_name'] = $item_details->name; //THIS IS LINE 28
$data2['item_description']= $item_details->description;
$data2['item_price'] = $item_details->price;
$this->load->view('item/item_edit',$data2);
}
There's an error to my view, and I don't know why
A PHP Error was encountered
Severity: Notice
Message: Undefined property: Item::$name
Filename: core/Model.php
Line Number: 77
Backtrace:
File: C:\xampp\htdocs\itwa213\application\controllers\Item.php
Line: 28
Function: __get
File: C:\xampp\htdocs\itwa213\index.php
Line: 292
Function: require_once
I already checked my autoload on config and it's properly configured with
$autoload['libraries'] = array('database');
You could try this
public function editItem() {
$this->load->helper('form');
$this->load->model('ItemModel');
$data['items'] = $this->ItemModel->itemlist();
$item_details = $this->ItemModel->edititem($this->input->get('id'));
$data2['item_name'] = $item_details['name']; //THIS IS LINE 28
$data2['item_description'] = $item_details['description'];
$data2['item_price'] = $item_details['price'];
$this->load->view('item/item_edit',$data2);
}
Your controller function change to like this
public function editItem(){
$this->load->helper('form');
$this->load->model('ItemModel');
$data['items'] = $this->ItemModel->itemlist();
$data2['item_details'] =$this->ItemModel->edititem($this->input->get('id'));
$this->load->view('item/item_edit',$data2);
}
and in your view page
echo $item_details->name;
You should look at your code for the get function you use. try to replace the $this->input->get('id') to existing id number first. check whether it success or not. If success by using existed id number, so it should your get function not working or something wrong. i'm seldom use $this->input->get() but $this->input->post().
on your edit link, you can use:
edit.
so the id is the third segment uri. use $this->uri->segment(3); to get the third segment and store into $item_id variable into your controller. so, no need to use $this->input->get(); to get the item_id value.

Using mpdf library in codeigniter return error

Error
A PHP Error was encountered
Severity: Warning
Message: preg_match() expects parameter 2 to be string, object given
Filename: libraries/mpdf.php
Line Number: 22307
Backtrace:
File: D:\xamp\htdocs\ci_test\application\libraries\mpdf.php
Line: 22307
Function: preg_match
File: D:\xamp\htdocs\ci_test\application\libraries\mpdf.php
Line: 13118
Function: ReadCharset
File: D:\xamp\htdocs\ci_test\application\controllers\Welcome.php
Line: 28
Function: WriteHTML
File: D:\xamp\htdocs\ci_test\index.php
Line: 292
Function: require_once
this is my controller
$this->load->library('mpdf');
$html = $this->load->view('welcome_message');
$this->mpdf->WriteHTML($html);
$this->mpdf->Output();
Try loading view likr this,
$html = $this->load->view('welcome_message','',TRUE);
^ will get the value to variable.

Using external class in my Controller

Im working on my first project in CodeIgniter and I wonder How can I use my class from library in my Controller.
libraries/Twitterclass.php:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Twetterclass {
public function __construct($hashtag, $tweet_id)
{
require_once('TwitterAPIExchange.php');
//There is my working code
//I want to make use of this $n in my Controller
return $n;
}
}
/* End of file Twetterclass.php */
My Controller:
public function microtweets()
{
$params = array('hashtag' => 'somehashtag', 'tweet_id' => '673899616799191040');
$data['count_tweets'] = $this->load->library('Twetterclass', $params);
$this->load->view('tweets', $data);
}
I want to use his extended class in my controller and work there on this $n value or for example display it in my View.
I get few errors:
A PHP Error was encountered
Severity: Warning
Message: Missing argument 2 for Twetterclass::__construct(), called in
/home/jail/kg7dad5/home/kg7dad5/domains/badzlepszy.pl/public_html/coinmonitor/system/core/Loader.php
on line 1246 and defined
Filename: libraries/Twetterclass.php
Line Number: 5
Backtrace:
File: /application/libraries/Twetterclass.php Line: 5 Function:
_error_handler
File: /application/controllers/Cointweet.php Line: 24 Function:
library
File: /public_html/coinmonitor/index.php Line: 292 Function:
require_once
first try to debug parameter you passed in constructor
public function __construct($hashtag, $tweet_id)
{
echo $hashtag;
echo $tweet_id;
die;
}
according to your code, change like this,
public function __construct($arr)
{
echo $arr['hashtag'];
echo $arr['tweet_id'];
exit;
}
Because you are passing 1 array, so access in library with array index.

Categories