I'm using a helper function to validate XML in Codeigniter.
My helper function is defined in xml_validation_helper.php and is as follows:
/**
* Function to generate a short html snippet indicating success
* or failure of XML loading
* #param type $xmlFile
*/
function validate_xml($xmlFile){
libxml_use_internal_errors(true);
$dom = new DOMDocument();
$dom->validateOnParse = true;
$dom->load($xmlFile);
if (!$dom->validate())
{
$result = '<div class="alert alert-danger"><ul>';
foreach(libxml_get_errors() as $error)
{
$result.="<li>".$error->message."</li>";
}
libxml_clear_errors();
$result.="</ul></div>";
}
else
{
$result = "<div class='alert alert-success'>XML Valid against DTD</div>";
}
return $result;
}
I'm using it in my controller (specifically in the index method) and that is as follows:
function index() {
$this->data['pagebody'] = "show_trends";
$this->load->helper("xml_validation");
$this->data['pokedex'] = display_file(DATA_FOLDER ."/xml/pokedex.xml");
$pokedexResult = validate_xml($this->data['pokedex']);
$this->data['gameSales'] = display_file(DATA_FOLDER . "/xml/sales.xml");
$gameSalesResult = validate_xml($this->data['gameSales']);
$this->render();
}
However, I keep getting a "Fatal error: Call to undefined function validate_xml() in C:\xampp\htdocs\project\application\controllers\show_trends.php on line 15 error, even though I can clearly load the file. I've even tried to move the function into the same file as the index method, but it still says it's undefined.
Why am I getting this error, even though this function is clearly defined?
Provided your helper is named the_helper_name_helper.php (it must end with _helper.php) and is located in the application/helpers you have to load the helper file using:
$this->load->helper('the_helper_name')
If you plan on using functions in this helper often, you better autoload it by adding 'the_helper_name' to the $config['helpers'] array in application/config/autoload.php
You must load libraries and helper files in contructor function
check it out
<?PHP
class controllername extends CI_Controller
{
public function __construct()
{
$this->load->helper("xml_validation");
}
public function index() {
$this->data['pagebody'] = "show_trends";
// $this->load->helper("xml_validation");
$this->data['pokedex'] = display_file(DATA_FOLDER ."/xml/pokedex.xml");
$pokedexResult = validate_xml($this->data['pokedex']);
$this->data['gameSales'] = display_file(DATA_FOLDER . "/xml/sales.xml");
$gameSalesResult = validate_xml($this->data['gameSales']);
$this->render();
}
}
?>
Related
i have a main php file which contains the variable:
$data['username']
which returns the username string correctly.
In this main file i included a class php file with:
require_once('class.php');
they seem linked together well.
My question is: how can I use the $data['username'] value inside the class file? I'd need to do an if statement to check its value inside that class.
class.php
<?php
class myClass {
function __construct() {
if ( $data['username'] == 'johndoe'){ //$data['username'] is null here
$this->data = 'YES';
}else{
$this->data = 'NO';
}
}
}
There are many ways to do that, we could give you accurate answer if we knew how your main php file and the class look like. One way of doing it, from the top of my head:
// main.php
// Instantiate the class and set it's property
require_once('class.php');
$class = new myClass();
$class->username = $data['username'];
// Class.php
// In the class file you need to have a method
// that checks your username (might look different in your class):
class myClass {
public $username = '';
public function __construct() {}
public function check_username() {
if($this->username == 'yourvalue') {
return 'Username is correct!';
}
else {
return 'Username is invalid.';
}
}
}
// main.php
if($class->username == 'yourvalue') {
echo 'Username is correct!';
}
// or
echo $class->check_username();
If the variable is defined before the call to require_once then you could access it with the global keyword.
main.php
<?php
$data = [];
require_once('class.php');
class.php
<?php
global $data;
...
If your class.php is defining an actual class then I would recommend Lukasz answer.
Based on your update I would add the data as a parameter in the constructor and pass it in on instantiation:
<?php
require_once('class.php');
$data = [];
new myClass($data);
Adjusting your constructor to have the signature __construct(array $data)
i have two helper classes
link are :
C:\xampp\htdocs\ecom\application\views\helpers\comman.php
C:\xampp\htdocs\ecom\application\views\helpers\RefineUrl.php
class Zend_View_Helper_Refinestr
{
public function Refinestr($str, $options = array()){
..............
.............
return $str;
}
}
second is
class Zend_View_Helper_Comman
{
public function Comman(){
return $this;
}
public function getPageContent($pageId){
// return $pageId;
$mapper = new Application_Model_StaticpageMapper();
$selectedFields=array('desc');
$tblName=array($mapper->getDbTable()->_name);
$whr= "`id`=$pageId";
$content=$mapper->fetchSelectedFields($tblName,$selectedFields,$whr);
$des=$content[0]['desc'];
// here i want to use function Refinestr() of another helper class how i use this
$des=$this->Refinestr($des);
// not working , searching this function inside comman class
} }
How to use one helper class function in another helper class function?
You can use below trick for your case.
While calling getPageContent() helper from your view file pass the view object in helper as a param (like $pageId) and use that view object to call another helper in helper definition.
View file:
<?php echo $this->getPageContent($pageId, $this); ?>
Helper File:
class Zend_View_Helper_GetPageContent {
public function getPageContent($pageId, $viewObj) {
// return $pageId;
$mapper = new Application_Model_StaticpageMapper ();
$selectedFields = array ('desc'
);
$tblName = array ($mapper->getDbTable ()->_name
);
$whr = "`id`=$pageId";
$content = $mapper->fetchSelectedFields ( $tblName, $selectedFields, $whr );
$des = $content [0] ['desc'];
// here i want to use function Refinestr() of another helper class how i
// use this
$des = $viewObj->Refinestr($des); //use view object to call another helper
}
}
Another helper will remain as it is.
One more solution to this problem could be, set view object in Zend Registry at the time of bootstrapping and use that registry variable in helper file to call another helper.
In Bootstrap File:
protected function _initConfig() {
$this->bootstrap('view');
$this->_view = $this->getResource('view');
Zend_Registry::set('viewObj', $this->_view);
}
Helper File:
class Zend_View_Helper_GetPageContent {
public function getPageContent($pageId) {
// return $pageId;
$mapper = new Application_Model_StaticpageMapper ();
$selectedFields = array ('desc');
$tblName = array ($mapper->getDbTable ()->_name);
$whr = "`id`=$pageId";
$content = $mapper->fetchSelectedFields ( $tblName, $selectedFields, $whr );
$des = $content [0] ['desc'];
// here i want to use function Refinestr() of another helper class how i
// use this
$viewObj = Zend_Registry::get('viewObj');
$des = $viewObj->Refinestr($des); //use view object to call another helper
}
}
I usually do the following:
Inside helper1
$this->helper1()->view->helper2();
In case helper1 is taking some arguments, I modify it to take no arguments and just return. Try it out, may work.
I have to parse a huge csv files in a Yii 1.1 Application.
Each row has to be validated and saved to the database.
I decided to use Multi Threading for this task.
So here is my code in the Controller action:
public function parseData($) {
$this->content = explode("\n", $this->content);
$thread_1 = new DatalogThread(array_slice($this->content, 0, 7000));
$thread_2 = new DatalogThread(array_slice($this->content, 7001));
$thread_1->start();
$thread_2->start();
}
And the Thread (I put it in models folder):
class DatalogThread extends Thread {
public $content;
public function __construct($content) {
$this->content = $content;
}
public function run() {
foreach ($this->content as $value) {
$row = str_getcsv($value);
$datalog = new Datalog($row);
$datalog->save();
}
}
}
The problem is that the Thread does not get access to the model file:
Fatal error: Class 'Datalog' not found in C:\xampp...\protected\models\DatalogThread.php
I tried Yii::autoload("Datalog"), but got The following error:
Fatal error: Cannot access property Yii::$_coreClasses in ...\YiiMain\framework\YiiBase.php on line 402
Yii uses a LOT of statics, this is not the best kind of code for multi-threading.
What you want to do is initialize threads that are not aware of Yii and reload it, I do not use Yii, but here's some working out to give you an idea of what to do:
<?php
define ("MY_YII_PATH", "/usr/src/yii/framework/yii.php");
include (MY_YII_PATH);
class YiiThread extends Thread {
public $path;
public $config;
public function __construct($path, $config = array()) {
$this->path = $path;
$this->config = $config;
}
public function run() {
include (
$this->path);
/* create sub application here */
}
}
$t = new YiiThread(MY_YII_PATH);
$t->start(PTHREADS_INHERIT_NONE);
?>
This will work much better ... I should think you want what yii calls a console application in your threads, because you don't want it trying to send any headers or anything like that ...
That should get you started ...
I have the controller
<?php
class Onetimescan extends CI_Controller{
public function index() {
#LOAD -->>>> LIB ###########
$this->load->library('session'); ## LOAD LIBS: SESSION
$this->load->helper('simpledom');
#LOAD -->>>> MODEL
$this->load->model('scanmodel');
$this->scanmodel->loadurlbasedonsessid($this->session->userdata('session_id')); // echo out inserted domain in loadurlbasedonsessid() func
$sss= $this->session->userdata('tld'); // echo $sss;
$siteToSearch = file_get_html($sss);
foreach($siteToSearch->find('form') as $element){
echo "<h1 style='color:red; '>".$element->action."</h1> <br />";
}
}
}
?>
I'm getting a fatal error of Call to a member function find() on a non-object on this line:
foreach($siteToSearch->find('form') as $element){
simpledom is a helper I loaded at the top (actually called simpledom_helper.php) which has a file_get_html defined like:
[http://pastebin.com/HaJtKfNb][1]
What am I doing wrong here? I tried definining the function like public function file_get_html{} but that threw errors.
I fixed this all by adding:
$prefix = "http://www.";
//echo "SSS is: ".$sss;
$siteToSearch = file_get_html($prefix.$sss);
FIXED>>
The url was trying to get_file_contents(somedomain.com) and was missing the http://www. as a fully qualified domain. This function get_file_contents seems to require the http://www.
Helper functions should be called like this since they are not objects:
$this->load->helper('simpledom');
$siteToSearch = file_get_html($sss);
I followed the documentation on Models and I keep getting this error, any help would be appreciated.
A PHP Error was encountered
Severity: Notice
Message: Undefined property: Manage::$File
Filename: files/manage.php
Line Number: 14
Fatal error: Call to a member function get_files() on a non-object in /var/www/uisimulator/application/controllers/files/manage.php on line 14*
Here is my Model: - Located: application/models/files/file.php
class File extends CI_Model {
var $filename = '';
function __construct() {
parent::__construct();
}
// Return all config files
function get_files() {
$query = $this->db->get('config_files');
return $query->result();
}
function insert_file() {
$this->filename = $this->input->post('filename');
$this->db->insert('config_files', $this);
}
function update_file() {
$this->filename = $this->input->post('filename');
$this->db->update('config_files', $this, array('id' => $this->input->post('id'));
}
}
Here is my Controller: Location: application/controllers/files/manage.php
class Manage extends CI_Controller {
function __construct() {
parent::__construct();
}
public function index() {
$this->load->model('files/file', TRUE);
$config_files['query'] = $this->File->get_files();
// Load the head section
$this->load->view('head');
// Load the view, passing in the data
$this->load->view('files/index', $configFiles);
// Load Footer
$this->load->view('footer');
}
}
Inside my view I have a simple loop to show the results:
<?php foreach ($configFiles as $file) : ?>
<li><?php echo $file['filename'];?></li>
<?php endforeach;?>
Try:
$this->load->model('files/file','', TRUE);
EDITED:
$data['configFiles'] = $this->File->get_files();
// Load the head section
$this->load->view('head');
// Load the view, passing in the data
$this->load->view('files/index', $data);
This code result one record, but give me only fields from one table.
function listar_dados_produto($id)
{
$this->db->where('id',$id);
$query = $this->db->get('produtos');
return $query->result();
}
Note: I am using activerecord. The table produtos return their fields corectly, but i need one field
for another table named categorias.
Any reason you have passed TRUE as the second parameter in the $this->load->model method?
My understanding with CI is that the second parameter is to refer to your model by a different name, other than it's class.
It would appear you are naming your class a boolean, which is bound to cause some kind of error.
Try:
$this->load->model('files/file', NULL, TRUE);
//$data['configFiles'] = $this->File->get_files();
print_r($this->File->get_files());
exit;
// do you get any data?
// Load the head section
$this->load->view('head');
// Load the view, passing in the data
$this->load->view('files/index', $data);