It is more complicated than it appears so I have attached some code that will do it (see below) as a PHP class. This is only a very simple basket but it can be easily extended.
It is used like so
$b = new myBasket("basketName"); // You can run several baskets, just use unique names
$b->addItem( $productCode ); // Adds 1 item
$b->addItem( $productCode2, 10 ); // Adds a quantity of 10 for productCode2
To display the basket
$items = $b->fetchAllItems();
$qty = $b->fetchAllQtys();
foreach( $items as $index=> $aProductCode ) {
echo "Product code $aProductCode has {$qty[$index]} items<br/>";
}