How to import data from excel to mysql database using php - php

I am using PHPExcel to import a XLSX file to my related database. But while running the function I am getting the error. My code looks like shown below.
Controller:
<?php if (!defined ('BASEPATH')) exit ('No direct access allowed');
class ExcelController extends CI_Controller
{
public function index()
{
//load library excel
$this->load->library('excel');
//Here i used microsoft excel 2007
$objReader= PHPExcel_IOFactory::createReader('Excel2007');
//Set to read only
$objReader->setReadDataOnly(true);
//Load excel file
$objPHPExcel=$objReader->load('data.xls'); // error in this line
$objWorksheet=$objPHPExcel->setActiveSheetIndex(0);
//load model
$this->load->model('user_model');
//loop from first data untill last data
for($i=2;$i<=77;$i++)
{
$name= $objWorksheet->getCellByColumnAndRow(0,$i)->getValue();
$address= $objWorksheet->getCellByColumnAndRow(1,$i)->getValue();
$data_user=array('name'=>$name, 'username'=>$address);
$this->user_model->add_data($data_user);
}
}
}
?>
model:
<?php
if (!defined ('BASEPATH')) exit ('No direct access allowed');
class User_model extends CI_Controller
{
public function __construct() {
parent::__construct();
}
public function add_data($data_user)
{
$this->load->database();
$this->db->insert('data',$data_user);
return $this->db->insert_id();
}
}
?>
Error in my code:
Fatal error: Uncaught exception 'PHPExcel_Reader_Exception' with message 'Could not open data.xls for reading! File does not exist.' in C:\xampp\htdocs\ci_excel\application\third_party\PHPExcel\Reader\Excel2007.php:347 Stack trace: #0 C:\xampp\htdocs\ci_excel\application\controllers\excelcontroller.php(19): PHPExcel_Reader_Excel2007->load('data.xls') #1 [internal function]: ExcelController->index() #2 C:\xampp\htdocs\ci_excel\system\core\CodeIgniter.php(359): call_user_func_array(Array, Array) #3 C:\xampp\htdocs\ci_excel\index.php(202): require_once('C:\xampp\htdocs...') #4 {main} thrown in C:\xampp\htdocs\ci_excel\application\third_party\PHPExcel\Reader\Excel2007.php on line 347

Judging by the error message and your comment, it looks like you are using an incorrect filepath.
$objPHPExcel=$objReader->load('data.xls');
In CodeIgniter paths are relative to the entry script, usually index.php.
Use a relative file path to this location or alternatively an absolute path.

I think you used incorrect path , change this
$objPHPExcel=$objReader->load('data.xls'); // error in this line
To
$objPHPExcel=$objReader->load('./application/third_party/Excel5/data.xlsx');

Related

Read old excel xls using php

I try to read excel file format slx by php code i use SimpleXLS class but i get error :
Fatal error: Uncaught Error: Class 'SimpleXLS' not found in C:\laragon\www\xls\examples\01-basic_usage.php:5
Stack trace:
#0 {main}
thrown in C:\laragon\www\xls\examples\01-basic_usage.php on line 5
Here is the page code, which is the original code for the page :
<?php /** #noinspection ForgottenDebugOutputInspection */
require_once __DIR__ . '/SimpleXLS.php';
echo '<h1>Parse books.xsl</h1><pre>';
if ( $xls = SimpleXLS::parse('books.xls') ) {
print_r( $xls->rows() );
} else {
echo SimpleXLS::parseError();
}
echo '<pre>';
when i use SimpleXLSX class with file xlsx it's work but xls not work
https://github.com/shuchkin/simplexls
any help?

PHP- PHP Fatal error: Uncaught ArgumentCountError: Too few arguments to function SignupContr::signupUser()

I am new to OOP php and took some lessons and now Im trying to practice, I used login with discord method and If user Is logged in the system sould add the discord id to database if user is not already in database but Im facing quite the issue and I have no luck so far to fix It myself, I even asked few of my buddies If they can see the issue but they did not also know the real issue.
My error:
[22-Jul-2022 06:56:00 UTC] PHP Fatal error: Uncaught ArgumentCountError: Too few arguments to function
SignupContr::signupUser(), 0 passed in C:\wamp64\www\OOP-Test\inc\signup.inc.php on line 13
and exactly 1 expected in C:\wamp64\www\OOP-Test\classes\signup-contr.class.php:10
Stack trace:
#0 C:\wamp64\www\OOP-Test\inc\signup.inc.php(13): SignupContr->signupUser()
#1 C:\wamp64\www\OOP-Test\dashboard\index.php(12): include('C:\\wamp64\\www\\O...')
#2 {main}
thrown in C:\wamp64\www\OOP-Test\classes\signup-contr.class.php on line 10
My code looks like this right now:
signup.inc.php
{
$discord = $_GET['user'];
include "../classes/dbh.class.php";
include "../classes/signup.class.php";
include "../classes/signup-contr.class.php";
$signup = new SignupContr($discord);
$signup->signupUser();
//header('location: ../dashboard/index.php');
}
signup.class.php
class Signup extends Dbh {
protected function setUser($discord){
$stmt = $this->connect()->prepare("INSERT INTO `users` ('discord') VALUES (?);");
if(!$stmt->execute(array($discord))) {
// $stmt = null;
// header("location: ../dashboard/index.php?useraddfailed");
// exit();
}
$stmt->debugDumpParams();
$stmt = null;
}
}
signup-contr.class.php
class SignupContr extends Signup{
private $discord;
public function _construct($discord){
$this->discord = $discord;
}
public function signupUser(){
$this->setUser($this->discord);
var_dump($this->discord);
}
}
I have not done the check part If user Is allready In the database
I Finally found the issue. I was missing one underscore in construct.
signup.contr.class.php should be like this:
class SignupContr extends Signup{
private $discord;
public function __construct($discord){
$this->discord = $discord;
}
public function signupUser(){
$this->setUser($this->discord);
var_dump($this->discord);
}
}
Of course it was the simplest issue there Is but I really tought there suppost to be only one underscore. You always should check the simple things first, I did not because usally you don't use construct In procedural php

Can't instantiate a class inside another PHP

How can I instatiate athe class OM inside the MySQL_DataMapper?
Here is the MySQL_DataMapper.php:
<?php
namespace asc {
include('../../models/OM.php');
class MySQL_DataMapper
{
[omitted code for simplicity]
public function fetchAllOMs()
{
$query = "REALLY LONG QUERY";
$result = $this->pdo->query($query);
$OMs = array();
while ($row = $result->fetch()){
$OM = new OM($row['id_organizacao_militar'], $row['nome'], $row['sigla'], $row['forca_armada']);
array_push($OMs, );
}
var_dump($OMs);
}
}
}
Here is the error I get:
Warning: include(../../models/OM.php): failed to open stream: No such file or directory in /home/alekrabbe/PhpstormProjects/stm_asc/controller/database/MySQL_DataMapper.php on line 10
Warning: include(): Failed opening '../../models/OM.php' for inclusion (include_path='.:/usr/share/php') in /home/alekrabbe/PhpstormProjects/stm_asc/controller/database/MySQL_DataMapper.php on line 10
Fatal error: Uncaught Error: Class 'asc\OM' not found in /home/alekrabbe/PhpstormProjects/stm_asc/controller/database/MySQL_DataMapper.php:37 Stack trace: #0 /home/alekrabbe/PhpstormProjects/stm_asc/views/cadastro-militar.php(13): asc\MySQL_DataMapper->fetchAllOMs() #1 {main} thrown in /home/alekrabbe/PhpstormProjects/stm_asc/controller/database/MySQL_DataMapper.php on line 37
I don't understand why it fails as I did just that on another php file and it worked there. Thank you.
EDIT 1
Forgot to mention but the class OM.php is also in the asc namespace.
Here it is:
<?php
namespace asc{
class OM
{
private $id;
private $nome;
private $sigla;
private $forca_armada;
public function __construct($id, $nome, $sigla, $forca_armada)
{
$this->id = $id;
$this->nome = $nome;
$this->sigla = $sigla;
$this->forca_armada = $forca_armada;
}
[Gets and Sets]
}
}
You're inside a namespace:
namespace asc {
So references to OM become asc\OM. Fix this by anchoring to the root namespace:
$OM = new \OM(...);
Try include __DIR__ . "/../../models/OM.php"; and make sure the path is correct
EDIT: the better way is to use an autoloader as #Alex Howansky mentioned in a comment on his answer, but in a pinch just make sure the path is correct and use DIR.

requiring a file from laravel facade file

I have a facade 'CustomMage', but from which I'm not able to include magento api.
<?php namespace Bond\CustomMage;
class CustomMage {
/* Using mage configurations*/
public static function changeIsActive($table, $id, $status){
$api_path = \Config::get('api.mage_soap_api_path');
require_once("{$api_path}app/Mage.php");
/*umask(0);
Mage::app('default');
$quote = Mage::getModel('sales/quote')->load($id);
$quote->setIsActive($status)->save();*/
}
}
I'm getting this error,
[2018-02-07 08:47:09] local.ERROR: exception 'ErrorException' with
message 'include(Bond\CustomMage\Mage.php): failed to open stream: No
such file or directory' in
/var/www/html/FootballTicketPad/ecomm/lib/Varien/Autoload.php:93
But this code is ok if i use it in app/helpers.php.
function GetModal($table, $id){
$api_path = \Config::get('api.mage_soap_api_path');
require_once("{$api_path}app/Mage.php");
umask(0);
Mage::app('default');
return Mage::getModel($table)->load($id);
}

Autoload error in php

I am trying autoload function in php. The files are all in the same directory.
I have a file called aviation.php with the following code:
class Avaitor{
public function __construct(){
echo "Fly high<br>";
}
}
Then in my autoloader file I am trying to do this:
function __autoload($fileName){
if(file_exists($fileName . ".php"))
require_once $fileName . ".php";
}
//require_once "aviatior.php";
$pilot = new Avaitor();
But I am getting this error:
Fatal error: Uncaught Error: Class 'Avaitor' not found in
/Applications/MAMP/htdocs/php_oop/autoload.php:22 Stack trace: #0
{main} thrown in /Applications/MAMP/htdocs/php_oop/autoload.php on
line 22
Just to test that require_once does find my aviator.php I tried it out and then commented it out.
What am I doing wrong here?

Categories