/* This function will return the pre-owned cars listings from both database and xml */
/* Usage: display_preowned_short_list ( status, page number, no per page, with pager? ) */
/* Returns: String */
function display_preowned_short_list($status='available',$page=1,$npp=8,$ajax=0,$pager=1){
global $connect_id;
global $settings;
/* path to sgcarmart xml used car listing */
$xml_path = $settings['po_xml_list_path'];
// 1. Mapping of field names (MySQL and XML)
$field_map['id'] = 'po_id';
$field_map['model'] = 'po_make_model';
$field_map['price'] = 'po_price';
$field_map['img_link'] = 'po_thumb_name';
$field_map['status'] = 'po_status';
$field_map['posted'] = 'po_date_added';
$structure[0]='id';
$structure[1]='model';
$structure[2]='price';
$structure[3]='img_link';
$structure[4]='status';
$structure[5]='posted';
// 2. Use the following default values in cases of invalid or missing inputs
$query['page'] = $page;
$query['entries'] = $npp;
$query['orderby'] = "posted";
$query['order'] = "desc";
// 3. Misc variables (do not need to change)
$filtered = array();
$orderby_column = array();
$default_orderby_column_1 = array();
$default_orderby_column_2 = array();
/** Retrieve all pre-owned cars from local database **/
$sql = "SELECT po_id, CONCAT(po_make,' ', po_model) AS po_make_model, po_price, po_date_added,
CONCAT('".$settings['site_url'].$settings['po_image_path']."',po_thumb_name) AS po_thumb_name
FROM es_pocars
WHERE po_status = '$status' AND po_removed = 0 AND po_display = 1 ";
$result = mysql_query($sql,$connect_id) or die();
$exists = mysql_num_rows($result);
if($exists){
while($row = mysql_fetch_array($result)){
foreach($structure as $field){
$filtering[$field] = $row[$field_map[$field]];
}
$filtered[] = $filtering;
// default array column(s) for sorting, in order of priority
$default_orderby_column_1[] = $row[$field_map['posted']];
// selected array column for sorting
if($query['orderby'])
$orderby_column[] = $row[$field_map[$query['orderby']]];
}
}
/** Retrieve pre-owned cars from XML feed **/
$array = xml2array(file_get_contents($xml_path));
foreach ($array as $sgcarmart) {
foreach ($sgcarmart as $cars) {
foreach ($cars as $car) {
foreach ($car as $key => $attribute) {
// filter by status [ available / SOLD ]
if(preg_match('/'.$status.'/i',$attribute['status'])){
// store filtered
$filtered[] = $attribute;
// default array column(s) for sorting, in order of priority
$default_orderby_column_1[] = $row[$query['orderby']];
// selected array column for sorting
if($query['orderby'])
$orderby_column[] = $attribute[$query['orderby']];
}
}
}
}
}
/* Sort according to key (key is actually the selected sort field) */
if($query['orderby']){
// use selected sorting
if($query['order']=="desc"){
array_multisort($orderby_column, SORT_DESC, $filtered);
}else{
array_multisort($orderby_column, SORT_ASC, $filtered);
}
}else{
// use default prioritized sorting
array_multisort($default_orderby_column_1, SORT_DESC, $filtered);
}
/* Summary of pre-owned cars */
$total_from_xml = sizeof($array['sgcarmart']['cars']['car']);
$total_from_database = $exists;
$total_filtered = sizeof($filtered);
$total_filtered_pages = ceil($total_filtered/$query['entries']);
/* Generate listing according to searched and paginated results */
$displayed_counter = 0;
$counter = 1;
$start = ($query['page']-1)*$query['entries']+1;
$end = $query['page']*$query['entries'];
if(sizeof($filtered)){
if(!$ajax)
$html .= linefeed('
');
foreach($filtered as $row){
// Only pull data from results that falls within range
if($counter>=$start && $counter<=$end ){
if($row['status']=='Available'){
$link = 'pre-owned-details.php?id='.$row['id'];
}else{
$link = 'archive-details.php?id='.$row['id'];
}
$html .= linefeed('
');
}
return $html;
}
/* This function will return the new cars short listings from database */
/* Usage: display_preowned_short_list ( status, page number, no per page, with pager? ) */
/* Returns: String */
function display_new_short_list($page=1){
global $connect_id;
global $settings;
$html_new_cars = array();
/* 1. Use the following default values in cases of invalid or missing inputs */
$query['page'] = $page;
/* 2. Misc variables (do not need to change) */
$filtered = array();
$orderby_column = array();
$default_orderby_column_1 = array();
$default_orderby_column_2 = array();
/* 3. Retrieve all new cars from local database */
$sql = "SELECT nc_id, CONCAT(nc_make,' ', nc_model) AS nc_make_model, nc_featured_image FROM es_newcars
WHERE nc_featured = 1 AND nc_removed = 0 AND nc_display = 1 ";
$result = mysql_query($sql,$connect_id) or die();
$exists = mysql_num_rows($result);
if($exists){
while($row = mysql_fetch_array($result)){
$html_new_cars['image'] .= linefeed('');
$html_new_cars['image'] .= linefeed('');
$html_new_cars['image'] .= linefeed('');
if(!$html_new_cars['text']){
$html_new_cars['text'] = linefeed($row['nc_make_model']);
}
}
}
return $html_new_cars;
}
/* This function will read the pre-owned cars xml and return an array of cars */
/* Usage: xml2array ( xml, with attributes? , priority ) */
/* Returns: Array */
function xml2array($contents, $get_attributes=1, $priority = 'tag') {
if(!$contents) return array();
if(!function_exists('xml_parser_create')) {
return array();
}
// Get the XML parser of PHP - PHP must have this module for the parser to work
$parser = xml_parser_create('');
xml_parser_set_option($parser, XML_OPTION_TARGET_ENCODING, "UTF-8");
xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0);
xml_parser_set_option($parser, XML_OPTION_SKIP_WHITE, 1);
xml_parse_into_struct($parser, trim($contents), $xml_values);
xml_parser_free($parser);
if(!$xml_values) return;
// Initializations
$xml_array = array();
$parents = array();
$opened_tags = array();
$arr = array();
$current = &$xml_array; // Reference
// Go through the tags.
$repeated_tag_index = array();// Multiple tags with same name will be turned into an array
foreach($xml_values as $data) {
unset($attributes,$value);// Remove existing values, or there will be trouble
// This command will extract these variables into the foreach scope
// tag(string), type(string), level(int), attributes(array).
extract($data);// We could use the array by itself, but this cooler.
$result = array();
$attributes_data = array();
if(isset($value)) {
if($priority == 'tag') $result = $value;
else $result['value'] = $value; //Put the value in a assoc array if we are in the 'Attribute' mode
}
//Set the attributes too.
if(isset($attributes) and $get_attributes) {
foreach($attributes as $attr => $val) {
if($priority == 'tag') $attributes_data[$attr] = $val;
else $result['attr'][$attr] = $val; //Set all the attributes in a array called 'attr'
}
}
//See tag status and do the needed.
if($type == "open") {//The starting of the tag ''
$parent[$level-1] = &$current;
if(!is_array($current) or (!in_array($tag, array_keys($current)))) { //Insert New tag
$current[$tag] = $result;
if($attributes_data) $current[$tag. '_attr'] = $attributes_data;
$repeated_tag_index[$tag.'_'.$level] = 1;
$current = &$current[$tag];
} else { //There was another element with the same tag name
if(isset($current[$tag][0])) {//If there is a 0th element it is already an array
$current[$tag][$repeated_tag_index[$tag.'_'.$level]] = $result;
$repeated_tag_index[$tag.'_'.$level]++;
} else {//This section will make the value an array if multiple tags with the same name appear together
$current[$tag] = array($current[$tag],$result);//This will combine the existing item and the new item together to make an array
$repeated_tag_index[$tag.'_'.$level] = 2;
if(isset($current[$tag.'_attr'])) { //The attribute of the last(0th) tag must be moved as well
$current[$tag]['0_attr'] = $current[$tag.'_attr'];
unset($current[$tag.'_attr']);
}
}
$last_item_index = $repeated_tag_index[$tag.'_'.$level]-1;
$current = &$current[$tag][$last_item_index];
}
} elseif($type == "complete") { //Tags that ends in 1 line ''
//See if the key is already taken.
if(!isset($current[$tag])) { //New Key
$current[$tag] = $result;
$repeated_tag_index[$tag.'_'.$level] = 1;
if($priority == 'tag' and $attributes_data) $current[$tag. '_attr'] = $attributes_data;
} else { //If taken, put all things inside a list(array)
if(isset($current[$tag][0]) and is_array($current[$tag])) {//If it is already an array...
// ...push the new element into that array.
$current[$tag][$repeated_tag_index[$tag.'_'.$level]] = $result;
if($priority == 'tag' and $get_attributes and $attributes_data) {
$current[$tag][$repeated_tag_index[$tag.'_'.$level] . '_attr'] = $attributes_data;
}
$repeated_tag_index[$tag.'_'.$level]++;
} else { //If it is not an array...
$current[$tag] = array($current[$tag],$result); //...Make it an array using using the existing value and the new value
$repeated_tag_index[$tag.'_'.$level] = 1;
if($priority == 'tag' and $get_attributes) {
if(isset($current[$tag.'_attr'])) { //The attribute of the last(0th) tag must be moved as well
$current[$tag]['0_attr'] = $current[$tag.'_attr'];
unset($current[$tag.'_attr']);
}
if($attributes_data) {
$current[$tag][$repeated_tag_index[$tag.'_'.$level] . '_attr'] = $attributes_data;
}
}
$repeated_tag_index[$tag.'_'.$level]++; //0 and 1 index is already taken
}
}
} elseif($type == 'close') { //End of tag ''
$current = &$parent[$level-1];
}
}
return($xml_array);
}
/* This function will return a string of option for the car makes */
/* Usage: get_car_makes_dropdown_options ( string = name of current car make ) */
/* Returns: string */
function get_car_makes_dropdown_options($current_make=''){
global $connect_id;
$str = '';
$sql = "SELECT m_id, m_name
FROM es_makes
ORDER BY m_name ASC";
$result = mysql_query($sql,$connect_id);
$exists = mysql_num_rows($result);
if($exists){
while($row = mysql_fetch_array($result)){
if($current_make==$row['m_name']){
$str .= linefeed('');
}else{
$str .= linefeed('');
}
}
}
return $str;
}
?>