How to insert php code onto a webpage...

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:

HS1

Price: R2500,00

HS2

Price: R1500,00

HS3

Price: R1750,00

<?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?

well to begin with all php is server side.
all you would have to do is add your new case switch if you want to add subcategories and change the original to this below
[php]
if ($_GET[‘category’]!=’’ && $_GET[‘subcategory’]=’’){
$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 ‘

Please select a category

’;

} [/php]
then add:
[php]
if ($_GET[‘category’]!=’’ && $_GET[‘subcategory’]!=’’){
$linkchoice=$_GET[‘subcategory’];
}else{
$linkchoice=’’;
}

switch($linkchoice){

case ‘subcatHockey’ :
subcat(subcatHockey);
break;

case ‘subcatRugby’ :
subcat1(subcatRugby);
break;

case ‘subcatSwimming’ :
subcat2(subcatSwimming);
break;

case ‘subcatWaterpolo’ :
subcat3(subcatWaterpolo);
break;

case ‘subcatCricket’ :
subcat4(subcatCricket);
break;

case ‘subcatviewcart’ :
subcat5(subcatviewcart);
break;

default :
echo ‘

Please select a subcategory

’;

}
[/php]
then add the appropriate links:
[php]

subcatHockey

[/php]

If I understood you correctly something like that should work.

Sponsor our Newsletter | Privacy Policy | Terms of Service