Jump to content

Recommended Posts

I have been integrate the shopping cart written by someone with following code.


function showCart() {
$link = mysql_connect('localhost', 'root', '');
if (!$link) {
    die('Not connected : ' . mysql_error());
}

$db_selected = mysql_select_db('tcmdb', $link);
if (!$db_selected) {
    die ('Can\'t use the Database: ' . mysql_error());
}
   
$cart = $_SESSION['cart'];
if ($cart) {
$items = explode(',',$cart);
$contents = array();
foreach ($items as $item) { //loop over items array and each loop, the value of the current element is assigned to item (use when unknown n. of elements. items is array name and item is array element. item is select by user.
$contents[$item] = (isset($contents[$item])) ? $contents[$item] + 1 : 1;
}
$output[] = '<form action="cart.php?action=update" method="post" id="cart">';
$output[] = '<table>';
foreach ($contents as $id=>$qty) { //key and value
$sql = 'SELECT * FROM Item WHERE Item_id = '.$id;
$result = mysql_query($sql);
$row = mysql_fetch_array($result, MYSQL_ASSOC);
extract($row); //Import variables into the current symbol table from an array
$output[] = '<tr>';
$output[] = '<td><a href="cart.php?action=delete&id='.$id.'" class="r">Remove</a></td>';
$output[] = '<td>'.$Item_name.'</td>';
$output[] = '<td>RM'.$Unit_price.'</td>';
$output[] = '<td><input type="text" name="qty'.$id.'" value="'.$qty.'" size="6" maxlength="3" /></td>';
$output[] = '<td>RM'.($Unit_price * $qty).'</td>';
$total += $Unit_price * $qty;
$output[] = '</tr>';
}
$output[] = '</table>';
$output[] = '<p>Grand total: <strong>RM'.$total.'</strong></p>';
$output[] = '<p align="center"><button type="submit">Update cart</button></p>';
$output[] = '[color=red]<input name="total" type="hidden" value="$total"></[/color]form><p><p><br>';
} else {
$output[] = '<p>You shopping cart is empty.</p>';
}
return join('',$output);
}
echo "<form></form>";

how i can use this variable information to pass to next page to check out? i use input type hidden but does not work...is it input type hidden could no longer used to pass the info to next page in a FUNCTION?
Link to comment
https://forums.phpfreaks.com/topic/33735-nit-ur-help/
Share on other sites

it will be passed - just use $_POST['total'] in the script that processes the form.

Your problem here is that you are trying to place variables in a single quoted string! change that line to:

$output[] = '<input name="total" type="hidden" value="' . $total . '"></form><p><p>

Link to comment
https://forums.phpfreaks.com/topic/33735-nit-ur-help/#findComment-158172
Share on other sites

[color=blue]function showCart() {
     
  $cart = $_SESSION['cart'];
  if ($cart) {
      $items = explode(',',$cart);
      $contents = array();
      foreach ($items as $item) {
        $contents[$item] = (isset($contents[$item])) ? $contents[$item] + 1 : 1;
      }
      $output[] = '<form action="cart.php?action=update" method="post" id="cart">';
      $output[] = '<table>';
      foreach ($contents as $id=>$qty) { //key and value
        $sql = 'SELECT * FROM Item WHERE Item_id = '.$id;
        $result = mysql_query($sql);
        $row = mysql_fetch_array($result, MYSQL_ASSOC);
        extract($row); //Import variables into the current symbol table from an array
        $output[] = '<tr>';
        $output[] = '<td><a href="cart.php?action=delete&id='.$id.'" class="r">Remove[/url]</td>';
        $output[] = '<td>'.$Item_name.'</td>';
        $output[] = '<td>RM'.$Unit_price.'</td>';
        $output[] = '<td><input type="text" name="qty'.$id.'" value="'.$qty.'" size="6" maxlength="3" /></td>';
        $output[] = '<td>RM'.($Unit_price * $qty).'</td>';
        $total += $Unit_price * $qty;
        $output[] = '</tr>';
      }
      $output[] = '</table>';
      $output[] = '<p>Grand total: <strong>RM'.$total.'</strong></p>';
      $output[] = '<p align="center"><button type="submit">Update cart</button></p>';
      $output[] = '<input name="total" type="hidden" value="$total"></form><p><p>
';
  } else {
      $output[] = '<p>You shopping cart is empty.</p>';
  }
  return join('',$output);
}
echo "<form></form>";[/color]


the $total pass well to another pages. How to pass the content of the cart to next page? the item selected by users....Information that I nit to pass:

$total -done
$qty of EACH item
$price of EACH item

any1 can help? thanks!
Link to comment
https://forums.phpfreaks.com/topic/33735-nit-ur-help/#findComment-158219
Share on other sites

it is an object...it put item selected vy user in shopping cart in session before insert into DB...this "virtual" cart is used so tat no nit query DB many times.

I got 1 problem to pass the value of the following:

Each quantity of each item.

Each price of each item.

is it have to use foreach??
Link to comment
https://forums.phpfreaks.com/topic/33735-nit-ur-help/#findComment-159711
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.