Formatting php snippet output

khan_lko - July 5, 2009 - 11:47

hi

i have got a code for custom cart block. its giving a perfect output like this " 0 items 0.00 " but i want to format it somewhat like this " Cart: 0 items ($0.00) " and hyperlinked. here is the code..

<?php
$number_of_items
= count(uc_cart_get_contents());
echo
$number_of_items;
?>

<?php
   $sinitem
= "item";
  
$pluitems = "items";

if (
$number_of_items == 1) {
   echo
$sinitem;
}  else {
   echo
$pluitems;
}
?>


<?php
  $subtotal
= 0;
  foreach (
uc_cart_get_contents() as $item) {
   
$subtotal += $item->price * $item->qty;
  }
?>


<?php
$dtotal
= sprintf("%1\$.2f",$subtotal);
echo
$dtotal;
?>

check this link to understand what i want.. http://www.ubercart.org/files/rifftrax_.jpg

This should help, you will

nevets - July 5, 2009 - 14:24

This should help, you will need to determine where to link to

<?php
$output
'Cart: ';

$number_of_items = count(uc_cart_get_contents());
$output  .= $number_of_items;

  
$sinitem = "item";
  
$pluitems = "items";

if (
$number_of_items == 1) {
  
$output  .= $sinitem;
}  else {
  
$output  .= $pluitems;
}

 
$subtotal = 0;
  foreach (
uc_cart_get_contents() as $item) {
   
$subtotal += $item->price * $item->qty;

$dtotal = sprintf("%1\$.2f",$subtotal);
$output  .=  ' ($' . $dtotal . ')';
//You need  to set path  approriately
//Should be a valid Drupal path
$path 'node/1';
print
l($output, $path);
?>

thanx for help nevets, i used

khan_lko - July 5, 2009 - 15:50

thanx for help nevets, i used your code but now it shows a parse error.
:( still looking for the answer.

What line number?

nevets - July 5, 2009 - 16:17

What line number?

On line 26 this is the exact

khan_lko - July 6, 2009 - 02:47

On line 26

this is the exact error i am getting...

Parse error: parse error, unexpected $end in D:\Programs\EasyPHP2\www\drupal6_demo\sites\all\themes\acquia_marina\block-uc_cart.tpl.php on line 26

Last line?

nevets - July 6, 2009 - 03:05

If ?> is the last line, simply remove it.

removing ?> didnt helped...

khan_lko - July 6, 2009 - 03:16

removing ?> didnt helped... but placing a closing curly bracket after this part solved my problem..

  $subtotal = 0;
  foreach (uc_cart_get_contents() as $item) {
    $subtotal += $item->price * $item->qty;
}

theres one more minor problem, i want a space between 0 and items "Cart: 0items ($0.00)". btw thanx for improving the snippet.

Change $output  .=

nevets - July 6, 2009 - 03:41

Change

$output  .= $number_of_items;

to
$output  .= $number_of_items . ' ';

thanx nevets that did the

khan_lko - July 6, 2009 - 09:09

thanx nevets that did the job... its perfect now

Text to image??

khan_lko - July 24, 2009 - 08:16

output =  'Cart: ';

How could i place an image in above snippet and remove the text " cart: "

 
 

Drupal is a registered trademark of Dries Buytaert.