Get content from xml file in codeigniter - codeigniter

I have this code below which loads xml file from url
Question How can I get the data from it and use a foreach loop to display the content;
<?php defined('BASEPATH') OR exit('No direct script access allowed');
class Welcome extends CI_Controller {
public function index()
{
$url = "http://xml.someurl.nz/schedule?token=mytoken";
$xml = simplexml_load_file($url);
echo "<pre>";
print_r($xml);
echo "</pre>";
foreach ($xml['meetings'] as $race) {
echo $race['metting'];
}
$this->load->view('welcome_message');
}
}
It prints out below like
<schedule date="2018-03-01">
<meetings>
<meeting>
<betslip_type>STD</betslip_type>
<code>WAKG</code>
<country>nzl</country>
<date>2018-03-01</date>
<name>Cambridge Dogs</name>
<number>3</number>
<nz>1</nz>
<status>CL</status>
<type>GR</type>
<venue>Cambridge</venue>
<races>
<race>
<class>C0</class>
<length>375</length>
<name>WELCOME TO CAMBRIDGE RACEWAY SPRINT</name>
<norm_time>2018-03-01 12:00:00</norm_time>
<number>1</number>
<stake>$1,365.00</stake>
<status>CL</status>
<track>GOOD</track>
<weather>OVERCAST</weather>
<options>
<option>
<number>301</number>
<type>FFWIN</type>
</option>
<option>
<number>1301</number>
<type>FFPLC</type>
</option>
</options>
<entries>
<entry>
<name>Phuket Paul</name>
<number>1</number>
<scratched>0</scratched>
</entry>
<entry>
<name>Jetsun Paint</name>
<number>2</number>
<scratched>1</scratched>
</entry>
<entry>
<name>Bigtime Wayno</name>
<number>3</number>
<scratched>0</scratched>
</entry>
<entry>
<name>Feed The Need</name>
<number>4</number>
<scratched>0</scratched>
</entry>
<entry>
<name>Jetsun Woody</name>
<number>5</number>
<scratched>1</scratched>
</entry>
<entry>
<name>Snoopy's Bro</name>
<number>6</number>
<scratched>0</scratched>
</entry>
<entry>
<name>Crackling</name>
<number>7</number>
<scratched>0</scratched>
</entry>
<entry>
<name>Jinja Lad</name>
<number>8</number>
<scratched>0</scratched>
</entry>
<entry>
<barrier>2</barrier>
<name>Blitzem Gem</name>
<number>9</number>
<scratched>0</scratched>
</entry>
</entries>
</race>
<meeting>
<meetings>
</schedule>

$xml = simplexml_load_string($url);
$json = json_encode($xml);
$array = json_decode($json,TRUE);
for json output -> echo $json;
for php array -> print_r($array);
eg: -
foreach($array as $value) {
echo $value['metting'];
}

I worked it out now I had to do a foreach loop with in a foreach loop
public function index()
{
$url = "http://xml.someurl.co.nz/schedule?token=sometoken";
$xml = simplexml_load_file($url);
echo "<pre>";
foreach ($xml as $key) {
foreach ($key->meeting as $value) {
echo "$value->betslip_type";
}
}
echo "</pre>";
$this->load->view('welcome_message');
}

You can create helper function like....
<?php
function loadXML()
{
if(file_exists('xml_load/test.xml'))
{
$xml = simplexml_load_file('xml_load/test.xml');
}
else
{
exit('Failed to open test.xml.');
}
?>
<?php
$xmlDoc = new DOMDocument();
$xmlDoc->load("xml_load/test.xml");
$node_list = $xmlDoc->getElementsByTagName('userrole');
echo "<select name='con' class='popup_dd'>";
echo "<option value=''> Select Role</option>";
foreach($node_list AS $node)
{
$cur1 = $node->getElementsByTagName("uservalue");
$cur = $node->getElementsByTagName("name");
$v_cur = $cur->item(0)->nodeValue;
$v_cur1 = $cur1->item(0)->nodeValue;
echo "<option value=".$v_cur1.">". $v_cur. "</option>";
}
echo "</select>";
}
?>
And use this in view file <?php loadXML(); ?>
But make sure, you need to add helper file in autoload.php.
$autoload['helper'] = array('helpername');

first of all your XML is not valid please validate your XML then you can use foreach.
XML VALIDATOR
<schedule date="2018-03-01">
<meetings>
<meeting>
<betslip_type>STD</betslip_type>
<code>WAKG</code>
<country>nzl</country>
<date>2018-03-01</date>
<name>Cambridge Dogs</name>
<number>3</number>
<nz>1</nz>
<status>CL</status>
<type>GR</type>
<venue>Cambridge</venue>
<races>
<race>
<class>C0</class>
<length>375</length>
<name>WELCOME TO CAMBRIDGE RACEWAY SPRINT</name>
<norm_time>2018-03-01 12:00:00</norm_time>
<number>1</number>
<stake>$1,365.00</stake>
<status>CL</status>
<track>GOOD</track>
<weather>OVERCAST</weather>
<options>
<option>
<number>301</number>
<type>FFWIN</type>
</option>
<option>
<number>1301</number>
<type>FFPLC</type>
</option>
</options>
<entries>
<entry>
<name>Phuket Paul</name>
<number>1</number>
<scratched>0</scratched>
</entry>
<entry>
<name>Jetsun Paint</name>
<number>2</number>
<scratched>1</scratched>
</entry>
<entry>
<name>Bigtime Wayno</name>
<number>3</number>
<scratched>0</scratched>
</entry>
<entry>
<name>Feed The Need</name>
<number>4</number>
<scratched>0</scratched>
</entry>
<entry>
<name>Jetsun Woody</name>
<number>5</number>
<scratched>1</scratched>
</entry>
<entry>
<name>Snoopy's Bro</name>
<number>6</number>
<scratched>0</scratched>
</entry>
<entry>
<name>Crackling</name>
<number>7</number>
<scratched>0</scratched>
</entry>
<entry>
<name>Jinja Lad</name>
<number>8</number>
<scratched>0</scratched>
</entry>
<entry>
<barrier>2</barrier>
<name>Blitzem Gem</name>
<number>9</number>
<scratched>0</scratched>
</entry>
</entries>
</race>
</races>
</meeting>
</meetings>
now you can use foreach or what ever you want to use
$xml = simplexml_load_string($XML);
$json = json_encode($xml);
$array = json_decode($json,TRUE);
print_r($array);
YOU CANNOT USE ' single quote in XML VALUE LIKE THIS
<name>Snoopy's Bro</name>

Related

Images does not show up

I wonder what's wrong with my codes that the image does not show up as it suppose to. Nothing appears. Just blank. I expect an image would appears.
views/editgalleries.php
<?php foreach ($pictures as $pictures_item): ?>
<td><br><img src="<?php echo base_url('../uploads/'); ?><?php echo $pictures_item['galleries_picture_name']; ?>" height="300" width="200"></td>
<?php endforeach; ?>
Cpages.php
public function edit_galleries_picture()
{
$gallery_id = $this->uri->segment(3);
$data['pictures'] = $this->Mpages->call_gallery_pictures($gallery_id);
$this->load->view('editgalleries', $data);
}
models/Mpages.php
public function call_gallery_pictures($gallery_id)
{
$this->db->where('gallery_id', $gallery_id);
$query = $this->db->get('galleries_pictures');
return $query->result_array();
}
set uploads folder in root directory and try this code
<?php echo base_url('uploads/'.$pictures_item['galleries_picture_name']); ?>
<img src="<?php echo base_url('uploads'); ?>/<?php echo $pictures_item['galleries_picture_name']; ?>" height="300" width="200">
Note that for the 'base_url('../uploads/')',
the '../' part is not required because it's give your host name.

Array to string conversion Line Number: 56

A PHP Error was encountered
Severity: Notice
Message: Array to string conversion
Filename: views/editslideshows.php
Line Number: 56
Backtrace:
File: C:\Program Files\EasyPHP-Devserver-16.1\eds-www\companygiondaci\application\views\editslideshows.php
Line: 56
Function: _error_handler
File: C:\Program Files\EasyPHP-Devserver-16.1\eds-www\companygiondaci\application\controllers\Cpages.php
Line: 310
Function: view
File: C:\Program Files\EasyPHP-Devserver-16.1\eds-www\companygiondaci\index.php
Line: 315
Function: require_once
views/editslideshows.php
<?php foreach ($images as $images_item): ?>
<?php echo $images; ?>
<?php endforeach; ?>
controllers/Cpages.php
public function editslideshow() {
$image_id = $this->uri->segment(3);
$data['images'] = $this->Mpages->call_point_slideshow($image_id);
$this->load->view('editslideshows', $data);
}
models/Mpages.php
public function call_point_slideshow($image_id)
{
$this->db->where('image_id', $image_id);
$query = $this->db->get('slideshow');
return $query->result();
}
In your view page views/editslideshows.php
You used echo directly <?php echo $images; ?> that's why you getting error.
You can use $images_item->db_field_name. that's means any field name from your slideshow table
bellow my correction :
<?php foreach ($images as $images_item): ?>
<?php echo $images_item->db_field_name; ?>
<?php endforeach; ?>
In your view
<?php foreach ($images as $images_item) { ?>
<h3><?php echo $images_item->field_name; ?></h3>
<?php } ?>

Change the path of product images path in magento site

In product view page page, images are serving from this path : link1
media/catalog/product/cache/1/image/350x350/9df78eab33525d08d6e5fb8d27136e95/c/h/image-name.jpg :
but I want to serve from this path : link2
`media/cache/images/1/thumbnail/602f0fa2c1f0d1ba5e241f914e856ff9/catalog/product/c/image-name.jpg` :
media.phtml
<?php
$_product = $this->getProduct();
$_helper = $this->helper('catalog/output');
$dexxtz = Mage::helper('productzoom');
$dexxtz->getCss();
$dexxtz->getJs();
?>
<ul id="etalage">
<li>
<img class="etalage_thumb_image" src="<?php echo $dexxtz->getImageFeatured($this->helper('catalog/image')->init($_product, 'image')); ?>" />
<img class="etalage_source_image" title="<?php echo $_product->getImageLabel(); ?>" src="<?php echo $dexxtz->getImageFeatured($this->helper('catalog/image')->init($_product, 'image'), true); ?>" />
</li>
<?php
foreach ($this->getGalleryImages() as $_image) {
if(Mage::registry('current_product')->getImage() != $_image->getFile()) { ?>
<li>
<img class="etalage_thumb_image" src="<?php echo $dexxtz->getImageFeatured($this->helper('catalog/image')->init($this->getProduct(), 'image', $_image->getFile())); ?>" />
<img class="etalage_source_image" title="<?php echo $_image->getLabel(); ?>" src="<?php echo $dexxtz->getImageFeatured($this->helper('catalog/image')->init($this->getProduct(), 'image', $_image->getFile()), true); ?>" />
</li>
<?php
}
}
?>
</ul>
You need first to copy app/code/core/Mage/Catalog/Model/Product/Image.php to app/code/local/Mage/Catalog/Model/Product/Image.php.
Then take a look to the file you just copied, l.313-319 :
// build new filename (most important params)
$path = array(
Mage::getSingleton('catalog/product_media_config')->getBaseMediaPath(),
'cache',
Mage::app()->getStore()->getId(),
$path[] = $this->getDestinationSubdir()
);
This "$path" array will build your catalog image path. Change it to whatever you like. In your case :
// build new filename (most important params)
$path = array(
Mage::getBaseDir('media'),
'cache/images',
Mage::app()->getStore()->getId(),
$path[] = $this->getDestinationSubdir()
);
Dont forget to modify clear cache path too, l.686 :
public function clearCache()
{
$directory = Mage::getBaseDir('media') . DS.'catalog'.DS.'product'.DS.'cache'.DS;
... to ...
public function clearCache()
{
$directory = Mage::getBaseDir('media') . DS.'cache'.DS.'images'.DS;
Next, go to your media.phtml file. Change :
<?php echo $dexxtz->getImageFeatured($this->helper('catalog/image')->init($_product, 'image')); ?>
...
<?php echo $dexxtz->getImageFeatured($this->helper('catalog/image')->init($this->getProduct(), 'image', $_image->getFile())); ?>
... to ...
<?php echo $dexxtz->getImageFeatured($this->helper('catalog/image')->init($_product, 'thumbnail')); ?>
...
<?php echo $dexxtz->getImageFeatured($this->helper('catalog/image')->init($this->getProduct(), 'thumbnail', $_image->getFile())); ?>

How do I add a filter on a product attribute to Magento Advanced Export Profiles?

I want to filter my data to csv using the "Manufacturer" field. I tried this, but it didn't work:
<action type="catalog/convert_adapter_product" method="load">
<var name="store"><![CDATA[0]]></var>
<var name="filter/manufacturer"><![CDATA[898]]></var>
</action>
<action type="catalog/convert_parser_product" method="unparse">
<var name="store"><![CDATA[0]]></var>
<var name="url_field"><![CDATA[0]]></var>
</action>
<action type="dataflow/convert_mapper_column" method="map">
<var name="map">
<map name="sku"><![CDATA[sku]]></map>
<map name="name"><![CDATA[name]]></map>
</var>
<var name="_only_specified">true</var>
</action>
<action type="dataflow/convert_parser_csv" method="unparse">
<var name="delimiter"><![CDATA[,]]></var>
<var name="enclose"><![CDATA["]]></var>
<var name="fieldnames">true</var>
</action>
<action type="dataflow/convert_adapter_io" method="save">
<var name="type">file</var>
<var name="path">var/export</var>
<var name="filename"><![CDATA[safety-gates-export.csv]]></var>
</action>
Any thoughts on how to get this to work with filtering on other attributes than the default ones?
Unfortunately, from reading the code it looks like the set of attributes you can filter on is very limited.
$attrFilterArray = array();
$attrFilterArray ['name'] = 'like';
$attrFilterArray ['sku'] = 'startsWith';
$attrFilterArray ['type'] = 'eq';
$attrFilterArray ['attribute_set'] = 'eq';
$attrFilterArray ['visibility'] = 'eq';
$attrFilterArray ['status'] = 'eq';
$attrFilterArray ['price'] = 'fromTo';
$attrFilterArray ['qty'] = 'fromTo';
$attrFilterArray ['store_id'] = 'eq';
To filter on other attributes requires a minor extension to the class.
/app/code/local/YourCompany/YourModule/Model/DataFlow/Catalog/Product/Adapter.php
<?php
class YourCompany_YourModule_Model_DataFlow_Catalog_Product_Adapter
extends Mage_Catalog_Model_Convert_Adapter_Product
{
/**
* Extend the parent method to add filtering capability for additional fields
*
* This is required since the parent load() uses a parent::setFilter instead of $this->setFilter
*
* #return Mage_Dataflow_Model_Convert_Adapter_Interface
*/
public function load()
{
// Add any additional attributes you want to filter on here
$attrFilterArray = array(
'manufacturer' => 'eq',
);
$this->setFilter($attrFilterArray, array());
return parent::load();
}
}
/app/code/local/YourCompany/YourModule/etc/config.xml
<?xml version="1.0"?>
<config>
<modules>
<YourCompany_YourModule>
<version>0.0.1</version>
</YourCompany_YourModule>
</modules>
<global>
<models>
<catalog>
<rewrite>
<convert_adapter_product>YourCompany_YourModule_Model_DataFlow_Catalog_Product_Adapter</convert_adapter_product>
</rewrite>
</catalog>
</models>
</global>
</config>
I know this topic is old but this may help someone - works in Magento 1.9. This would be better as a Module but this is a "quick fix". Back up all files before uploading. I needed to add Product ID (from > to) and Manufacturer dropdown field but can easily be adopted for most attribute types.
FOR System > Import/Export > DataProfiles > Export All Products
app/code/core/Mage/Catalog/Model/Convert/Adapter/Product.php
copy to
app/code/local/Mage/Catalog/Model/Convert/Adapter/Product.php
FIND public function load()
Add Attributes to $attrFilterArray = array();
$attrFilterArray = array();
$attrFilterArray ['name'] = 'like';
$attrFilterArray ['entity_id'] = 'fromTo'; //Custom Field
$attrFilterArray ['manufacturer'] = 'eq'; //Custom Field
$attrFilterArray ['sku'] = 'startsWith';
$attrFilterArray ['type'] = 'eq';
$attrFilterArray ['attribute_set'] = 'eq';
$attrFilterArray ['visibility'] = 'eq';
$attrFilterArray ['status'] = 'eq';
$attrFilterArray ['price'] = 'fromTo';
$attrFilterArray ['qty'] = 'fromTo';
$attrFilterArray ['store_id'] = 'eq';
If type ="fromTo" you need to add an extra filter before return parent::load();
The following is for entity_id
if ($productId = $this->getFieldValue($filters, 'entity_id')) {
$this->_filter[] = array(
'attribute' => 'entity_id',
'from' => $productId['from'],
'to' => $productId['to']
);
$this->setJoinAttr(array(
'alias' => 'entity_id',
'attribute' => 'catalog_product/entity_id',
'bind' => 'entity_id',
'joinType' => 'LEFT'
));
}
Add the fields to the form
app/design/adminhtml/default/default/template/system/convert/profile/wizard.phtml
Duplicate as a Backup
Find
<div class="profile_entity_type_product">
Add extra Field Sets within this div
The following is for the fromTo for entity id
<span class="field-row">
<label for="product_filter_entity_id_from"><?php echo $this->__("Product ID:") ?></label>
<input class="input-text" style="width:5em" id="product_filter_entity_id_from" name="gui_data[product][filter][entity_id][from]" value="<?php echo $this->getValue('gui_data/product/filter/entity_id/from') ?>"/> <?php echo $this->__('to') ?>
<input class="input-text" style="width:5em" id="product_filter_entity_id_to" name="gui_data[product][filter][entity_id][to]" value="<?php echo $this->getValue('gui_data/product/filter/entity_id/to') ?>"/>
</span>
The following is for the Manufacturer dropdown
<span class="field-row">
<label for="product_filter_manufacturer"><?php echo $this->__("Manufacturer Name:") ?></label><select id="product_filter_manufacturer" name="gui_data[product][filter][manufacturer]">
<?php $manufacturer = Mage::getSingleton('eav/config')->getAttribute('catalog_product','manufacturer')->getSource()->getAllOptions(); ?>
<?php foreach ($manufacturer as $option): ?>
<option value="<?php echo $option['value'] ?>" <?php echo $this->getSelected('gui_data/product/filter/manufacturer', $option['value']) ?>><?php echo htmlspecialchars($option['label']) ?></option>
<?php endforeach ?>
</select>

List object properties delimited by comma in CodeIgniter with DataMapper OverZealous Edition

I have 2 classes - Students and Groups with many-to-many relationship. On a student page, I want to show all his details and list all groups he belongs to, delimited by comma.
This is my Students controller:
class Students extends Controller {
function __construct() {
parent::__construct();
}
function index() {
$this->get_all_students();
}
function get_all_students() {
$s = new Student();
$data['students'] = $s->select('id, name, email')->get();
$this->load->view('students', $data);
}
function view($id) {
$s = new Student();
$s->get_by_id($id);
$s->groups->get();
$data['student'] = $s;
$this->load->view('student_view', $data);
}
}
I can get student's details like this in student_view:
Name: <?php echo $student->name; ?>
E-mail: <?php echo $student->email; ?>
Groups:
<?php foreach ($student->groups as $group) : ?>
<?php echo anchor("/groups/$group->id", $group->name) ?>
<?php endforeach; ?>
So, how can I list groups delimited by comma? I tried adding group names to an array in the controller and then just <?php echo implode(', ', $groups); ?> in the view. But this way I cannot make a link using group IDs.
<?php echo anchor("/groups/$group->id", $group->name) ?>
should become ( see the comma at the end of the line )
<?php echo anchor("/groups/$group->id", $group->name) ?> ,
or display user groups as a list :
<?php foreach ($student->groups as $group) : ?>
<?php echo anchor("/groups/$group->id", $group->name) ?>
<?php endforeach; ?>
should become
<ul>
<?php foreach ($student->groups as $group) : ?>
<li><?php echo anchor("/groups/$group->id", $group->name) ?></li>
<?php endforeach; ?>
</ul>
<?php
$first = true;
foreach ($student->groups as $group) :
if (! $first) echo ', ';
echo anchor("/groups/$group->id", $group->name);
$first = false;
endforeach;
?>

Resources