The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages - More
> XML Programming
|
Assistance / Guidance for XML feed
Discuss Assistance / Guidance for XML feed in the XML Programming forum on Dev Shed. Assistance / Guidance for XML feed XML Programming forum discussing XML and related technologies, including XUL and XSL. XML is a self-describing file format, designed for maximum compatibility between applications.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

December 20th, 2011, 07:33 AM
|
|
Contributing User
|
|
Join Date: Dec 2011
Posts: 39
Time spent in forums: 18 h 22 m 50 sec
Reputation Power: 2
|
|
|
Assistance / Guidance for XML feed
Hi all, my name is Phil, new to the Dev Shed and hope you experienced developers could assist me with a project I've recently taken on. However please bare with me while i get to grips with certain things.
Basically I need to include a XML file that is being linked from a different server to the one the site is on, and this XML feed is to display Car details for a Used Car section.
So firstly I have assumed that PHP was the best direction to go so to the file not being on the same server.
I need to implement a Search and Pagination function for the feed to allow users to search makes and models etc.
But with countless hours searching the internet (mostly here) I've implement pagination from a resource that I found which uses simplexml_load_file into array and works perfectly (however might not be the best) but cant figure out the best course of action for searching the array etc
Hope you can all help, see below the current stage I'am at and have hosted the DEMO XML which holds similar structure to real one but ID's etc are different
Code:
<?php
class Pagination {
protected
/*
* Configuration defaults
*/
$_arrConfig = array(
'limit'=>10
,'page'=>1
,'basePath'=>'#'
,'label'=>'Items'
,'visiblePages'=>5
,'pageFlag'=>'{page}'
)
/*
* Data to be passed to template
*/
,$_arrTemplateData = array()
/*
* function to pass resolved offset and limit
*/
,$_onPaginate
/*
* extra arguments to pass to to pagination callback
* such as data source or db connection
*/
,$_arrArgs;
public function __construct($onPaginate,$arrConfig=array(),$arrArgs=array()) {
$this->_onPaginate = $onPaginate;
$this->_arrArgs = $arrArgs;
foreach($arrConfig as $strKey=>$mixValue) {
if(array_key_exists($strKey,$this->_arrConfig)) {
$this->_arrConfig[$strKey] = $mixValue;
}
}
}
public function paginate() {
/*
* Calculate page offset
*/
$intOffset = ($this->_arrConfig['page']-1)*$this->_arrConfig['limit'];
/*
* Paginate and get number of found rows
*/
$intFoundRows = call_user_func_array($this->_onPaginate,array_merge(array($intOffset,$this->_arrConfig['limit']),$this->_arrArgs));
/*
* Assign template data
*/
$this->_arrTemplateData['limit'] = $this->_arrConfig['limit'];
$this->_arrTemplateData['page'] = $this->_arrConfig['page'];
$this->_arrTemplateData['offset'] = $intOffset;
$this->_arrTemplateData['found_rows'] = $intFoundRows;
$this->_arrTemplateData['total_pages'] = $intFoundRows < $this->_arrConfig['limit']?1:ceil($intFoundRows/$this->_arrConfig['limit']);
$this->_arrTemplateData['visible_pages'] = $this->_arrConfig['visiblePages'];
$this->_arrTemplateData['base_path'] = $this->_arrConfig['basePath'];
$this->_arrTemplateData['label'] = $this->_arrConfig['label'];
$this->_arrTemplateData['page_flag'] = $this->_arrConfig['pageFlag'];
/*
* Return the pagination template
*/
return $this->display();
}
public function display() {
extract($this->_arrTemplateData);
if($total_pages <= $visible_pages) {
$page_start = 1;
$page_end = $total_pages;
} else if($page <= ceil($visible_pages/2)) {
$page_start = 1;
$page_end = $visible_pages;
} else if($page > ($total_pages - ceil($visible_pages/2))) {
$page_start = $total_pages - (ceil(($visible_pages/2)*2)-1);
$page_end = $total_pages;
} else {
$page_start = $page-(floor($visible_pages/2));
$page_end = $page+(floor($visible_pages/2));
}
$return = sprintf(
'<div class="summary"><p class="pages">%u %s</p><p class="total">%u %s</p></div>'
,$total_pages
,$total_pages == 1?'Page':'Pages'
,$found_rows
,$found_rows == 1?$label:$label
);
$return.= sprintf('<ul class="pagination">');
$return.= sprintf(
'<li class="first">%s%s%s</li>'
,$page == 1?'':sprintf('<a href="%s">',str_replace($page_flag,1,$base_path))
,'First'
,$page == 1?'':'</a>'
);
$return.= sprintf(
'<li class="previous">%s%s%s</li>'
,$page == 1?'':sprintf('<a href="%s">',str_replace($page_flag,($page-1),$base_path))
,'Previous'
,$page == 1?'':'</a>'
);
foreach(range($page_start,$page_end,1) as $i) {
$return.= sprintf(
'<li%s>%s%s%s</li>'
,$page == $i?' class="current"':''
,$page == $i?'':sprintf('<a href="%s">',str_replace($page_flag,$i,$base_path))
,$i
,$page == $i?'':'</a>'
);
}
$return.= sprintf(
'<li class="next">%s%s%s</li>'
,$page == $total_pages?'':sprintf('<a href="%s">',str_replace($page_flag,($page+1),$base_path))
,'Next'
,$page == $total_pages?'':'</a>'
);
$return.= sprintf(
'<li class="last">%s%s%s</li>'
,$page == $total_pages?'':sprintf('<a href="%s">',str_replace($page_flag,$total_pages,$base_path))
,'Last'
,$page == $total_pages?'':'</a>'
);
$return.= sprintf('</ul>');
return $return;
}
}
/* Retrieve XML Data */
class Action {
private $_objXML;
private $_arrvehicles = array();
public function __construct() {
$this->_objXML = simplexml_load_file('http://formationdesigners.co.uk/mazda/demoXML.xml');
}
public function paginate($offset,$limit) {
$arrvehicles = $this->_objXML->xpath('vehicle');
if(isset($arrvehicles[$offset])) {
for($i=0;$i<$limit;$i++) {
if(!isset($arrvehicles[($i+$offset)])) break;
$this->_arrvehicles[] = array(
'vehicleID'=>(string) $arrvehicles[($i+$offset)]->vehicleID
,'condition'=>(string) $arrvehicles[($i+$offset)]->condition
,'make'=>(string) $arrvehicles[($i+$offset)]->make
,'model'=>(string) $arrvehicles[($i+$offset)]->model
,'variant'=>(string) $arrvehicles[($i+$offset)]->variant
,'engineSize'=>(string) $arrvehicles[($i+$offset)]->engineSize
,'colour'=>(string) $arrvehicles[($i+$offset)]->colour
,'fuelType'=>(string) $arrvehicles[($i+$offset)]->fuelType
,'transmission'=>(string) $arrvehicles[($i+$offset)]->transmission
,'regYear'=>(string) $arrvehicles[($i+$offset)]->regYear
,'price'=>(string) $arrvehicles[($i+$offset)]->price
);
}
}
// return number of total found rows
return count($arrvehicles);
}
public function getvehicles() {
return $this->_arrvehicles;
}
}
$action = new Action();
$func = create_function('$offset,$limit,$action','return $action->paginate($offset,$limit);');
$pg = new Pagination($func,array(
'basePath'=>$_SERVER['PHP_SELF'].'?p={replace_me_with_page}'
,'pageFlag'=>'{replace_me_with_page}'
,'label'=>'vehicles'
,'limit'=>8
,'page'=>isset($_GET['p']) && is_numeric($_GET['p']) && $_GET['p'] != 0?$_GET['p']:1
,'visiblePages'=>5
),array($action));
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html lang="en">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<title>Untitled</title>
<style type="text/css">
.pagination {
text-align: center;
}
.pagination li {
display: inline-block;
}
*+html .pagination li {
display: inline;
}
* html .pagination li {
display: inline;
}
</style>
</head>
<body>
<?php echo $pg->paginate(); ?>
<?php
if($action->getvehicles()) {
echo '<ol>';
foreach($action->getvehicles() as $arrvehicle) {
printf(
'<li>
<ul>
<li><p>%s</p></li>
<li><p>%s</p></li>
<li><p>%s</p></li>
<li><p>%s</p></li>
<li><p>%s</p></li>
<li><p>%s</p></li>
<li><p>%s</p></li>
<li><p>%s</p></li>
<li><p>%s</p></li>
<li><p>%s</p></li>
<li><p>%s</p></li>
</ul>
</li>'
,$arrvehicle['vehicleID']
,$arrvehicle['condition']
,$arrvehicle['make']
,$arrvehicle['model']
,$arrvehicle['variant']
,$arrvehicle['engineSize']
,$arrvehicle['colour']
,$arrvehicle['fuelType']
,$arrvehicle['transmission']
,$arrvehicle['regYear']
,$arrvehicle['price']
);
}
echo '</ol>';
} else {
echo "<p>No vehicles available</p>";
}?>
</body>
</html>
Heres a link also http://formationdesigners.co.uk/maz...Pagination1.php
Thanks all
Phil
|

December 21st, 2011, 03:30 AM
|
|
Contributing User
|
|
Join Date: Dec 2011
Posts: 39
Time spent in forums: 18 h 22 m 50 sec
Reputation Power: 2
|
|
|
Hi all,
Please advise if it would be best to post in PHP section, wasn't to sure
Thanks
Phil
|

January 3rd, 2012, 07:44 AM
|
|
Contributing User
|
|
Join Date: Dec 2011
Posts: 39
Time spent in forums: 18 h 22 m 50 sec
Reputation Power: 2
|
|
|
Nobody? Any advice would be helpful
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|