<?php
//Uses JQuery Carousel-PikaChoose

//Connect to database
$db=mysql_connect($dbhost, $dbuser, $dbpass) or die
	('Cant connect to the database because: ') . mysql_error();
	
//Select the database
mysql_select_db($dbname);
$i=0;
//If a category is chosen by user, then display carousel with that categorie's specifics, else display all categories
if($sentprodcat)
{

?>
<script language="javascript">
alert('in first one, but not working... sentprodcat = <?= $sentprodcat ?>');
</script>
<?php

	$resultPRODNAME=mysql_query("SELECT * FROM products WHERE product_category=\"$sentprodcat\" GROUP BY product_sales_class_name LIMIT 9");
	
	$mynumprodname=mysql_num_rows($resultPRODNAME);

	$myprodcatimage=mysql_escape_string($rowPRODNAME['product_category_image_path']);
	$myprodcatsumm=mysql_escape_string($rowPRODNAME['product_category_summary']);

	if($mynumprodname>0)
	{
		echo "<div><ul>";
		
		while($rowPRODNAME=mysql_fetch_array($resultPRODNAME))
		{
			for ($i=0; $i<=3;$i++)
			{
			$myprodname=mysql_escape_string($rowPRODNAME['product_sales_class_name']);	
			$myprodnameimage=$rowPRODNAME['product_sales_class_name_image_path'];
			$myprodnamesumm=mysql_escape_string($rowPRODNAME['product_sales_class_summary']);
			
			$myprodcatimage = file_exists($myprodcatimage) ? $myprodcatimage : "images/mb_placeholder.jpg";
 			
			echo "<li>
					<a title=\"$myprodname\" href=\"products_name_display.php?myprodcat=$sentprodcat
						&amp;myprodcatimage=$myprodcatimage
						&amp;myprodcatsumm=$myprodcatsumm
						&amp;myprodname=$myprodname
						&amp;myprodnamesumm=$myprodnamesumm
						&amp;myprodnameimage=$myprodnameimage\">
						<img alt=\"$myprodname\" src=\"$myprodnameimage\" height=\"125\" width=\"125\" />
					</a>
					<span class=\"centersmallblackcaption\">$myprodname ... $i</span>
					<br /><br />
					<span class=\"centersmallblack\">$myprodsumm</span>
				</li>";
			}
		}
		echo "</ul></div>";
	}
	else
	{
		echo "No list items exist";
	}	
}
else
{
	$resultPRODCAT=mysql_query("SELECT * FROM products GROUP BY product_category");

	$mynumprodcat=mysql_num_rows($resultPRODCAT);

	if($mynumprodcat>0)
	{
		echo "<div><table><tr>";
		
		while($rowPRODCAT=mysql_fetch_array($resultPRODCAT))
		{

			$myprodcat=mysql_escape_string($rowPRODCAT['product_category']);	
			$myprodcatimage=$rowPRODCAT['product_category_image_path'];
			$myprodcatsumm=mysql_escape_string($rowPRODCAT['product_category_summary']);

			$myprodcatimage = file_exists($myprodcatimage) ? $myprodcatimage : "images/mb_placeholder.jpg"; 
			//$myprodcatsumm = strstr($myprodcatsumm, "&#46;", true) ? strstr($myprodcatsumm, "&#46;", true): "Meadow Burke - Innovating Concrete Construction.";
?>
<script language="javascript">
alert('$myprodcatsumm= <?= $myprodcatsumm ?>');
</script>
<?php			
			$test=strpos(($myprodcatsumm, "&#46;");
?>
<script language="javascript">
alert('test= <?= $test ?>');
</script>
<?php
			if(strpos($myprodcatsumm, "&#46;")>0)
			{
				$myprodcatsumm = strstr($myprodcatsumm, "&#46;", true);
			}
			else
			{
				$myprodcatsumm = "Meadow Burke - Innovating Concrete Construction.";			
			}
			
			echo "<td>
					<a title=\"$myprodcat\" href=\"products_cat_display.php?myprodcat=$sentprodcat
						&amp;myprodcatimage=$myprodcatimage
						&amp;myprodcatsumm=$myprodcatsumm\">
						<img alt=\"$myprodcat\" src=\"$myprodcatimage\" height=\"125\" width=\"125\" />
						<br />
						<span class=\"centersmallblackcaption\">$myprodcat</span>
						<br />
						<span class=\"centersmallblack\">$myprodcatsumm</span>						
					</a>
				</td>";
			$i++;				
			if ($i % 3 == 0) 
			{
				//we have reached the mid-point, let's create a 2nd row
				echo "</tr><tr>";
			}
		}
		echo "</tr></table></div>";
	}
	else
	{
		echo "No list items exist";
	}
}
?>			  
			  