Jump to content

when click product add to array in php


jasmeet
Go to solution Solved by PaulRyan,

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
Share on other sites

  • Solution

 

<?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;
 
?>
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.