You are here:  » Displaying a numerical value as a word

Support Forum



Displaying a numerical value as a word

Submitted by paddyman on Tue, 2007-09-25 21:05 in

Hi David,

Sorry for the title, please edit if you can think of a better way to describe this question :)

I have a postage cost field in my database which fills the postage cost for each merchant. You recently helped me with displaying a Total cost by adding postage and price.

Some of the merchants don't charge for delivery, so I would like to change these values in html\prices from £0.00 to "free".

The following code displays the postage cost in a column.

<?php
 
print $config_currencyHTML.$product["postage"]; 
?>

and this code displays the total price

<?php
  $totalPrice 
sprintf("%.2f",($product["price"]+$product["postage"]));
  print 
$config_currencyHTML.$totalPrice;
?>

Is there anyway to change 0.00 to the word "Free" and still have the total price working?

Hope this makes sense.

Cheers

Adrian

Submitted by support on Tue, 2007-09-25 21:29

Hi Adrian,

Sure - where you currently have:

<?php
 
print $config_currencyHTML.$product["postage"];
?>

...try something like this:

<?php
if (floatval($product["postage"]))
{
  print 
$config_currencyHTML.$product["postage"];
}
else
{
  print 
"Free";
}
?>

Hope this helps,
Cheers,
David.

Submitted by paddyman on Tue, 2007-09-25 22:41

Thanks yet again :)

Much appreciated.

Adrian

Submitted by clare on Wed, 2007-09-26 15:35

Hi,
Would this be possible for the general price field, for those products that cant specify price and rather than price showing as £0.00, I wondered if prices of £0.00 could display as NA or something similar?

Submitted by support on Wed, 2007-09-26 16:45

Hi Clare,

Sure - various places need to be modified as the price is shown on the search results, the product page and the prices table. Taking them in turn;

html/searchresults.php
Line 28, currently:

<strong><?php print $config_currencyHTML.$product["price"]; ?></strong><br />

change this to:
<strong><?php print (floatval($product["price"])?$config_currencyHTML.$product["price"]:"NA"); ?></strong><br />

html/product.php
Line 23, currently:

<?php print $config_currencyHTML.$mainProduct["price"]; ?>&nbsp;<?php print translate("from"); ?>&nbsp;

change this to:
<?php print (floatval($product["price"])?$config_currencyHTML.$product["price"]:"NA"); ?>&nbsp;<?php print translate("from"); ?>&nbsp;

html/prices.php
Line 13, currently:

<td><strong><?php print $config_currencyHTML.$product["price"]; ?></strong></td>

change this to:
<td><strong><?php print (floatval($product["price"])?$config_currencyHTML.$product["price"]:"NA"); ?></strong></td>

Hope this helps!
Cheers,
David.

Submitted by clare on Thu, 2007-09-27 17:15

Thanks, works great!

Submitted by richard on Wed, 2008-05-21 20:32

Hi David

I have used the following threads to add delivery costs to prices.php

http://www.pricetapestry.com/node/313 - additional fields
http://www.pricetapestry.com/node/1398 - adding fields together
http://www.pricetapestry.com/node/1422 - checking for £0.00 and replacing with text

My problem is that I have found that I have two scenarios with my feeds regarding delivery costs:

1. Feed includes delivery costs that can be either free (reported as £0.00) or there is a charge such as £4.95
2. Feed does not include delivery costs and thus is zero in the database

What I would like to do is to differentiate between feeds that offer free delivery and feeds that omit data. With respect to the first where the merchant states delivery cost or zero charge for free delivery I have used:

<?php
if (floatval($product["delcost"]))
{
print 
$config_currencyHTML.number_format($product["delcost"],2);
}
else
{
  print 
"Free";
}
 
?>

I set the database field "delcost" to VARCHAR(255), as opposed to decimal, and set the default value to "Refer to retailer" for merchants that don't include delivery costs within the feed. However, this did not work - perhaps I did not implement correctly!

Any ideas on how I could tackle feeds with no delivery cost data

Many thanks

Regards

Richard

Submitted by support on Thu, 2008-05-22 06:43

Hi Richard,

The default value should work fine, provided that you aren't using tapestry_decimalise() within the import function as this would result in a value of 0.00 being imported. If you remove that part of the code from the import function, you should then be able to use a couple of conditions to display either case;

<?php
if (is_numeric($product["delcost"]))
{
  if (
$product["delcost"] > 0)
  {
    print 
$config_currencyHTML.number_format($product["delcost"],2);
  }
  else
  {
    print 
"Free";
  }
}
else
{
  print 
$product["delcost"]; // should print default value in database
}
?>

Cheers,
David.

Submitted by richard on Thu, 2008-05-22 11:16

Hi David

Thanks for prompt response -I'm starting to think there must be a team of you working there :)

I have implemented your suggestion and it works well. Thanks

The only problem appears to be getting the database to accept a default value.

My settings are:
Field - delcost
Type - VARCHAR
Length/Values - 255
Attributes - empty
Null - not null
default - Refer to retailer (no quotes)
Extra - empty

Any ideas? If not don't worry.

PS you should add a paypal donate button to your site

Submitted by support on Thu, 2008-05-22 11:22

Hi Richard,

I think it is more likely to do with how the field is being set in includes/admin.php. For the default value to be assigned, the field must not be present in the SQL; otherwise, since empty is a valid value for VARCHAR it will probably end up empty if the postage field during import is blank.

Personally, I always prefer to have field values set by the application, as this keeps your entire application logic in one place. To do this in includes/admin.php, you could add the following code immediately before the SQL is constructed:

if ($record[$admin_importFeed["field_postage"]]=="")
{
  $record[$admin_importFeed["field_postage"]] = "Refer to retailer";
}

Cheers,
David.

Submitted by richard on Thu, 2008-05-22 13:11

Hi David

Many thanks, it works a treat :)

Regards

Richard