[solved]Turn a php class into CI library? - El Forum - 01-29-2010
[eluser]zimco[/eluser]
Yes, i read the manual, but I keep messing this up and my brain is fried. What am i doing wrong?
Here's the class i am trying to make into a library i can use:
Code: <?php
/**
* XMLToArray Generator Class
* @author : MA Razzaque Rupom <[email protected]>, <[email protected]>
* Moderator, phpResource (LINK1http://groups.yahoo.com/group/phpresource/LINK1)
* URL: LINK2http://www.rupom.infoLINK2
* @version : 1.0
* @date 06/05/2006
* Purpose : Creating Hierarchical Array from XML Data
* Released : Under GPL
*/
class Xmltoarray
{
var $xml='';
/**
* Default Constructor
* @param $xml = xml data
* @return none
*/
function Xmltoarray($xml)
{
$this->xml = $xml;
}
/**
* _struct_to_array($values, &$i)
*
* This is adds the contents of the return xml into the array for easier processing.
* Recursive, Static
*
* @access private
* @param array $values this is the xml data in an array
* @param int $i this is the current location in the array
* @return Array
*/
function _struct_to_array($values, &$i)
{
$child = array();
if (isset($values[$i]['value'])) array_push($child, $values[$i]['value']);
while ($i++ < count($values)) {
switch ($values[$i]['type']) {
case 'cdata':
array_push($child, $values[$i]['value']);
break;
case 'complete':
$name = $values[$i]['tag'];
if(!empty($name)){
$child[$name]= ($values[$i]['value'])?($values[$i]['value']):'';
if(isset($values[$i]['attributes'])) {
$child[$name] = $values[$i]['attributes'];
}
}
break;
case 'open':
$name = $values[$i]['tag'];
$size = isset($child[$name]) ? sizeof($child[$name]) : 0;
$child[$name][$size] = $this->_struct_to_array($values, $i);
break;
case 'close':
return $child;
break;
}
}
return $child;
}//_struct_to_array
/**
* createArray($data)
*
* This is adds the contents of the return xml into the array for easier processing.
*
* @access public
* @param string $data this is the string of the xml data
* @return Array
*/
function createArray()
{
$xml = $this->xml;
$values = array();
$index = array();
$array = array();
$parser = xml_parser_create();
xml_parser_set_option($parser, XML_OPTION_SKIP_WHITE, 1);
xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0);
xml_parse_into_struct($parser, $xml, $values, $index);
xml_parser_free($parser);
$i = 0;
$name = $values[$i]['tag'];
$array[$name] = isset($values[$i]['attributes']) ? $values[$i]['attributes'] : '';
$array[$name] = $this->_struct_to_array($values, $i);
return $array;
}//createArray
}//XmlToArray
?>
Here's my controller
Code: function myXml($scanDataFile) {
$xml_data = simplexml_load_string(file_get_contents($scanDataFile));
$this->load->library('XmlToArray');
//Creating Instance of the Class
$xmlObj = new Xmltoarray($xml_data);
//Creating Array
$arrayData = $this->xmlObj->createArray();
//Displaying the Array
echo "<pre>";
print_r($arrayData);
echo "</pre>";
exit;
} // End of function myXml
I keep getting errors like:
Undefined property: ...::$xmlObj
Missing argument 1
[solved]Turn a php class into CI library? - El Forum - 01-29-2010
[eluser]Shay Falador[/eluser]
You are doing this wrong.
The line:
Code: $xmlObj = new Xmltoarray($xml_data);
Will not create an object in the $this object.
This line:
Code: $this->load->library('XmlToArray');
DOES create an object (named XmlToArray) in the $this object..
In order to create the object named xmlObj in the $this you should do one of the following:
1) Instead of:
Code: $this->load->library('XmlToArray');
do:
Code: $this->load->library('XmlToArray','xmlObj');
2) Instead of:
Code: $xmlObj = new Xmltoarray($xml_data);
do:
Code: $this->xmlObj = new Xmltoarray($xml_data);
[solved]Turn a php class into CI library? - El Forum - 01-29-2010
[eluser]zimco[/eluser]
Thanks for the reply and explanations. So, i changed my controller as you suggested:
Code: function myXml($scanDataFile) {
$xml_data = simplexml_load_string(file_get_contents($scanDataFile));
$this->load->library('XmlToArray','xmlObj');
$this->xmlObj = new Xmltoarray($xml_data);
//Creating Array
$arrayData = $this->xmlObj->createArray();
//Displaying the Array
echo "<pre>";
print_r($arrayData);
echo "</pre>";
exit;
} // End of function myXml
Now i get back the following errors all related to the php class/Library. Did i do something wrong in setting up the library for use with CI?
Quote:A PHP Error was encountered
Severity: Warning
Message: Missing argument 1 for Xmltoarray::Xmltoarray(), called in /Library/WebServer/CodeIgniter_1.7.1/system/libraries/Loader.php on line 931 and defined
Filename: libraries/XmlToArray.php
Line Number: 24
A PHP Error was encountered
Severity: Notice
Message: Undefined variable: xml
Filename: libraries/XmlToArray.php
Line Number: 28
A PHP Error was encountered
Severity: Notice
Message: Undefined offset: 0
Filename: libraries/XmlToArray.php
Line Number: 100
[solved]Turn a php class into CI library? - El Forum - 01-29-2010
[eluser]Shay Falador[/eluser]
Well I was wrong and you were doing what I said wrong too
Code: function myXml($scanDataFile) {
$xml_data = simplexml_load_string(file_get_contents($scanDataFile));
$this->load->library('XmlToArray',$xml_data,'xmlObj');
//Creating Array
$arrayData = $this->xmlObj->createArray();
//Displaying the Array
echo "<pre>";
print_r($arrayData);
echo "</pre>";
exit;
} // End of function myXml
You should consider using REQUIRE_ONCE and calling it normally because you may want more than once..
[solved]Turn a php class into CI library? - El Forum - 01-30-2010
[eluser]zimco[/eluser]
D'oh Chalk it up to flatlining friday brain cells!
With your help and re-reading manual, i discovered that i have to send any parameters to a library as an array. So, by tweaking these lines:
Code: $params = array('xml' => $xml_data);
$this->load->library('Xmltoarray',$params,'xmlObj');
It sends the xml data to the library. Of course, i then have errors like array to string conversion to work out, but thanks for all your help. Maybe monday will be better day for my brain....
|