[转]Parsing XML file using CodeIgniter’s SimpleXML library

This Simplexml class provides an alternative implementation of the SimpleXML API that works under PHP 4, so if you have an application that is running under PHP4 environment this is really helpful for you.

The original class was created by Taha Paksu of http://www.phpclasses.org and it was modified by Chris Brainard so that it would work with codeigniter.

Lets take a look at the code.

  1. <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');  
  2.   
  3. class Simplexml{  
  4.   
  5.     var $result = array();  
  6.     var $ignore_level = 0;  
  7.     var $skip_empty_values = false;  
  8.     var $php_errormsg;  
  9.     var $evalCode="";  
  10.   
  11.         function xml_parse($data){  
  12.                 $php_errormsg="";  
  13.         $this->result="";  
  14.         $this->evalCode="";  
  15.         $values="";  
  16.                 $encoding = 'UTF-8';  
  17.         $parser = xml_parser_create($encoding);  
  18.         xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0);  
  19.         xml_parser_set_option($parser, XML_OPTION_SKIP_WHITE, 1);  
  20.         $ok = xml_parse_into_struct($parser, $data, $values);  
  21.         if (!$ok) {  
  22.             $errmsg = sprintf("XML parse error %d '%s' at line %d, column %d (byte index %d)",  
  23.             xml_get_error_code($parser),  
  24.             xml_error_string(xml_get_error_code($parser)),  
  25.             xml_get_current_line_number($parser),  
  26.             xml_get_current_column_number($parser),  
  27.             xml_get_current_byte_index($parser));  
  28.         }  
  29.   
  30.         xml_parser_free($parser);  
  31.         return $this->xml_reorganize($values);  
  32.         }  
  33.   
  34.     function xml_reorganize($array)  
  35.     {  
  36.   
  37.         $count = count($array);  
  38.         $repeat = $this->xml_tags($array);  
  39.         $repeatedone = false;  
  40.         $tags = array();  
  41.         $k = 0;  
  42.         for ($i = 0; $i < $count; $i++) {  
  43.             switch ($array[$i]['type']) {  
  44.                 case 'open':  
  45.                     array_push($tags, $array[$i]['tag']);  
  46.                     if ($i > 0 && ($array[$i]['tag'] == $array[$i-1]['tag']) && ($array[$i-1]['type'] == 'close'))  
  47.                     $k++;  
  48.                     if (isset($array[$i]['value']) && ($array[$i]['value'] || !$this->skip_empty_values)) {  
  49.                         array_push($tags, '@content');  
  50.                         $this->array_insert(count($tags), $tags, $array[$i]['value'], "open");  
  51.                         array_pop($tags);  
  52.                     }  
  53.   
  54.                     if (in_array($array[$i]['tag'] . $array[$i]['level'], $repeat)) {  
  55.                         if (($repeatedone == $array[$i]['tag'] . $array[$i]['level']) && ($repeatedone)) {  
  56.                             array_push($tags, strval($k++));  
  57.                         } else {  
  58.                             $repeatedone = $array[$i]['tag'] . $array[$i]['level'];  
  59.                             array_push($tags, strval($k));  
  60.                         }  
  61.                     }  
  62.   
  63.                     if (isset($array[$i]['attributes']) && $array[$i]['attributes'] && $array[$i]['level'] != $this->ignore_level) {  
  64.                         array_push($tags, '@attributes');  
  65.                         foreach ($array[$i]['attributes'] as $attrkey => $attr) {  
  66.                             array_push($tags, $attrkey);  
  67.                             $this->array_insert(count($tags), $tags, $attr, "open");  
  68.                             array_pop($tags);  
  69.                         }  
  70.                         array_pop($tags);  
  71.                     }  
  72.                     break;  
  73.   
  74.                 case 'close':  
  75.                     array_pop($tags);  
  76.                     if (in_array($array[$i]['tag'] . $array[$i]['level'], $repeat)) {  
  77.                         if ($repeatedone == $array[$i]['tag'] . $array[$i]['level']) {  
  78.                             array_pop($tags);  
  79.                         } else {  
  80.                             $repeatedone = $array[$i + 1]['tag'] . $array[$i + 1]['level'];  
  81.                             array_pop($tags);  
  82.                         }  
  83.                     }  
  84.                     break;  
  85.   
  86.                 case 'complete':  
  87.                     array_push($tags, $array[$i]['tag']);  
  88.                     if (in_array($array[$i]['tag'] . $array[$i]['level'], $repeat)) {  
  89.                         if ($repeatedone == $array[$i]['tag'] . $array[$i]['level'] && $repeatedone) {  
  90.                             array_push($tags, strval($k));  
  91.                         } else {  
  92.                             $repeatedone = $array[$i]['tag'] . $array[$i]['level'];  
  93.                             array_push($tags, strval($k));  
  94.                         }  
  95.                     }  
  96.   
  97.                     if (isset($array[$i]['value']) && ($array[$i]['value'] || !$this->skip_empty_values)) {  
  98.                         if (isset($array[$i]['attributes']) && $array[$i]['attributes']) {  
  99.                             array_push($tags, '@content');  
  100.                             $this->array_insert(count($tags), $tags, $array[$i]['value'], "complete");  
  101.                             array_pop($tags);  
  102.                         } else {  
  103.                             $this->array_insert(count($tags), $tags, $array[$i]['value'], "complete");  
  104.                         }  
  105.                     }  
  106.   
  107.                     if (isset($array[$i]['attributes']) && $array[$i]['attributes']) {  
  108.                         array_push($tags, '@attributes');  
  109.                         foreach ($array[$i]['attributes'] as $attrkey => $attr) {  
  110.                             array_push($tags, $attrkey);  
  111.                             $this->array_insert(count($tags), $tags, $attr, "complete");  
  112.                             array_pop($tags);  
  113.                         }  
  114.                         array_pop($tags);  
  115.                     }  
  116.   
  117.                     if (in_array($array[$i]['tag'] . $array[$i]['level'], $repeat)) {  
  118.                         array_pop($tags);  
  119.                         $k++;  
  120.                     }  
  121.   
  122.                     array_pop($tags);  
  123.                     break;  
  124.             }  
  125.         }  
  126.         eval($this->evalCode);  
  127.         $last = $this->array_reindex($this->result);  
  128.         return $last;  
  129.     }     
  130.   
  131.     function array_insert($level, $tags, $value, $type)  
  132.     {  
  133.         $temp = '';  
  134.         for ($c = $this->ignore_level + 1; $c < $level + 1; $c++) {  
  135.             if (isset($tags[$c]) && (is_numeric(trim($tags[$c])) || trim($tags[$c]))) {  
  136.                 if (is_numeric($tags[$c])) {  
  137.                     $temp .= '[' . $tags[$c] . ']';  
  138.                 } else {  
  139.                     $temp .= '["' . $tags[$c] . '"]';  
  140.                 }  
  141.             }  
  142.         }  
  143.         $this->evalCode .= '$this->result' . $temp . "=\"" . addslashes($value) . "\";//(" . $type . ")\n";  
  144.         #echo $code. "\n";  
  145.     }  
  146.   
  147.     /** 
  148.      * Define the repeated tags in XML file so we can set an index 
  149.      * 
  150.      * @param array $array 
  151.      * @return array 
  152.      */  
  153.     function xml_tags($array)  
  154.     {   $repeats_temp = array();  
  155.     $repeats_count = array();  
  156.     $repeats = array();  
  157.   
  158.     if (is_array($array)) {  
  159.         $n = count($array) - 1;  
  160.         for ($i = 0; $i < $n; $i++) {  
  161.             $idn = $array[$i]['tag'].$array[$i]['level'];  
  162.             if(in_array($idn,$repeats_temp)){  
  163.                 $repeats_count[array_search($idn,$repeats_temp)]+=1;  
  164.             }else{  
  165.                 array_push($repeats_temp,$idn);  
  166.                 $repeats_count[array_search($idn,$repeats_temp)]=1;  
  167.             }  
  168.         }  
  169.     }  
  170.     $n = count($repeats_count);  
  171.     for($i=0;$i<$n;$i++){  
  172.         if($repeats_count[$i]>1){  
  173.             array_push($repeats,$repeats_temp[$i]);  
  174.         }  
  175.     }  
  176.     unset($repeats_temp);  
  177.     unset($repeats_count);  
  178.     return array_unique($repeats);  
  179.     }  
  180.   
  181.     /** 
  182.      * Converts Array Variable to Object Variable 
  183.      * 
  184.      * @param array $arg_array 
  185.      * @return $tmp 
  186.      */  
  187.     function array2object ($arg_array)  
  188.     {  
  189.   
  190.         if (is_array($arg_array)) {  
  191.             $keys = array_keys($arg_array);  
  192.             if(!is_numeric($keys[0])) $tmp = new Xml;  
  193.             foreach ($keys as $key) {  
  194.                 if (is_numeric($key)) $has_number = true;  
  195.                 if (is_string($key)) $has_string = true;  
  196.             }  
  197.             if (isset($has_number) and !isset($has_string)) {  
  198.                 foreach ($arg_array as $key => $value) {  
  199.                     $tmp[] = $this->array2object($value);  
  200.                 }  
  201.             } elseif (isset($has_string)) {  
  202.                 foreach ($arg_array as $key => $value) {  
  203.                     if (is_string($key))  
  204.                     $tmp->$key = $this->array2object($value);  
  205.                 }  
  206.             }  
  207.         } elseif (is_object($arg_array)) {  
  208.             foreach ($arg_array as $key => $value) {  
  209.                 if (is_array($value) or is_object($value))  
  210.                 $tmp->$key = $this->array2object($value);  
  211.                 else  
  212.                 $tmp->$key = $value;  
  213.             }  
  214.         } else {  
  215.             $tmp = $arg_array;  
  216.         }  
  217.         return $tmp; //return the object  
  218.     }  
  219.   
  220.     /** 
  221.      * Reindexes the whole array with ascending numbers 
  222.      * 
  223.      * @param array $array 
  224.      * @return array 
  225.      */  
  226.     function array_reindex($array)  
  227.     {  
  228.         if (is_array($array)) {  
  229.             if(count($array) == 1 && $array[0]){  
  230.                 return $this->array_reindex($array[0]);  
  231.             }else{  
  232.                 foreach($array as $keys => $items) {  
  233.                     if (is_array($items)) {  
  234.                         if (is_numeric($keys)) {  
  235.                             $array[$keys] = $this->array_reindex($items);  
  236.                         } else {  
  237.                             $array[$keys] = $this->array_reindex(array_merge(array(), $items));  
  238.                         }  
  239.                     }  
  240.                 }  
  241.             }  
  242.         }  
  243.   
  244.         return $array;  
  245.     }  
  246.   
  247. }  
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');  class Simplexml{  	var $result = array(); 	var $ignore_level = 0; 	var $skip_empty_values = false; 	var $php_errormsg; 	var $evalCode="";          function xml_parse($data){                 $php_errormsg=""; 		$this->result=""; 		$this->evalCode=""; 		$values="";             	$encoding = 'UTF-8'; 		$parser = xml_parser_create($encoding); 		xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0); 		xml_parser_set_option($parser, XML_OPTION_SKIP_WHITE, 1); 		$ok = xml_parse_into_struct($parser, $data, $values); 		if (!$ok) { 			$errmsg = sprintf("XML parse error %d '%s' at line %d, column %d (byte index %d)", 			xml_get_error_code($parser), 			xml_error_string(xml_get_error_code($parser)), 			xml_get_current_line_number($parser), 			xml_get_current_column_number($parser), 			xml_get_current_byte_index($parser)); 		}  		xml_parser_free($parser); 		return $this->xml_reorganize($values);         }  	function xml_reorganize($array) 	{  		$count = count($array); 		$repeat = $this->xml_tags($array); 		$repeatedone = false; 		$tags = array(); 		$k = 0; 		for ($i = 0; $i < $count; $i++) { 			switch ($array[$i]['type']) { 				case 'open': 					array_push($tags, $array[$i]['tag']); 					if ($i > 0 && ($array[$i]['tag'] == $array[$i-1]['tag']) && ($array[$i-1]['type'] == 'close')) 					$k++; 					if (isset($array[$i]['value']) && ($array[$i]['value'] || !$this->skip_empty_values)) { 						array_push($tags, '@content'); 						$this->array_insert(count($tags), $tags, $array[$i]['value'], "open"); 						array_pop($tags); 					}  					if (in_array($array[$i]['tag'] . $array[$i]['level'], $repeat)) { 						if (($repeatedone == $array[$i]['tag'] . $array[$i]['level']) && ($repeatedone)) { 							array_push($tags, strval($k++)); 						} else { 							$repeatedone = $array[$i]['tag'] . $array[$i]['level']; 							array_push($tags, strval($k)); 						} 					}  					if (isset($array[$i]['attributes']) && $array[$i]['attributes'] && $array[$i]['level'] != $this->ignore_level) { 						array_push($tags, '@attributes'); 						foreach ($array[$i]['attributes'] as $attrkey => $attr) { 							array_push($tags, $attrkey); 							$this->array_insert(count($tags), $tags, $attr, "open"); 							array_pop($tags); 						} 						array_pop($tags); 					} 					break;  				case 'close': 					array_pop($tags); 					if (in_array($array[$i]['tag'] . $array[$i]['level'], $repeat)) { 						if ($repeatedone == $array[$i]['tag'] . $array[$i]['level']) { 							array_pop($tags); 						} else { 							$repeatedone = $array[$i + 1]['tag'] . $array[$i + 1]['level']; 							array_pop($tags); 						} 					} 					break;  				case 'complete': 					array_push($tags, $array[$i]['tag']); 					if (in_array($array[$i]['tag'] . $array[$i]['level'], $repeat)) { 						if ($repeatedone == $array[$i]['tag'] . $array[$i]['level'] && $repeatedone) { 							array_push($tags, strval($k)); 						} else { 							$repeatedone = $array[$i]['tag'] . $array[$i]['level']; 							array_push($tags, strval($k)); 						} 					}  					if (isset($array[$i]['value']) && ($array[$i]['value'] || !$this->skip_empty_values)) { 						if (isset($array[$i]['attributes']) && $array[$i]['attributes']) { 							array_push($tags, '@content'); 							$this->array_insert(count($tags), $tags, $array[$i]['value'], "complete"); 							array_pop($tags); 						} else { 							$this->array_insert(count($tags), $tags, $array[$i]['value'], "complete"); 						} 					}  					if (isset($array[$i]['attributes']) && $array[$i]['attributes']) { 						array_push($tags, '@attributes'); 						foreach ($array[$i]['attributes'] as $attrkey => $attr) { 							array_push($tags, $attrkey); 							$this->array_insert(count($tags), $tags, $attr, "complete"); 							array_pop($tags); 						} 						array_pop($tags); 					}  					if (in_array($array[$i]['tag'] . $array[$i]['level'], $repeat)) { 						array_pop($tags); 						$k++; 					}  					array_pop($tags); 					break; 			} 		} 		eval($this->evalCode); 		$last = $this->array_reindex($this->result); 		return $last; 	}	  	function array_insert($level, $tags, $value, $type) 	{ 		$temp = ''; 		for ($c = $this->ignore_level + 1; $c < $level + 1; $c++) { 			if (isset($tags[$c]) && (is_numeric(trim($tags[$c])) || trim($tags[$c]))) { 				if (is_numeric($tags[$c])) { 					$temp .= '[' . $tags[$c] . ']'; 				} else { 					$temp .= '["' . $tags[$c] . '"]'; 				} 			} 		} 		$this->evalCode .= '$this->result' . $temp . "=\"" . addslashes($value) . "\";//(" . $type . ")\n"; 		#echo $code. "\n"; 	}  	/** 	 * Define the repeated tags in XML file so we can set an index 	 * 	 * @param array $array 	 * @return array 	 */ 	function xml_tags($array) 	{	$repeats_temp = array(); 	$repeats_count = array(); 	$repeats = array();  	if (is_array($array)) { 		$n = count($array) - 1; 		for ($i = 0; $i < $n; $i++) { 			$idn = $array[$i]['tag'].$array[$i]['level']; 			if(in_array($idn,$repeats_temp)){ 				$repeats_count[array_search($idn,$repeats_temp)]+=1; 			}else{ 				array_push($repeats_temp,$idn); 				$repeats_count[array_search($idn,$repeats_temp)]=1; 			} 		} 	} 	$n = count($repeats_count); 	for($i=0;$i<$n;$i++){ 		if($repeats_count[$i]>1){ 			array_push($repeats,$repeats_temp[$i]); 		} 	} 	unset($repeats_temp); 	unset($repeats_count); 	return array_unique($repeats); 	}  	/** 	 * Converts Array Variable to Object Variable 	 * 	 * @param array $arg_array 	 * @return $tmp 	 */ 	function array2object ($arg_array) 	{  		if (is_array($arg_array)) { 			$keys = array_keys($arg_array); 			if(!is_numeric($keys[0])) $tmp = new Xml; 			foreach ($keys as $key) { 				if (is_numeric($key)) $has_number = true; 				if (is_string($key)) $has_string = true; 			} 			if (isset($has_number) and !isset($has_string)) { 				foreach ($arg_array as $key => $value) { 					$tmp[] = $this->array2object($value); 				} 			} elseif (isset($has_string)) { 				foreach ($arg_array as $key => $value) { 					if (is_string($key)) 					$tmp->$key = $this->array2object($value); 				} 			} 		} elseif (is_object($arg_array)) { 			foreach ($arg_array as $key => $value) { 				if (is_array($value) or is_object($value)) 				$tmp->$key = $this->array2object($value); 				else 				$tmp->$key = $value; 			} 		} else { 			$tmp = $arg_array; 		} 		return $tmp; //return the object 	}  	/** 	 * Reindexes the whole array with ascending numbers 	 * 	 * @param array $array 	 * @return array 	 */ 	function array_reindex($array) 	{ 		if (is_array($array)) { 			if(count($array) == 1 && $array[0]){ 				return $this->array_reindex($array[0]); 			}else{ 				foreach($array as $keys => $items) { 					if (is_array($items)) { 						if (is_numeric($keys)) { 							$array[$keys] = $this->array_reindex($items); 						} else { 							$array[$keys] = $this->array_reindex(array_merge(array(), $items)); 						} 					} 				} 			} 		}  		return $array; 	}  } 

In this article we don’t need to understand what is in the code above. What is my aim is to show you how to use it.

Supposing you have an xml format like below, and you need it to present as a tabular data in an html page.

  1. <?xml version="1.0" encoding="UTF-8" standalone="yes"?>  
  2. <products xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">  
  3.   
  4.     <item>  
  5.         <id>1</id>  
  6.         <name>iPhone</name>  
  7.         <category>Mobile</category>  
  8.         <price>300</price>  
  9.     </item>  
  10.   
  11.     <item>  
  12.         <id>2</id>  
  13.         <name>iMac</name>  
  14.         <category>Desktop Computers</category>  
  15.         <price>2500</price>  
  16.     </item>  
  17.   
  18.     <item>  
  19.         <id>3</id>  
  20.         <name>MacBook Pro</name>  
  21.         <category>Mobile PC</category>  
  22.         <price>2000</price>  
  23.     </item>  
  24.   
  25.     <item>  
  26.         <id>4</id>  
  27.         <name>iTouch</name>  
  28.         <category>Gadgets</category>  
  29.         <price>150</price>  
  30.     </item>  
  31.   
  32.     <item>  
  33.         <id>5</id>  
  34.         <name>Wii</name>  
  35.         <category>Gaming</category>  
  36.         <price>1250</price>  
  37.     </item>  
  38.   
  39.     <item>  
  40.         <id>6</id>  
  41.         <name>Time Capsule</name>  
  42.         <category>Acessories</category>  
  43.         <price>1000</price>  
  44.     </item>  
  45.   
  46.     <item>  
  47.         <id>7</id>  
  48.         <name>Apple TV</name>  
  49.         <category>Gadgets</category>  
  50.         <price>800</price>  
  51.     </item>  
  52.   
  53. </products>  
<?xml version="1.0" encoding="UTF-8" standalone="yes"?> <products xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">  	<item> 		<id>1</id> 		<name>iPhone</name> 		<category>Mobile</category> 		<price>300</price> 	</item>  	<item> 		<id>2</id> 		<name>iMac</name> 		<category>Desktop Computers</category> 		<price>2500</price> 	</item>  	<item> 		<id>3</id> 		<name>MacBook Pro</name> 		<category>Mobile PC</category> 		<price>2000</price> 	</item>  	<item> 		<id>4</id> 		<name>iTouch</name> 		<category>Gadgets</category> 		<price>150</price> 	</item>  	<item> 		<id>5</id> 		<name>Wii</name> 		<category>Gaming</category> 		<price>1250</price> 	</item>  	<item> 		<id>6</id> 		<name>Time Capsule</name> 		<category>Acessories</category> 		<price>1000</price> 	</item>  	<item> 		<id>7</id> 		<name>Apple TV</name> 		<category>Gadgets</category> 		<price>800</price> 	</item>  </products> 

Lets start by creating our controller, to make it easier lets make use of the default controller in fresh CI installation which is the welcome controller. Now open you welcome controller and in you index function populate the code below.

  1. function index()  
  2. {  
  3.     //load the parser library  
  4.     $this->load->library('parser');  
  5.   
  6.               $data['title'] = 'Parsing XML using Simplexml class of CodeIgniter';  
  7.   
  8.               $data['products'] = $this->_getXML('myxml');  
  9.   
  10.        $this->parser->parse('table_view', $data);  
  11. }  
	function index() 	{ 		//load the parser library 		$this->load->library('parser');                 $data['title'] = 'Parsing XML using Simplexml class of CodeIgniter';                 $data['products'] = $this->_getXML('myxml');  	       $this->parser->parse('table_view', $data); 	} 

In this function we load the parser library, for those who dont know what a parser library is, its a simple templating engine of codeIgniter. Im using this almost always for me to get rid if the php tag in my view. Next we have a varialble title, and a variable products which calls the _getXML function with a string parameter myxml, we use this as reference of the filename for our XML file to be parse. Then load our table_view.

Lets add the _getXML function to our controller. Add this code in your welcome controller.

  1.  function _getXML($fname)  
  2.  {  
  3.   
  4.              $filename = $fname.'.xml';  
  5.              $xmlfile="./xml/".$filename;  
  6.              $xmlRaw = file_get_contents($xmlfile);  
  7.   
  8.              $this->load->library('simplexml');  
  9.              $xmlData = $this->simplexml->xml_parse($xmlRaw);  
  10.   
  11.              foreach($xmlData['item'] as $row)  
  12.              {  
  13.   
  14. $result .= '<tr>';  
  15. $result .= '<td>'.$row['id'].'</td>';  
  16. $result .= '<td>'.$row['name'].'</td>';  
  17. $result .= '<td>'.$row['category'].'</td>';  
  18. $result .= '<td>$ '.$row['price'].'</td>';  
  19. $result .= '</tr>';  
  20.   
  21.              }  
  22.               return $result;  
  23.      }  
    function _getXML($fname)     {                  $filename = $fname.'.xml';                 $xmlfile="./xml/".$filename;                 $xmlRaw = file_get_contents($xmlfile);                  $this->load->library('simplexml');                 $xmlData = $this->simplexml->xml_parse($xmlRaw);                  foreach($xmlData['item'] as $row)                 {  			$result .= '<tr>'; 			$result .= '<td>'.$row['id'].'</td>'; 			$result .= '<td>'.$row['name'].'</td>'; 			$result .= '<td>'.$row['category'].'</td>'; 			$result .= '<td>$ '.$row['price'].'</td>'; 			$result .= '</tr>';                  }                  return $result;         } 

This assume that the file location of your xml file is in the root of your CI installation an inside the xml folder. Then using the file_get_contents() function we load the xml data to $xmlRaw varialble. We loaded the simple XML library and then we populate it to the table element using foreach() function.

Now you only need to add a very little code in your view file.

  1. <table cellpadding="0" cellspacing="0">  
  2.     <thead>  
  3.     <th>  
  4.             <td>PRODUCT ID</td>  
  5.             <td>PRODUCT NAME</td>  
  6.             <td>CATEGORY</td>  
  7.             <td>PRICE</td>  
  8.     </th>  
  9.     </thead>  
  10.   
  11.     <tbody>  
  12.         {products}  
  13.     </tbody>  
  14.   
  15. </table>  
<table cellpadding="0" cellspacing="0"> 	<thead> 	<th> 			<td>PRODUCT ID</td> 			<td>PRODUCT NAME</td> 			<td>CATEGORY</td> 			<td>PRICE</td> 	</th> 	</thead>  	<tbody> 		{products} 	</tbody>  </table> 

Thats it!. Adding some quick css styling and a jQuery for table row stripe effect. You get something like this.

Adding a quick table stripe effect.

Download and include the jQuery library file in your view.

  1. <script type="text/javascript" src="public/js/jquery.js"></script>  
<script type="text/javascript" src="public/js/jquery.js"></script> 

Then add this line before the tag in your view file.

  1. <script type="text/javascript">  
  2.     $(document).ready(function(){  
  3.   
  4.         $("table tr:nth-child(even)").addClass("even");  
  5.   
  6.     });  
  7. </script>  
<script type="text/javascript"> 	$(document).ready(function(){  		$("table tr:nth-child(even)").addClass("even");  	}); </script> 

And you must have a style like this in your css file.

  1. table tr.even td{  
  2.     background: #cdded6;  
  3. }  
table tr.even td{     background: #cdded6; } 

Were done. Thanks for reading.


来自:

 http://blog.insicdesigns.com/2009/03/parsing-xml-file-using-codeigniters-simplexml-library/

原文地址:https://www.cnblogs.com/answer/p/1717032.html