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
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
thanx for help nevets, i used your code but now it shows a parse error.
:( still looking for the answer.
What line number?
What line number?
On line 26 this is the exact
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?
If
?>is the last line, simply remove it.removing ?> didnt helped...
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 .=
Change
$output .= $number_of_items;to
$output .= $number_of_items . ' ';thanx nevets that did the
thanx nevets that did the job... its perfect now
Text to image??
output = 'Cart: ';How could i place an image in above snippet and remove the text " cart: "