Jump to content

when click product add to array in php


jasmeet

Recommended Posts

<?php
session_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);
   }
?>
<?php
foreach($_SESSION['cart'] as $value)
{
    echo $value; ?><br /><?php
}
?>

 

 

 

thanks..

Link to comment
https://forums.phpfreaks.com/topic/280989-when-click-product-add-to-array-in-php/
Share on other sites

 

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.