jasmeet Posted August 9, 2013 Share Posted August 9, 2013 <?phpsession_start();$_SESSION['cart']=array();include("config.php");?><?php$query=mysql_query("select * from answers");while($result=mysql_fetch_array($query)){ ?><a href="m.php?cart=<?php echo $result['answer']; ?>&hello=yes"><?php echo $name= $result['answer'];?></a><br /><?php } if (isset($_REQUEST['hello'])) { $prod_id=$_REQUEST['cart']; array_push($_SESSION['cart'],$prod_id); }?><?phpforeach($_SESSION['cart'] as $value){ echo $value; ?><br /><?php}?> thanks.. Quote Link to comment https://forums.phpfreaks.com/topic/280989-when-click-product-add-to-array-in-php/ Share on other sites More sharing options...
Solution PaulRyan Posted August 9, 2013 Solution Share Posted August 9, 2013 <?PHP //### Start the session session_start(); //### Include config include('config.php'); //### Start output $cartOutput = ''; $answerOutput = ''; //### If session cart is not set, remove it if(!isset($_SESSION['cart'])) { $_SESSION['cart'] = array(); } //### Add item to cart if(isset($_GET['hello'])) { $itemID = isset($_GET['cart']) ? $_GET['cart'] : FALSE ; if(!empty($itemID)) { array_push($_SESSION['cart'], $itemID); } } //### Display cart if items exist if(isset($_SESSION['cart'])) { foreach($_SESSION['cart'] as $value) { $cartOutput .= $value.' <br>'; } } //### Select items from database $selectAnswersQuery = "SELECT `answer` FROM `answers`"; $selectAnswers = mysql_query($selectAnswersQuery) OR DIE (mysql_error()); //### Check to see if rows are returned if(mysql_num_rows($selectAnswers)) { while($row = mysql_fetch_assoc($selectAnswers)) { $answerOutput .= '<a href="m.php?cart='. $row['answer'] .'&hello=yes">'. $row['answer'] .'</a><br>'; } } else { $answerOutput .= 'No rows returned. <br>'; } //### Display output echo $answerOutput; echo $cartOutput; ?> Quote Link to comment https://forums.phpfreaks.com/topic/280989-when-click-product-add-to-array-in-php/#findComment-1444139 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.