(need explanation) spout reader documentation - codeigniter

can someone help me with this spout documentation,
use Box\Spout\Reader\Common\Creator\ReaderEntityFactory;
$reader = ReaderEntityFactory::createReaderFromFile('/path/to/file.ext');
$reader->open($filePath);
foreach ($reader->getSheetIterator() as $sheet) {
foreach ($sheet->getRowIterator() as $row) {
// do stuff with the row
$cells = $row->getCells();
...
}
}
$reader->close();
what i don't get is :
What should i do with ('/path/to/file.ext')
where do the $filePath come from ? do we need to create a variable named $filePath which will directing to the temporary uploaded file ?
I have read the docimentation and i still don't get it
I also tried to implement it with code igniter, here's my code :
require_once APPPATH.'third_party\spout\src\Spout\Autoloader\autoload.php';
use Box\Spout\Reader\Common\Creator\ReaderEntityFactory;
class Excel extends CI_Controller {
//==========================================================
// C O N S T R U C T O R
//==========================================================
public function __construct()
{
parent::__construct();
$this->load->model('Excel_model');
}
public function index()
{
$reader = ReaderEntityFactory::createReaderFromFile('/path/to/file.ext');
$filePath = APPPATH.'third_party\spout\src\temp\test.xlsx';
$reader->open($filePath);
foreach ($reader->getSheetIterator() as $sheet) {
foreach ($sheet->getRowIterator() as $row) {
// do stuff with the row
$cells = $row->getCells();
echo $cells;
}
}
$reader->close();
}
}
and got this error message instead :
An uncaught Exception was encountered
Type: Box\Spout\Common\Exception\UnsupportedTypeException
Message: No readers supporting the given type: ext
Filename: D:\ONNE\OTHERS\_CODING_PROGRAMMING\XAMPP\htdocs\bulus-ci\application\third_party\spout\src\Spout\Reader\Common\Creator\ReaderFactory.php
Line Number: 59
Backtrace:
File: D:\ONNE\OTHERS\_CODING_PROGRAMMING\XAMPP\htdocs\bulus-ci\application\third_party\spout\src\Spout\Reader\Common\Creator\ReaderFactory.php
Line: 42
Function: createFromType
File: D:\ONNE\OTHERS\_CODING_PROGRAMMING\XAMPP\htdocs\bulus-ci\application\third_party\spout\src\Spout\Reader\Common\Creator\ReaderEntityFactory.php
Line: 24
Function: createFromFile
File: D:\ONNE\OTHERS\_CODING_PROGRAMMING\XAMPP\htdocs\bulus-ci\application\controllers\Excel.php
Line: 20
Function: createReaderFromFile
File: D:\ONNE\OTHERS\_CODING_PROGRAMMING\XAMPP\htdocs\bulus-ci\index.php
Line: 315
Function: require_once

File path is a string pointing to the spreadsheet you're trying to read. You need to define it and pass it to Spout this way:
use Box\Spout\Reader\Common\Creator\ReaderEntityFactory;
$filePath = APPPATH.'third_party\spout\src\temp\test.xlsx';
$reader = ReaderEntityFactory::createReaderFromFile($filePath);
$reader->open($filePath);
foreach ($reader->getSheetIterator() as $sheet) {
foreach ($sheet->getRowIterator() as $row) {
// do stuff with the row
$cells = $row->getCells();
...
}
}
$reader->close();
Can you give it a try?

Related

ErrorException in AssetController.php line 21: Trying to get property of non-object

I have this code source that i'm trying to use in one of my projects, it worked with laravel 5.2. This the function in the assetController:
namespace App\Http\Controllers;
use App\Setting;
use File;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
class AssetController extends Controller
{
/**
* List all image from image directory
*/
public function getAsset()
{
//Get Admin Images
$adminImages = array();
//get image file array
$images_dir = Setting::where('name', 'images_dir')->first();
$folderContentAdmin = File::files($images_dir->value);
//check the allowed file extension and make the allowed file array
$allowedExt = Setting::where('name', 'images_allowedExtensions')->first();
$temp = explode('|', $allowedExt->value);
foreach ($folderContentAdmin as $key => $item)
{
if( ! is_array($item))
{
//check the file extension
$ext = pathinfo($item, PATHINFO_EXTENSION);
//prep allowed extensions array
if (in_array($ext, $temp))
{
array_push($adminImages, $item);
}
}
}
//Get User Images
$userImages = array();
$userID = Auth::user()->id;
$images_uploadDir = Setting::where('name', 'images_uploadDir')->first();
if (is_dir( $images_uploadDir->value . "/" .$userID ))
{
$folderContentUser = File::files($images_uploadDir->value . "/" .$userID );
if ($folderContentUser)
{
foreach ($folderContentUser as $key => $item)
{
if ( ! is_array($item))
{
//check the file extension
$ext = pathinfo($item, PATHINFO_EXTENSION);
//prep allowed extensions array
//$temp = explode("|", $this->config->item('images_allowedExtensions'));
if (in_array($ext, $temp))
{
array_push($userImages, $item);
}
}
}
}
}
//var_dump($folderContent);
//var_dump($adminImages);
return view('assets/images', compact('adminImages', 'userImages'));
}
The problem is in the line 21 :
//get image file array
$images_dir = Setting::where('name', 'images_dir')->first();
$folderContentAdmin = File::files($images_dir->value);
From my research I find out that the reason is because the setting table is empty which it is true.
Please tell me if there is another cause to that problem if it's not the case I need a solution because I don't have a way to fill that table except doing it from the database itself (phpmyAdmin)

Message: Undefined property: CI_DB_mysqli_driver::$query

I have a problem in CodeIgniter.
My code:
<?php
$kullanici_yetki_id=$this->session->user_sess['id'];
$result= array(
$this->db->select('*'),
$this->db->from('menuler'),
$this->db->join('menu_yetki', 'menuler.menu_id=menu_yetki.menu_id'),
$this->db->where('menuler.is_active', '1'),
$this->db->where('user_id', $kullanici_yetki_id),
$this->db->where('durum', "1"),
$this->db->limit('1')
);
$sorgu=$this->db->query->$result();
if($sorgu->num_rows()>0){
foreach ($sorgu as $row) {
echo $row->menu_adi;
} } else{
echo "nope";
}
?>
When I run it, it returns this error:
Message: Undefined property: CI_DB_mysqli_driver::$query
Filename: admin/_leftmenu.php Line Number: 34
Backtrace:
File: C:\wamp64\www\bireberberci\application\views\admin_leftmenu.php
Line: 34 Function: _error_handler
File:
C:\wamp64\www\bireberberci\application\controllers\admin\Home.php
Line: 16 Function: view
File: C:\wamp64\www\bireberberci\index.php Line: 315 Function:
require_once
And this:
A PHP Error was encountered Severity: Error
Message: Method name must be a string
Filename: admin/_leftmenu.php
Line Number: 34
You are missing the get() and your session part user_sess['id'] is wrong
https://www.codeigniter.com/user_guide/general/models.html#loading-a-model
<?php
class Example_model extends CI_Model {
public function example() {
$data = array();
$this->db->select('*');
$this->db->from('menuler');
$this->db->join('menu_yetki', 'menuler.menu_id=menu_yetki.menu_id');
$this->db->where('menuler.is_active', '1');
$this->db->where('user_id', $this->session->userdata('id'));
$this->db->where('durum', "1");
$this->db->limit('1');
$sorgu = $this->db->get();
if($sorgu->num_rows() > 0) {
foreach ($sorgu->result() as $row) {
$data[] = $row->menu_adi;
}
}
return $data;
}
}
You are close, but your syntax is a tad off. You can refer to the manual about query builder for more information.
Your biggest mistake was putting all the db calls into an array. This is much closer to what you want.
$kullanici_yetki_id = $this->session->userdata['id'];
$this->db->from('menuler');
$this->db->join('menu_yetki', 'menuler.menu_id=menu_yetki.menu_id');
$this->db->where('menuler.is_active', '1');
$this->db->where('user_id', $kullanici_yetki_id);
$this->db->where('durum', "1");
$this->db->limit('1');
$sorgu = $this->db->get();
if ($sorgu->num_rows() > 0) {
foreach ($sorgu->result() as $row) {
echo $row->menu_adi;
}
} else {
echo "nope";
}

select fails in custom model codeigniter 2

I have a problem with database select function, in my custom model. This is the code
class MY_Model extends CI_Model
{
public function __construct()
{
parent::__construct();
$this->load->database();
$this->load->helper('inflector');
}
public function fetch($parameters = array(), $raw = FALSE)
{
$tablename = $this->getTableName();
$this->select_fields(FALSE == empty($parameters['fields']) ? $parameters['fields'] : FALSE);
unset($parameters['fields']);
if (FALSE == empty($parameters['limit'])) $limit = $parameters['limit'];
if (FALSE == empty($parameters['offset'])) $offset = $parameters['offset']; else $offset = 0;
unset($parameters['limit']);
unset($parameters['offset']);
if (FALSE == empty($limit))
{
$this->db->limit($limit, $offset);
}
$this->parseFilters($parameters);
$query = $this->db->get($tablename);
if ($query->num_rows() > 0)
{
if ($raw)
return $query;
$rows = $query->result();
$objects = array();
foreach ($rows as $row)
$objects[] = $this->hidrate($row);
return $objects;
}
else
{
return array();
}
}
protected function select_fields($fields)
{
if (TRUE == empty($fields))
{
$fields = "`" . $this->getTableName() . "`.*";
}
$this->db->select($fields);
}
public function fetchOne($parameters = array())
{
$parameters['limit'] = 1;
$list = $this->fetch($parameters);
if (FALSE == empty($list))
{
return reset($list);
}
else
{
return null;
}
}
Expecifict in $this->db->select($fields);
Fatal error: Call to a member function select() on a non-object
The model is a custom model and the applicacions model extends of this model. The question is why throws that error the database is correct.
I have a MY_loader create in codeginiter 1.7 and I try update to codeigniter 2
class MY_Loader extends CI_Loader
{
function model($model, $name = '', $db_conn = FALSE)
{
if (is_array($model))
{
foreach($model as $babe)
{
$this->model($babe);
}
return;
}
if ($model == '')
{
return;
}
if ( substr($model, -4) == '_dao' )
{
return parent::model('dao/' . $model, $name, $db_conn);
}
parent::model( 'dao/' . $model . '_dao', $model, $db_conn);
include_once APPPATH . '/models/' . $model . EXT;
}
}
I don't know how update this model to codeigniter 2 and I believe this Loader generates error with my MY_Model
I'll try troubleshooting why does db return as a non-object.
I'd remove all code and start with a simple select(), if that works, I'll start adding code gradually and see where it breaks.
everything seems to be in order but first you'll need to see if the basic functionality exists.
so
1)remove all code, see if a basic select() works, if it doesn't, troubleshoot further.
2)if it does, keep adding code and see what breaks the select() statement.
3)keep adding code until you spot the issue.

Getting data from model to controller

I do have a question : I cannot pass the data from model to controller
You can see some of my codes, please help me.
It does not work!
this is my model "mymodel.php"
....
$query = $this->db->query("SELECT * FROM `rand` WHERE `used` = 0 LIMIT 0, 1");
if($query){
foreach ($query->result() as $row);
}
$t = "EXAMPLE/{$row->code}";
function wandad() {
return $t;
}
.....
and this is my controller mycont.php
...
$this->load->model('mymodel');
$data['new'] = $this->Mymodel->wandad();
$this->load->view('myview',$data);
...
and this is my view myview.php
....
echo $new;
.....
Clearly you The model is not written properly and to corrent this simple do this
1.) I put a default value on $t
2.) I put the query >> loop on the inside function
wandad
so it can be executed once called from controller
function wandad() {
$query = $this->db->query("SELECT * FROM `rand` WHERE `used` = 0 LIMIT 0, 1");
$t = "";
if($query){
foreach ($query->result() as $row){
$t = "EXAMPLE/{$row->code}".'<br>';
}
}
return $t;
}
Here are several issues into your model
Your Foreach function doesn't do anything
$t is not in the same namespace than wandad()
Function wandad is not defined into your model class
I'm not sure of what you wanna get with wandad() function but here's a pattern.
function yourFunction()
{
/* This will return the full query as an array */
$query = $this->db->query("SELECT * FROM `rand` WHERE `used` = 0 LIMIT 0, 1")->result_array();
/* Store variable in the same class */
$this->t = "EXAMPLE/".$query[0]['code'];
/* Close yourFunction() */
}
public function wandad() {
return $this->t;
}
Then into your controller, do that instead :
$this->load->model('mymodel');
$this->mymodel->yourFunction();
$data['new'] = $this->mymodel->wandad();
$this->load->view('myview',$data);
#Touki his foreach actually does something. It will set the variable $row with the last row that is returned from the query. Not the best way to do it ... But it's a way. Better would be to use a limit in the query.
There is a small mistake in your code #Ernesto.that is
foreach ($query->result() as $row){
$t. = "EXAMPLE/{$row->code}".'<br>';
}
but your code was simply nice

Codeigniter - Call to undefined method CI_DB_mysql_driver::

I am getting this error "Fatal error: Call to undefined method CI_DB_mysql_driver::findVenueInfo()" when I try to use one of my models.
I have a view with this anchor:
echo anchor('welcome/searchVenue/' . $row->venue_id, $row->venue);
which generates a link like: http://localhost/ci-llmg/index.php/welcome/searchVenue/1
the method called is
function searchVenue()
{
$this->load->model('venues');
//get venue info
$data['info'] = $this->venues->findVenueInfo($this->uri->segment(3)); //this line generates the error
}
and the findVenueInfo function in the model (venues.php) is:
function findVenueInfo($id)
{
$data = array();
$this->db->where('id', $id);
$Q = $this->db->get('venues');
if ($Q->num_rows() > 0)
{
foreach ($Q->result() as $row)
{
$data[] = $row;
}
}
$Q->free_result();
return $data;
}
..but the result of this is Fatal error: Call to undefined method CI_DB_mysql_driver::findVenueInfo()
I'm probably missing something stupid, but can't get it to work! What do you think?
Are you sure your index.php file at the root of ur project have included bootstrap in proper place
Go to the end of the index.php file and include the bootstrap file just before including codeigniter.php.
Your index.php file should have its end lines look like this.
/*
* --------------------------------------------------------------------
* LOAD THE BOOTSTRAP FILE
* --------------------------------------------------------------------
*
* And away we go...
*
*/
require_once APPPATH.'third_party/datamapper/bootstrap.php';
require_once BASEPATH.'core/CodeIgniter.php';
function findVenueInfo($id)
{
$data = array();
$this->db->select()->from('venues')->where('id', $id);<----change it to
$Q = $this->db->get();<----change it to
if ($Q->num_rows() > 0)
{
foreach ($Q->result() as $row)
{
$data[] = $row;
}
}
$Q->free_result();
return $data;
}
I was getting a similar error and found that in 3.0 I needed to enable query_builder in application/config/database.php
$query_builder = TRUE;

Resources