?
Solved

PHP Array Pagination from XML Data

Posted on 2009-04-26
5
Medium Priority
?
1,089 Views
Last Modified: 2013-12-12
Hi There,

I have a list of results which are being loaded in from an xml file and there are over 50 results on some searches and I want to have pagination for this so I can show 10 results per page.

I have tried several classes and I just don't know how to add pagination onto my code I have now.
If someone can please help me add pagination to this that would be great!

Please see below my code:

Cheers,
George
<?php
 
$searchtype = Trim(stripslashes($_POST['searchtype']));
$location = Trim(stripslashes($_POST['suburbs']));
$pricefrom = Trim(stripslashes($_POST['pricefrom']));
$priceto = Trim(stripslashes($_POST['priceto']));
$propertytype = Trim(stripslashes($_POST['propertyType']));
 
$xml_file ="http://www.demo.demosite.com.au/cgi-bin/clients/demo/getlistings_xml.cgi?clientid=demo&searchtype=$searchtype&lower=$pricefrom&upper=$priceto&suburb=$location&proptype=$propertytype";
 
$xml_photo_key = "*PROPERTIES*PROPERTY*PHOTOS*PHOTO*PHOTOLINK";
$xml_title_key = "*PROPERTIES*PROPERTY*TITLE";
$xml_about_key = "*PROPERTIES*PROPERTY*ABOUT";
$xml_beds_key = "*PROPERTIES*PROPERTY*BEDS";
$xml_baths_key = "*PROPERTIES*PROPERTY*BATHS";
$xml_cars_key = "*PROPERTIES*PROPERTY*CARS";
$xml_suburb_key = "*PROPERTIES*PROPERTY*SUBURB";
$xml_streetaddress_key = "*PROPERTIES*PROPERTY*STREETADDRESS";
$xml_agentName_key = "*PROPERTIES*PROPERTY*AGENTS*AGENT*NAME";
$xml_agentPhone_key = "*PROPERTIES*PROPERTY*AGENTS*AGENT*PHONE";
$xml_agentMobile_key = "*PROPERTIES*PROPERTY*AGENTS*AGENT*MOBILE";
$xml_propertyID_key = "*PROPERTIES*PROPERTY*PROPERTYID";
$xml_price_key = "*PROPERTIES*PROPERTY*PRICE";
 
$searchListingArray = array();
 
$counter = -1;
class property_listing {
    public $PhotoLink;
    public $Title;
    public $About;
    public $Beds;
    public $Baths;
    public $Cars;
    public $Suburb;
    public $StreetAddress;
    public $Name;
    public $Phone;
    public $Mobile;
    public $propertyID;
    public $Price; 
}
 
 
function startTag($parser, $data,$element_attrs){
    global $current_tag;
 
    $current_tag .= "*$data";
    //echo "<br>$current_tag<br>";
}
 
function endTag($parser, $data){
    global $current_tag;
    $tag_key = strrpos($current_tag, '*');
    $current_tag = substr($current_tag, 0, $tag_key);
    //echo "<br>---- end tag ----<br>";
}
 
function contents($parser, $data) {
    global 	$current_tag, $xml_photo_key, 
				$xml_title_key, $xml_about_key, 
				$xml_beds_key, $xml_suburb_key,
				$xml_baths_key, $xml_cars_key,
				$xml_streetaddress_key,
				$xml_agentName_key, 
				$xml_agentPhone_key, 
				$xml_agentMobile_key, 
				$xml_propertyID_key, 
				$xml_price_key, 
				$counter, 
				$searchListingArray;
				
    switch($current_tag) {
		
		case $xml_title_key:
			$searchListingArray[$counter]->Title .= $data;
			//echo $searchListingArray[$counter]->Title;
			break;
			
		case $xml_photo_key:
            $searchListingArray[$counter]->PhotoLink .= $data;
			//$searchListingArray[$counter]->PhotoLink = substr($data,0);
			//$p_data = substr($data,0); 
			//echo "<br> *** $data *** <br>";
			//echo " <<<< ".$searchListingArray[$counter]->PhotoLink ." >>>>";
            break;
			
		case $xml_about_key:
            $searchListingArray[$counter]->About .= $data;
			//echo "- $data -";
            break;
			
		case $xml_beds_key:
			$searchListingArray[$counter]->Beds = $data;
			break;
			
		case $xml_baths_key:
			$searchListingArray[$counter]->Baths = $data;
			break;
			
		case $xml_cars_key:
			$searchListingArray[$counter]->Cars = $data;
			break;
			
		case $xml_streetaddress_key:
			$searchListingArray[$counter]->StreetAddress = $data;
			break;
			
		case $xml_suburb_key:
			$searchListingArray[$counter]->Suburb .= $data;
			break;
			
		case $xml_agentName_key:
            $searchListingArray[$counter]->Name = $data;
            break;
			
		case $xml_agentPhone_key:
            $searchListingArray[$counter]->Phone = $data;
            break;
			
		case $xml_agentMobile_key:
            $searchListingArray[$counter]->Mobile = $data;
            break;
			
		case $xml_propertyID_key:
			$counter++; //echo $counter;
			$searchListingArray[$counter] = new property_listing();		
            $searchListingArray[$counter]->PropertyID = $data;
            break;
			
		case $xml_price_key:
            $searchListingArray[$counter]->Price = $data;
            break;
    }
}
 
$xml_parser = xml_parser_create('ISO-8859-1');
 
xml_set_element_handler($xml_parser, "startTag", "endTag");
 
xml_set_character_data_handler($xml_parser, "contents");
 
 
$fp = fopen($xml_file, "r") or die("Could not open file");
 
$data = stream_get_contents($fp) or die("Could not read file");
 
$value_arr = array();
$index_arr = array();
 
xml_parse_into_struct($xml_parser,$data,$value_arr,$index_arr);
 
if(!(xml_parse($xml_parser, $data, feof($fp)))){
    die("Error on line " . xml_get_current_line_number($xml_parser));
}
 
xml_parser_free($xml_parser);
 
fclose($fp);
 
?>
 
 
<?php
 
for($x=0;$x<count($searchListingArray);$x++) {
if($x % 2) { $propSuburb = 'property-suburb-light'; } else { $propSuburb = 'property-suburb'; }
echo '<div class="property-listing">';
//echo $x;
 
    $propertySuburb = '
				<div class="' . $propSuburb .'">
					<h5><a href="'. $path .'property-details/index.php?propertyid='  . $searchListingArray[$x]->PropertyID . '" target="_self">' . $searchListingArray[$x]->Suburb . '</a></h5>
				</div>';
				echo $propertySuburb;
				
	$propertyPhoto = '
			<div class="property-photo"><img src="' . $searchListingArray[$x]->PhotoLink . '" /></div>';
			echo $propertyPhoto;
	
	$propertyDesc = '
			<div class="description">
				<div class="property-title">
					<h1><a href="'. $path .'property-details/index.php?propertyid='  . $searchListingArray[$x]->PropertyID . '" target="_self">' . $searchListingArray[$x]->Title . '</a></h1>
				</div>';
	
    $propertyDesc .= '
				<div class="property-about">
					<p>' . $searchListingArray[$x]->About . ' 
						<a href="'. $path .'property-details/index.php?propertyid='  . $searchListingArray[$x]->PropertyID . '" target="_self" class="more">more &raquo;</a>
					</p>
				</div>';
				
	$propertyDesc .= '
				<div class="prop-details">
						<p>
							'  . $searchListingArray[$x]->Beds . ' <img src="' . $img . 'content/buy/bed.jpg" />
							'  . $searchListingArray[$x]->Baths . ' <img src="' . $img . 'content/buy/bath.jpg"/>
							'  . $searchListingArray[$x]->Cars . ' <img src="' . $img . 'content/buy/cars.jpg" />
						</p>
				</div>
			</div>';
			echo $propertyDesc;
 
    $agentDetails = '
			<div class="property-left-col">
				<div class="prop-address">
					<p><span class="results-sub-heading">ADDRESS</span><br />
					'  . $searchListingArray[$x]->StreetAddress . '</p>
				</div>';
			
	$agentDetails .= '
				<div class="property-agent-name">
					<p><span class="results-sub-heading">AGENT</span><br />
					' . $searchListingArray[$x]->Name . '<br />
					'  . $searchListingArray[$x]->Mobile . '</p>
				</div>';
				echo $agentDetails;
				
	$price = '
				<div class="property-price">
					<p><span class="results-sub-heading">PRICE</span><br />
					'  . $searchListingArray[$x]->Price . '</p>
				</div>
			</div>';
				echo $price;
echo '</div>';
 
echo '<div class="clear">&nbsp;</div>';
}
 
 
 
?>

Open in new window

0
Comment
Question by:George-TCC
  • 3
4 Comments
 
LVL 14

Accepted Solution

by:
shobinsun earned 1500 total points
ID: 24238830
Hi,

Here I am posting a very good tutorial about this:

http://www.sitepoint.com/article/perfect-php-pagination/

Also look at this:

http://www.lotsofcode.com/php/php-array-pagination.htm


Hope this will help you.

Thanks and Regards
0
 

Author Comment

by:George-TCC
ID: 24238968
Hi There,

The second one is the one I am trying at the moment and it kind of works the only problem I am having is capturing my results into an array that I can use with the class.

There example works as it is an easy array but trying to put my xml results into that is confusing me.

I will post my new version of code I have - hope this helps.

Thanks,
George
for($x=0;$x<count($searchListingArray);$x++) {
	$props[] = array(
		$searchListingArray[$x]->PropertyID,
		$searchListingArray[$x]->PhotoLink,
		$searchListingArray[$x]->About,
	);
}
 
// Create the pagination object
$pagination = new pagination;
 
// If we have an array with items
if (count($props)) {
 
	// Parse through the pagination class
    $propertyPages = $pagination->generate($props, 5);
    
	// If we have items 
    if (count($propertyPages) != 0) {
	
    	// Create the page numbers
    	echo $pageNumbers = '<div class="numbers">'.$pagination->links().'</div>';
        
		// Loop through all the items in the array
		foreach ($propertyPages as $propArray) {
			//for ($x=0;$x<count($searchListingArray);$x++) {
				
				// Show the information about the item
				if($x % 2) { $propSuburb = 'property-suburb-light'; } else { $propSuburb = 'property-suburb'; }
				echo '<div class="property-listing">';
				
					$propertySuburb = '
							<div class="' . $propSuburb .'">
								<h5>
									<a href="'. $path .'property-details/index.php?propertyid='  . $searchListingArray[$x]->PropertyID . '" target="_self">' . $searchListingArray[$x]->Suburb . '</a>
								</h5>
							</div>';
					echo $propertySuburb;
								
					$propertyPhoto = '
							<div class="property-photo"><img src="' . $searchListingArray[$x]->PhotoLink . '" /></div>';
					echo $propertyPhoto;
					
					$propertyDesc = '
							<div class="description">
								<div class="property-title">
									<h1><a href="'. $path .'property-details/index.php?propertyid='  . $searchListingArray[$x]->PropertyID . '" target="_self">' . $searchListingArray[$x]->Title . '</a></h1>
								</div>';
					
					$propertyDesc .= '
								<div class="property-about">
									<p>' . $searchListingArray[$x]->About . ' 
										<a href="'. $path .'property-details/index.php?propertyid='  . $searchListingArray[$x]->PropertyID . '" target="_self" class="more">more &raquo;</a>
									</p>
								</div>';
								
					$propertyDesc .= '
								<div class="prop-details">
										<p>
											'  . $searchListingArray[$x]->Beds . ' <img src="' . $img . 'content/buy/bed.jpg" />
											'  . $searchListingArray[$x]->Baths . ' <img src="' . $img . 'content/buy/bath.jpg"/>
											'  . $searchListingArray[$x]->Cars . ' <img src="' . $img . 'content/buy/cars.jpg" />
										</p>
								</div>
							</div>';
					echo $propertyDesc;
				
					$agentDetails = '
							<div class="property-left-col">
								<div class="prop-address">
									<p>
										<span class="results-sub-heading">ADDRESS</span><br />
										'  . $searchListingArray[$x]->StreetAddress . '
									</p>
								</div>';
							
					$agentDetails .= '
								<div class="property-agent-name">
									<p>
										<span class="results-sub-heading">AGENT</span><br />
										' . $searchListingArray[$x]->Name . '<br />
										'  . $searchListingArray[$x]->Mobile . '
									</p>
								</div>';
					echo $agentDetails;
								
					$price = '
								<div class="property-price">
									<p>
										<span class="results-sub-heading">PRICE</span><br />
										'  . $searchListingArray[$x]->Price . '
									</p>
								</div>
							</div>';
					echo $price;
					
				echo '</div>';
				echo '<div class="clear">&nbsp;</div>';
			//}
		}
	// print out the page numbers beneath the results
	echo $pageNumbers;
	echo '<div class="clear">&nbsp;</div>';
	}
}

Open in new window

0
 

Author Comment

by:George-TCC
ID: 24239220
just an UPDATE.

I have the pagination working now - but when I click on page 2 it looses it's search variables that were given to it, so now it shows all of the results instead of keeping it filtered by the end users suburb selection.

Anybody had this problem before?
0
 

Author Comment

by:George-TCC
ID: 24427212
Hi Guys,

I have managed to get this working by implementing a pagination class into my script.

Cheers,
George
0

Featured Post

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
Suggested Courses

598 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question