Hey guys,
I am new to php and am busy designing a site.
My current problem is that on my product page i have a variable ($category) which then goes to a switch statement to tell the server which code to get, in this there is another switch statement.
My only problem after that is that i need to call another function (which requires a switch statement to run), bu the code will not run.
here is the code;
The product page:
Hockey
Rugby
Swimming
Waterpolo
Cricket
</div>
<?php
if (isset($_GET['category'])){
$linkchoice=$_GET['category'];
}else{
$linkchoice='';
}
switch($linkchoice){
case 'Hockey' :
viewProducts(Hockey);
break;
case 'Rugby' :
viewProducts(Rugby);
break;
case 'Swimming' :
viewProducts(Swimming);
break;
case 'Waterpolo' :
viewProducts(Waterpolo);
break;
case 'Cricket' :
viewProducts(Cricket);
break;
case 'viewcart' :
viewCart(Cricket);
break;
default :
echo '<h1 align="center">Please select a category</h1>';
}
?>
viewProduct function:
function viewProducts($category){
$result = mysql_query("SELECT * FROM " . $category );
$output2[] = ‘<?php’;
$output2[] = ’ if (isset($_GET[‘name’])){’;
$output2[] = ‘$linkchoice=$_GET[‘name’];’;
$output2[] = ‘}else{’;
$output2[] = ‘$linkchoice=’’;’;
$output2[] = ‘}’;
$output2[] = ‘switch($linkchoice){’;
while($row = mysql_fetch_array($result)){
$output1[] = '<div id="product">';
$output1[] = '<img src="' . $row['img'] . '" class="Dimg"/>';
$output1[] = '<h3>' . $row['name'] . '</h3>';
$output1[] = '<h3>Price: R' . $row['price'] . ',00</h3>';
$output1[] = '<a href="?name='.$row['name'].'&category='.$category.'">';
$output1[] = '<img src="images/atc.jpg"/>';
$output1[] = '</a>';
$output1[] = '</div>';
$output2[] = 'case \'' . $row['name'] . '\': ';
$output2[] = 'addToCart(' . $category . ',' . $row['id'] . '); ';
$output2[] = 'break; ';
}
$output2[] = 'default:';
$output2[] = ' ';
$output2[] = '}';
$output2[] = '?>';
$preout1 = join('',$output1);
$preout2 = join('',$output2);
$finalOut = $preout1 . $preout2;
echo $finalOut;
}
This is the source code of the page once i click add to cart:
<?php if (isset($_GET['name'])){ $linkchoice=$_GET['name']; }else{ $linkchoice=''; } switch($linkchoice){ case 'HS1': addToCart(Hockey,1); break;case ‘HS2’:
addToCart(Hockey,2);
break;
case ‘HS3’:
addToCart(Hockey,3);
break;
default:
}
?>
Any help is greatly appreciated, but from what i can gather is that for the switch to run the php code needs to be server side but how would i do this?