<? 
header("Content-type: text/xml"); 
	$dbh = mysql_connect( "localhost", "charlest_mac", "apelle" );

	mysql_select_db( "charlest_xmb1", $dbh ) or die;

	foreach ( $_GET as $key => $value ) {

		if ( is_array( $value ) ) {

			$$key = $value;

		} else {

			$$key = trim( $value );

		}

	}

	$pmin = str_replace( ",", "", $pmin );

	$pmax = str_replace( ",", "", $pmax );

	if ( $mlsno ) {

		if ( $mlsno ) {

			if ( strpos( $mlsno, " " ) !== false ) {

				$mlsnoarr = explode( " ", $mlsno );

			} elseif ( strpos( $mlsno, "," ) !== false ) {

				$mlsnoarr = explode( ",", $mlsno );

			} else {

				$mlsnoarr[] = $mlsno;

			}

			foreach ( $mlsnoarr as $value ) {

				$value = trim( $value );

				$mlsnocrit = ( $mlsnocrit ? ( $mlsnocrit . " or MLS_ACCT='$value' " ) : ( "MLS_ACCT='$value'" ) );

			}

			$crit = "where ($mlsnocrit)";

		}

		$theListings = mysql_query( "select * from MLS $crit order by LIST_PRICE desc" );

	} else {

		$where[] = ( $pmin ) ? ( ( $pmax ) ? ( "(LIST_PRICE >= $pmin AND LIST_PRICE <= $pmax )" ) : ( "( LIST_PRICE >= $pmin )" ) ) : ( $pmax ? ( "( LIST_PRICE <= $pmax )" ) : "" );

		$where[] = ( $ymin ) ? ( ( $ymax ) ? ( "(YEAR_BUILT >= $ymin AND YEAR_BUILT <= $ymax )" ) : ( "( YEAR_BUILT >= $ymin )" ) ) : ( $ymax ? ( "( YEAR_BUILT <= $ymax )" ) : "" );

		$where[] = ( $bedmin ) ? ( ( $bedmax ) ? ( "(BEDROOMS >= $bedmin AND BEDROOMS <= $bedmax )" ) : ( "( BEDROOMS >= $bedmin )" ) ) : ( $bedmax ? ( "( BEDROOMS <= $bedmax )" ) : "" );

		$where[] = ( $bathsmin ) ? ( ( $bathsmax ) ? ( "(BATHS_FULL >= $bathsmin AND BATHS_FULL <= $bathsmax )" ) : ( "( BATHS_FULL >= $bathsmin )" ) ) : ( $bathsmax ? ( "( BATHS_FULL <= $bathsmax )" ) : "" );

		$where[] = ( $sqftmin ) ? ( ( $sqftmax ) ? ( "(TOT_HEAT_SQFT >= $sqftmin AND TOT_HEAT_SQFT <= $sqftmax )" ) : ( "( TOT_HEAT_SQFT >= $sqftmin )" ) ) : ( $sqftmax ? ( "( TOT_HEAT_SQFT <= $sqftmax )" ) : "" );

		$where[] = ( $kind ) ? ( "(FTR_KIND = '$kind')" ) : ( "" );
		
		$where[] = ( $lot ) ? ( "(FTR_LOT_DESC LIKE '%$lot%')" ) : ( "" );

		$where[] = ( $addr ) ? ( "(CONCAT(STREET_NUM, ' ',STREET_NAME) LIKE '%$addr%')" ) : ( "" );

		$where[] = ( $city ) ? ( "CITY LIKE '%$city%'" ) : ( "" );

		$where[] = ( $area ) ? ( "(AREA = '$area')" ) : ( "" );

		$where[] = ( $subdiv ) ? ( "SUBDIVISION LIKE '%$subdiv%'" ) : ( "" );
		
		$where[] = ( "LO_IDX_YN LIKE 'Y'" );

		if ( $zips ) {

			if ( strpos( $zips, " " ) !== false ) {

				$zipsarr = explode( " ", $zips );

			} elseif ( strpos( $zips, "," ) !== false ) {

				$zipsarr = explode( ",", $zips );

			} else {

				$zipsarr[] = $zips;

			}

			foreach ( $zipsarr as $value ) {

				$value = trim( $value );

				$zipscrit = ( $zipscrit ? ( $zipscrit . " or ZIP='$value' " ) : ( "ZIP='$value'" ) );

			}

			$where[] = "($zipscrit)";

		}


		foreach ( $where as $value ) {

			$crit = $crit ? ( $value ? ( $crit . " and " . $value ) : $crit ) : ( $value ? ( "where " . $value ) : "" );

		}

		$theListings = mysql_query( "select * from MLS $crit order by LIST_PRICE desc" );

	}

// Calculate which page we're on

	if ( !$listingSkip ) {

		$listingSkip = 15; // Show this many listings

	}

	if ( strpos( $_SERVER["REQUEST_URI"], "skipRows" ) > 0 ) {

		$_SERVER["REQUEST_URI"] = substr( $_SERVER["REQUEST_URI"], 0, strpos( $_SERVER["REQUEST_URI"], "skipRows" ) - 1 );

	}

	if ( strpos( $_SERVER["REQUEST_URI"], "?" ) === false ) {

		$_SERVER["REQUEST_URI"] .= "?";

	} else {

		$_SERVER["REQUEST_URI"] .= "&";

	}

	if ( is_resource( $theListings ) ) {

		$numRows = mysql_num_rows( $theListings );

		if ( $skipRows ) {

			mysql_data_seek( $theListings, $skipRows ); // wind the database to the current point

			$skipRows += $listingSkip;

		} else {

			$skipRows = $listingSkip;

		}

		if ( $skipRows > $listingSkip ) {

			$printSkip = "<a href=\"" . $_SERVER["REQUEST_URI"] . "skipRows=". ($skipRows - ( $listingSkip * 2 ) ) . "\"><img src=back-arrow.gif width=54 height=26 border=0></a>";

			$theCount = "<center>Showing " . ($skipRows-($listingSkip-1));

		} else {

			$theCount = "<center>Showing 1";

		}

		if ( $numRows > $skipRows ) {

			if ( $printSkip ) {

				$printSkip .="&nbsp;&nbsp;&nbsp;&nbsp;<a href=\"" . $_SERVER["REQUEST_URI"] . "pop=n&skipRows=$skipRows\"><img src=next-arrow.gif width=54 height=26 border=0></a>";

			} else {

				$printSkip = "<a href=\"" . $_SERVER["REQUEST_URI"] . "skipRows=$skipRows\"><img src=next-arrow.gif width=54 height=26 border=0></a>";

			}

		}

		if ( $skipRows > $numRows ) {

			$lastListing = $numRows;

		} else {

			$lastListing = $skipRows;

		}

		$theCount .= " through " . $lastListing . " of " . $numRows . " listings";

	}

?>


<rss version="2.0">
<chanel>

<?

	if ( is_resource( $theListings ) ) {

		if ( mysql_num_rows( $theListings ) == 0 ) {

			print "";

		}

		print "";

		$cellCount = 1;

		while ( ( $tl = mysql_fetch_array( $theListings, MYSQL_ASSOC ) ) && $readRows++ < $listingSkip ) {

			if ( $tl["UDF_PHOTO0"] ) {

				$theImages = explode( ",", $tl["UDF_PHOTO0"] );

				$theImage = "http://ctarmls.com/CharlestonReports/Media/" . $theImages[0];

			} else {

// !!! PUT YOUR "MISSING" IMAGE TAG HERE <img src=...>

				$theImage = "http://www.charlestonlistingservice.com/nophoto2.gif";

			}

?>

<?

//	Virtual Tour Link:

if ( count( $theImages ) > 0 ) {

	foreach ( $theImages as $thisImage ) {

		if ( strpos( $thisImage, ".htm" ) === false ) {

		}

		else {

		print "";

	}

	}

}

?>

<?
$description = preg_replace("/[^\w\.@-]/"," ", htmlspecialchars($tl['REMARKS']));
$description = substr("$description",0,100); 
?>
<?
$broker = preg_replace("/[^\w\.@-]/"," ", htmlspecialchars($tl['LO_NAME']));
$broker = substr("$broker",0,28); 
?>

<item>
<title><?=htmlentities(strip_tags($tl['SUBDIVISION'],'ENT_QUOTES'));?> Real Estate</title>
<description><? print $description; ?></description>
<picture><? print $theImage; ?></picture>
<link>http://www.charlestonrealestateguide.com/<?=htmlentities(strip_tags($tl['MLS_ACCT'],'ENT_QUOTES'));?>.html</link>
<address><?=htmlentities(strip_tags($tl['STREET_NUM'],'ENT_QUOTES'));?> <?=htmlentities(strip_tags($tl['STREET_NAME'],'ENT_QUOTES'));?></address>
<price>$<?=htmlentities(number_format( $tl['LIST_PRICE'] ));?></price>
<beds><?=htmlentities(strip_tags($tl['BEDROOMS'],'ENT_QUOTES'));?></beds>
<baths><?=htmlentities(strip_tags($tl['BATHS_FULL'],'ENT_QUOTES'));?>.<?=htmlentities(strip_tags($tl['BATHS_HALF'],'ENT_QUOTES'));?></baths>
<broker><? print $broker; ?></broker>
<contact>877-212-2498</contact>
<sqft><?=htmlentities(number_format( $tl['TOT_HEAT_SQFT'] ));?></sqft>
<mls><?=htmlentities(strip_tags($tl['MLS_ACCT'],'ENT_QUOTES'));?></mls>
</item>

<?

	}

		print "";

} else {

	print "";

}

?>
</chanel>
</rss>





