1

Good Day, I am trying to concatenate the values that are being fetched through "$_GET" The values like medicine name are being fetched from database dynamically.

while ($row = $result->fetchObject())
{  $display .= "<tr><td>$row->MedName</td>". "<td>$row->MedPrice</td>". "<td>$row->MedStock</td>". "<td>$row->MedDetail</td>". "<td><a href='storepage.php?id=$row->MedName' style='text-decoration: none; color: purple'>Add to cart</a></td> </tr>";}
    $display .= "</table>";
    echo $display;

    $comma_separated = join(",",  $array($_GET['id']));
    array_push($_SESSION['basket'], $comma_separated );
    echo $dis = "<a href='test.php'>go</a>";

The values are being saved in $_SESSION['basket']

<?php

session_start();
if (isset($_SESSION['mail'])) 
{
    $_SESSION['basket'] = array();
}
else
header("location: clientsigninpage.php?msg= Log-in First");

?>

My problem here is that as soon as I press Add to cart multiple times and I go to the test-page it prints only the last value in the next page, it does not concatenate.

<?php

session_start();
if (isset($_SESSION['mail'])) 
{
    $list = $_SESSION['basket'];
}
else
header("location: clientsigninpage.php?msg= Log-in First");

foreach ($list  as $value) 
{
  echo $value;
}

I have seen some solutions but i couldn't get my solution as I want to dynamically get the data and store it. The links are: http://php.net/manual/en/function.array-push.php; https://www.w3schools.com/php/func_array_push.asp; How to add elements to an empty array in PHP?; How to push both value and key into array

M A S S
  • 23
  • 5
  • Maybe [How to add elements to an empty array in PHP?](https://stackoverflow.com/questions/676677/how-to-add-elements-to-an-empty-array-in-php) – ficuscr May 24 '18 at 16:55
  • @ficuscr Yes I have used this method look : `$comma_separated = join(",", array($_GET['id'])); array_push($_SESSION['basket'], $comma_separated); array_push($_SESSION['basket'], 13); array_push($_SESSION['basket'], 14); echo $dis = "go";` – M A S S May 24 '18 at 22:31
  • I cannot use this method as I would not know how many times the user will use add to cart and I would have to write this statement as many times `array_push($_SESSION['basket'], $somevalue);` – M A S S May 24 '18 at 22:41

0 Answers0