Undefined index: dontprint - php

I am trying to use php-excel-reader(http://www.yiiframework.com/extension/php-excel-reader/) extension to upload excel file into database. I was trying to implement the example function provided in the extension page. But i keep on getting the error "Undefined index: dontprint".I am not able to understand what is causing the error. please help.This is my controler code.
public function actionUpload()
{
Yii::import('ext.phpexcelreader.JPhpExcelReader');
$data=new JPhpExcelReader(Yii::app()->getBasePath().'/import/example.xls');
echo $data->dump(true,true);
}
This is the code involving dontprint in extension code.
for($col=1;$col<=$this->colcount($sheet);$col++) {
// Account for Rowspans/Colspans
$rowspan = $this->rowspan($row,$col,$sheet);
$colspan = $this->colspan($row,$col,$sheet);
for($i=0;$i<$rowspan;$i++) {
for($j=0;$j<$colspan;$j++) {
if ($i>0 || $j>0) {
$this->sheets[$sheet]['cellsInfo'][$row+$i][$col+$j]['dontprint']=1;
}
}
}
if(!$this->sheets[$sheet]['cellsInfo'][$row][$col]['dontprint']) {
$style = $this->style($row,$col,$sheet);
if ($this->colhidden($col,$sheet)) {
$style .= "display:none;";
}
Any help appreciated. Thanks in advance.

A quick Google shows an identical issue happening when iconv isn't installed and enabled, do you have that installed/enabled?

IT's a notice.. Problem solved with adding on Controller
error_reporting(E_ERROR|E_WARNING);
But this is not a safe solution.

Related

Invalid argument supplied for foreach() simple_html_dom.php on line 1119

here is what im doing
im using
pthreads from - pthreads.org
php Simple Html DOM parser from - simplehtmldom.sourceforge.net
now the process of what i (will) do is:
I am reading a bulk of links that is from a text file.
I initialize a thread to have a separate process
I am creating a log file for this thread so that i will know, what happens later on.
now, this is my code for my thread class.
class ReadLinks extends Thread {
private $conn;
private $links;
private $fileObj;
public function __construct($conn, $links, $fileObj) {
//.. well just asign this to the global variables
}
public function run() {
try {
$this->logMsg("Start Reading Reviews");
$this->readLinks();
} catch (Exception $ex) {
$this->logMsg($ex);
}
$this->closeLog();
}
private function readLinks() {
$this->logMsg("Links");
foreach ($this->links as $link) {
$link = trim(preg_replace('/\s\s+/', ' ', $link));
$this->logMsg("Link: " . $link);
$html = html_readLink($link);
break;
}
}
private function logMsg($msg) {//something to write on the text file
}
private function closeLog() {//closes the textfile
}}
$conn - is my mysqli link to have db actions in the future
$links - is an array of links to be read.
$fileObj- is a resource return from fopen(). ( well to write into a file)
now who is that html_readlink,
its an outer function that is like this:
function html_readLink($link) {
return file_get_html($link);}
basically it is the resource returned by a function from simple html dom parser
now, i have as well a function that reads a link alone to do the other (different business requirement) and im using the simple html dom parser with ease.
with the pthreads, i tried writing the file(logs first) so that i can ensure that everything as well works fine.
about contacting db is not yet sure., well ill try to figure it out if it works fine, but logically it should work.
now when i called this class: its like this:
try {
$thread = new readLinks($conn, $Links, createlog());
if ($thread->start()) {
$thread->join();
} else {
echo "something i need to research if this happens";
}
} catch (Exception $err) {
echo $err; //something i need to research as well if this happens
}
i got this error
Warning: Invalid argument supplied for foreach() in C:\my\path\to\simplehtmldom_1_5\simple_html_dom.php on line 1119
that simplehtmldom code is:
function clear()
{
foreach ($this->nodes as $n) {$n->clear(); $n = null;}
// This add next line is documented in the sourceforge repository. 2977248 as a fix for ongoing memory leaks that occur even with the use of clear.
if (isset($this->children)) foreach ($this->children as $n) {$n->clear(); $n = null;}
if (isset($this->parent)) {$this->parent->clear(); unset($this->parent);}
if (isset($this->root)) {$this->root->clear(); unset($this->root);}
unset($this->doc);
unset($this->noise);
}
now that is the source code coming from simple html dom. that foreach is the one that is returning the error. now my other code using simple html dom doesn't have a problem with simple html dom. but with pthreads i got this error.
also, when i change my codes and didn't use pthreads, (had some revisions like this:
on pthreads class:
class ReadLinks {// extends Thread {
//insert other codes
public function readLinks() {
$this->logMsg("Links");
foreach ($this->links as $link) {
$link = trim(preg_replace('/\s\s+/', ' ', $link));
$this->logMsg("Link: " . $link);
$html = html_readLink($link);
$this->logMsg(getTitle($html));
//
break;
}
}
and change the way this is called like this:
try {
$thread = new ReadLinks($conn, $revLinks, createlog());
$thread->readLinks();
// if ($thread->start()) {
// $thread->join();
// } else {
// echo "something i need to research if this happens";
// }
} catch (Exception $err) {
echo $err; //something i need to debug and research if this happens
}
everything works fine, i get the desired results.
pthreads is something i need to use since loading bulk links and reading each of them is quite a time consuming process. and i need it to be on a separate thread. now i dont know whats wrong with these pthreads, or simple html dom parser. have i done something unnecessary/wrong? is there other way to do this?
anyone??
EDIT
i followed the answer of Prafulla Kumar Sahu:
the new code for the function clear() of simple html dom is:
function clear() {
if (is_array($this->nodes) || $this->nodes instanceof Traversable) {
foreach ($this->nodes as $n) {
$n->clear();
$n = null;
}
}
// This add next line is documented in the sourceforge repository. 2977248 as a fix for ongoing memory leaks that occur even with the use of clear.
if (isset($this->children))
foreach ($this->children as $n) {
$n->clear();
$n = null;
}
if (isset($this->parent)) {
$this->parent->clear();
unset($this->parent);
}
if (isset($this->root)) {
$this->root->clear();
unset($this->root);
}
unset($this->doc);
unset($this->noise);
}
the result is: it eliminated the error
but it is not the desired result
when using the function
$x=$resource->find($selector,0);
//resource is the return obj of file_gets_content, selector is my css selector string
it returns null/empty where in fact it should have a value.
ive checked a separate function that uses the simple html dom after i updated their code, seems it wasn't affected, and it is working properly. but with my pthread class, it is not working correctly.
The code I have doesn't have a foreach on line 1119; maybe you have an older version. You're getting a warning only, what problem(s) do you see in the results?
1117 // save dom as string
1118 function save($filepath='')
1119 {
1120 $ret = $this->root->innertext();
1121 if ($filepath!=='') file_put_contents($filepath, $ret, LOCK_EX);
1122 return $ret;
1123 }
It happens if the variable you are trying to traverse using foreach is not irritable so please check if your variable is either an array or instanceof Traversable class .
*It may be because you are not getting any value for that variable you want to traverse.
so, I would suggest you to use is_array( $whatever ) || $whatever instanceof Traversable just before foreach.
ie.
if( is_array( $whatever ) || $whatever instanceof Traversable ){
foreach( $whatever as $what ){
//some code
}
}
In your case it is
function clear()
{
foreach ($this->nodes as $n) {$n->clear(); $n = null;}
// This add next line is documented in the sourceforge repository. 2977248 as a fix for ongoing memory leaks that occur even with the use of clear.
if (isset($this->children)) foreach ($this->children as $n) {$n->clear(); $n = null;}
if (isset($this->parent)) {$this->parent->clear(); unset($this->parent);}
if (isset($this->root)) {$this->root->clear(); unset($this->root);}
unset($this->doc);
unset($this->noise);
}
source:- https://github.com/jalbertbowden/simplehtmldom/blob/master/simplehtmldom_1_5/simple_html_dom.php#L1119
this means you are unable to get $this->nodes correctly, so please var_dump it before you are calling function clear or before the foreach .

Undefined variable: _SESSION [CORE/src/Network/Session.php CakePHP 3.0

I am using CakePHP 3.0, and uploaded it to the server, it was working fine in local but somehow after uploading it to server it shows error
Notice (8): Undefined variable: _SESSION
[CORE/src/Network/Session.php, line 438]
If any have idea about that please help me. I am still trying to solve it.
I have also tried to put session_start(); in AppController and many other places but then it goes blank.
I have found the issue,
Its when I was uploading the files to servers it was automatically appending the white blank lines at the end of the AppController and also another controller files.
So when I removed the blank lines from that files it is working fine again
//To detect the file which have issue or extra space or early header start
//File location : vendor/cakephp/cakephp/src/Network/Session.php
public function write($name, $value = null)
{
if (empty($name)) {
return;
}
if (!$this->started()) {
$this->start();
}
$write = $name;
if (!is_array($name)) {
$write = [$name => $value];
}
//remove the comment from bottom line and run your code it will
//show you the exact file from where you have to remove the blank space
//session_start();
$data = $_SESSION ?: [];
foreach ($write as $key => $val) {
$data = Hash::insert($data, $key, $val);
}
$this->_overwrite($_SESSION, $data);
}
Small issue but took long time to detect it,hope this will help you all.
Place this in your php.ini file on your server
session.save_path = “/var/tmp”
You need make sure to start the session at the top of every PHP file where you want to use the $_SESSION superglobal. Like this:
<?php
session_start();
echo $_SESSION['youritem'];
?>
You forgot the Session HELPER.
Check this link : book.cakephp.org/2.0/en/core-libraries/helpers/session.html

PHP Switch-Case not working with certain string?

I'm trying to get switch/case to work with some variables and they aren't working and I am wondering why:
function convert_time($time_code) {
switch ($time_code) {
case "8:00a-10:00p":
return 1;
break;
}
}
Then the code that calls this function is:
$testvariable = "8:00a-10:00p";
$testtimecode = covert_time($testvariable);
echo "TTC: $testtimecode";
It always outputs "TTC:"
I went to PhpFiddle and tested it and it also doesn't work there, but I couldn't find a way to make a link to it like in jsfiddle.
However, if I do this code:
$time_code = "8:00a-10:00a";
if ($time_code == "8:00a-10:00a") {echo "yes";} else {echo "no";}
It will echo yes.
So my question is, what about the format of my 8:00a-10:00a is breaking the switch? and is it fixable.
Nevermind.
I found my problem and it was a typo.
It should have been 8:00a-10:00a, and it was 8:00a-10:00p.
sorry!
Got this to work on my local server:
function convert_time($time_code) {
switch ($time_code) {
case "8:00a-10:00p": return 1;
}
}
$testvariable = "8:00a-10:00p";
$testtimecode = convert_time($testvariable);
echo "TTC: $testtimecode";
not quite sure what could've happened on your end, possibly something wrong with your server itself but give this a shot.

Illegal string offset Warning PHP '#type' in VBridgeApp->create()

I get a strange PHP error after updating my php version to 5.4
This is my function
protected function create() {
//if (VBRIDGE_DEBUG)
//drupal_set_message(__CLASS__ .'::'.__METHOD__);
$path = $this->vbridge_root_path;
$path_vbridge = $path . '/' . VBridge::VBRIDGE_CLASS_PREFIX;
$subclass = $this->getClass();
foreach ($this->_objclass as $objclass) {
if (!$this->createObj($path, $objclass, $subclass)) {
$this->createObj($path_vbridge, $objclass);
}
}
if (self::getStatus()) {
return false;
}
// Set User Session Qookie
//$this->getUser()->setQookie($this->getQookie());
// Set User Session
$this->getUser()->setSession($this->getSession());
$this->getSession()->setQookie($this->getQookie());
//$this->getUser()->setAuth($this->getAuth());
// Set User Pass
$this->getUser()->setPass($this->getPass());
// Set Auth
$this->setAuthMethods();
$this->setAuthStorages();
//
foreach ($this->getConfig() as $config) {
if ($config['#type'] == '#class') {
//createObj($config['#name'], $config['#type'], $config['#class'], $config['#path'], $appData['#config']);
}
}
return true;
}
This is the line that gives
if ($config['#type'] == '#class') {
I've looked at similar questions but haven't figured out how to fix this. Any assistance would be helpful.
Edit: Yes, I did put wrong code up last night. I was very tired after trying to tangle with this.
The error message you've posted doesn't match the line of code you say it originates from. You should get a different error for that, specifically to do with only using variables by reference.
Your code should be
$accounts = user_load_multiple(array(), array('name' => $login));
$account = array_shift($account);
But Drupal already has a helper method for that, so you might as well use it:
$account = user_load_by_name($login);

Use of undefined constant - Learning OOP PHP

The undefined constant question isn't a new one, and I've tried Google already and found a million answers involving arrays with missing quotes and that's not my solution. I'm learning OOP coding with PHP and this is literally my first few hours working with it. This is the class I created.
class template {
var $page;
function __construct() {
$this->page = 'home';
}
function getHeader($header = 'header') {
include ('template/'.$header.'.php');
}
function getFooter($footer = 'footer') {
include ('template/'.$footer.'.php');
}
function setPage($page = 'home') {
$this->page = $page;
}
function getPage() {
if(page === 'home') {
include('template/home.php');
} else {
include('template/'.$this->page.'.php');
}
}
}
And this is how I instantiated it.
include('class.template.php');
$template = new template();
$template->getHeader();
if(isset($_GET['page'])) {
$template->setPage($_GET['page']);
}
$template->getPage();
$template->getFooter();
And of course the error - Notice: Use of undefined constant page - assumed 'page' in /Applications/MAMP/htdocs/titan-up/class.template.php on line 24
This is obviously something I should spot right away that I'm doing wrong but it's late and I'm at a loss. Any help is greatly appreciated.
Edit: Any links that make the learning process easier would be greatly appreciated. I'm already going through the PHP manual on it and have a strong background in PHP, but not OOP.
Change from this:
function getPage() {
if(page === 'home') {
To this:
function getPage() {
if($this->page === 'home') {
The error message "Notice: Use of undefined constant page - assumed 'page'" isn't terribly helpful, but it's due to the unfortunate fact that PHP will implicitly convert an unknown token as a constant with the same value.
That is it's seeing page (which doesn't have a $ in front of it and therefore isn't a variable name) and treating it as though there was a previous statement define('page', 'page');.
Note: Another common cause of this error message is if you forget to wrap a string in quotes - eg $some_array[some_key] instead of $some_array['some_key'].

Categories