<?php 

header('Content-type: text/xml');

//CONNECT TO DATABASE
mysql_connect('localhost', 'ledtvpr1', 'Dennis2012');
mysql_select_db('ledtvpr1_ledtvprices');
//END CONNECT TO DATABASE

$tvid = $_GET['tv'];

if ($tvid != ""){

$information = mysql_query("SELECT * FROM tvs WHERE shortname='$tvid'");

$tvinfo = mysql_fetch_array( $information );

$tvnumber = $tvinfo['id'];
$ustv = $tvinfo['ustv'];

if ($ustv == "1"){
	$currency = "$";
}else{
	$currency = "£";
}

$addquery = "WHERE tvid='$tvnumber'";

}

echo "<?xml version=\"1.0\" encoding=\"utf-8\"?>";

$prices = mysql_query("SELECT * FROM prices $addquery ORDER BY lastchanged ASC");
  
  $counter = 0;
  
  while ($priceinfo = mysql_fetch_array( $prices )){
	  
	  $pricesarray[$counter] = $priceinfo;
	  
	  $price = $priceinfo['price'];
	   $deliverycharge = $priceinfo['delivery'];
	  
	  $vouchertext = "No voucher codes available";
	  
	  if ($priceinfo['vouchers'] != 0){
		
	$voucherid = $priceinfo['vouchers'];
		
	$voucherinfo = mysql_query("SELECT * FROM vouchers WHERE id='$voucherid' AND CURRENT_TIMESTAMP() < expiry  LIMIT 1");

	$voucher = mysql_fetch_array( $voucherinfo );
	
	$information = $voucher['information'];
	
	if ($information == ""){
		
		$vouchertext = "No voucher codes available";
		
	}else{
	
	$percentage = $voucher['percentage'];
	$discount = $voucher['discount'];
	
	$vouchertext = $information;
	
	if ($percentage != 0){
		
		$toremove = round($price / 100 * $percentage, 2);
		
		$price = $price - $toremove;
		
	}else{
		
		$price = $price - $discount;
		
	}
	
	}
		
	}
	
	$price = $price + $deliverycharge;
	
	$price = number_format($price, 2, '.', '');
	
	$pricesarray[$counter]['price'] = $price;
	$pricesarray[$counter]['voucherinformation'] = $vouchertext;
	  
	  $counter ++;
	  
  }
 
 ?>

<rss version="2.0">
<channel>
<title><?php echo $tvinfo['middlename'];?> Price Alert Feed :: LED TV Prices</title>
<description>Price change notification feed for the <?php echo $tvinfo['name'];?>.</description>
<link>http://www.ledtvprices.com/<?php echo $tvinfo['shortname'];?></link>
<copyright>Copyright 2009, LED TV Prices</copyright>
<language>en-us</language>
<lastBuildDate></lastBuildDate>
<managingEditor>tom@ledtvprices.com (Tom)</managingEditor>
<webMaster>tom@ledtvprices.com (Tom)</webMaster>
<ttl>120</ttl>
  
  <?php
  

$secondcounter = 0;

while ($secondcounter != $counter){
	
	$price = $pricesarray[$secondcounter]['price'];
	$shopname = $pricesarray[$secondcounter]['name'];
	$pricetext = $pricesarray[$secondcounter]['pricetext'];
	$id = $pricesarray[$secondcounter]['id'];
	$timestamp = $pricesarray[$secondcounter]['lastchanged'];
	
	$shortname = $tvinfo['shortname'];
	
	$url = "http://www.ledtvprices.com/out?tv=$shortname&amp;id=$id";
	
	$vouchertext = $pricesarray[$secondcounter]['voucherinformation'];
	
	 //GET DATE OUTPUTABLE TEXT
 $difference = 60*60*7;
	   $time = date("D, d M Y H:i:s", strtotime($timestamp) + $difference);
	   //END GET DATE OUTPUTABLE TEXT
	   
	  $vouchertext = str_replace("&pound;", "£", $vouchertext);
	  
	  if ($tvid == ""){

$thistvid = $pricesarray[$secondcounter]['tvid'];

$theinfo = mysql_query("SELECT * FROM tvs WHERE shortname='$thistvid'");

$thetvinfo = mysql_fetch_array( $theinfo );

$tvinfo['middlename'] = $thetvinfo['middlename'];

}
	
	echo "
	<item>

<title>$shopname: $currency$price $pricetext</title>
<link>$url</link>
<guid isPermaLink=\"true\">$url</guid>
<category>$shopname</category>
<description>Get hold of a ".$tvinfo['middlename']." at ".$shopname." for the new price of only $currency".$price." $pricetext.
&lt;p&gt;  &lt;/p&gt; &lt;p&gt; $vouchertext. &lt;/p&gt; &lt;p&gt;  &lt;/p&gt; &lt;p&gt; &lt;a href=\"$url\" target=\"_blank\"&gt;Click here to visit the retailer&lt;/a&gt;, or &lt;a href=\"http://www.ledtvprices.com/$shortname\" target=\"_blank\"&gt;click here to view more prices from LED TV Prices&lt;/a&gt;. &lt;/p&gt;
</description>
<pubDate>$time GMT</pubDate>
<author>LED TV Prices</author>

</item>";
	
	$secondcounter ++;
	
	
	}

 ?>
 </channel>
 </rss>