Welcome Guest, Not a member yet? Register   Sign In
SimpleXML to return a few items at a time
#7

[eluser]pickupman[/eluser]
You don't want to echo the variable $res, as this will output all of the xml to the browser you only want to fetch it. You are really close with your model. You need to integrate the code I had shown above.

1.) Check if xml is saved as an array in the cache
2.) If cache does not exists, use curl to fetch xml.
3.) Convert xml response to an array in a private method
4.) Save converted array to the cache
5.) Return array to controller.

Not sure exactly why you have the for loop inside of the foreach loop. It seems like you are trying to iterate the xml using both methods. What you really need to be doing is iterating the converted array. This allows you to reference the index to paginate the results. SimpleXML doesn't allow this feature directly unless you have the SimpleXML iterator class avaialble. I think the conversion of the xml will be much easier to support.

Then you can pass the array to your view, using the pagination class and the array, create the pagination links based on the number of keys (products) in the array. Then you can use the code I first posted, using the 3rd segment to offset the start of your for loop.
Code:
$this->load->library('pagination');
$this->load->model('catalog_model');

$xml = $this->catalog_model->get_xml($catid); //Get xml/array for catid

$config['base_url'] = site_url('products/'.$catid . '/');
$config['total_rows'] = count($xml);
$config['per_page'] = 20;
$this->pagination->initialize($config);

$data['pagination'] = $this->pagination->create_links(); //Pagination links for view

$i = $this->uri->segment(3,0); //Start array index based on 3rd uri segment (0 = default)

$data['products'] = '';
for($i < ($i + $config['per_page']); $i++)
{
   $data['products'] .= $xml[$i]['ProductID']['PrName'] . '<br/>'; //Append some data here
}

$this->load->view('catalog/products', $data);

//In view
<div id="products">
  &lt;?php echo $products;?&gt;
</div>


Messages In This Thread
SimpleXML to return a few items at a time - by El Forum - 06-29-2012, 07:35 PM
SimpleXML to return a few items at a time - by El Forum - 06-29-2012, 08:23 PM
SimpleXML to return a few items at a time - by El Forum - 06-29-2012, 08:56 PM
SimpleXML to return a few items at a time - by El Forum - 06-30-2012, 05:51 AM
SimpleXML to return a few items at a time - by El Forum - 07-03-2012, 05:25 PM
SimpleXML to return a few items at a time - by El Forum - 07-07-2012, 02:21 PM
SimpleXML to return a few items at a time - by El Forum - 07-07-2012, 02:52 PM
SimpleXML to return a few items at a time - by El Forum - 07-07-2012, 03:58 PM
SimpleXML to return a few items at a time - by El Forum - 07-07-2012, 05:02 PM
SimpleXML to return a few items at a time - by El Forum - 07-07-2012, 05:04 PM
SimpleXML to return a few items at a time - by El Forum - 07-07-2012, 07:32 PM
SimpleXML to return a few items at a time - by El Forum - 07-07-2012, 07:38 PM
SimpleXML to return a few items at a time - by El Forum - 07-12-2012, 02:32 PM
SimpleXML to return a few items at a time - by El Forum - 07-12-2012, 08:33 PM
SimpleXML to return a few items at a time - by El Forum - 12-08-2012, 03:22 PM
SimpleXML to return a few items at a time - by El Forum - 12-09-2012, 03:24 PM



Theme © iAndrew 2016 - Forum software by © MyBB