Hi everyone.
So one of the last things of my shopping cart is to fix the currency.
The problem is that it is on the left side like this $10, and I want it to be on the right like this 10$.
Here is the file:

<?php
header ('Content-type: text/html; charset=utf-8');
require_once 'utf.php';
if (!defined('WEB_ROOT')) {
	exit;
}

// get current configuration
$sql = "SELECT sc_name, sc_address, sc_phone, sc_email, sc_shipping_cost, sc_currency, sc_order_email
        FROM tbl_shop_config";
$result = dbQuery($sql);

// extract the shop config fetched from database
// make sure we query return a row
if (dbNumRows($result) > 0) {
	extract(dbFetchAssoc($result));
} else {
	// since the query didn't return any row ( maybe because you don't run plaincart.sql as is )
	// we just set blank values for all variables
	$sc_name = $sc_address = $sc_phone = $sc_email = $sc_shipping_cost = $sc_currency = '';
	$sc_order_email = 'y';
}

// get available currencies
$sql = "SELECT cy_id, cy_code
        FROM tbl_currency
		ORDER BY cy_code";
$result = dbQuery($sql);

$currency = '';
while ($row = dbFetchAssoc($result)) {
	extract($row);
	$currency .= "<option value=\"$cy_id\"";
	if ($cy_id == $sc_currency) {
		$currency .= " selected";
	}
	
	$currency .= ">$cy_code</option>\r\n";
}		
?>
<p>&nbsp;</p>
<form action="processConfig.php?action=modify" method="post" name="frmConfig" id="frmConfig">
 <table width="100%" border="0" cellspacing="1" cellpadding="2" class="entryTable">
  <tr id="entryTableHeader"> 
   <td colspan="2">Shop Configuration</td>
  </tr>
  <tr> 
   <td width="150" class="label">Shop Name</td>
   <td class="content"><input name="txtShopName" type="text" class="box" id="txtShopName" value="<?php echo $sc_name; ?>" size="50" maxlength="50"></td>
  </tr>
  <tr> 
   <td width="150" class="label">Address</td>
   <td class="content"><textarea name="mtxAddress" cols="50" rows="3" id="mtxAddress" class="box"><?php echo $sc_address; ?></textarea></td>
  </tr>
  <tr> 
   <td width="150" class="label">Telephone</td>
   <td class="content"><input name="txtPhone" type="text" class="box" id="txtPhone" value="<?php echo $sc_phone; ?>" size="30" maxlength="30"></td>
  </tr>
  <tr> 
   <td class="label">Email</td>
   <td class="content"><input name="txtEmail" type="text" class="box" id="txtEmail" value="<?php echo $sc_email; ?>" size="30" maxlength="30"></td>
  </tr>
 </table>
 <p>&nbsp;</p>
 <table width="100%" border="0" cellspacing="1" cellpadding="2" class="entryTable">
  <tr id="entryTableHeader"> 
   <td colspan="2">Misc. Configuration</td>
  </tr>
  <tr> 
   <td width="150" class="label">Currency</td>
   <td class="content"><select name="cboCurrency" id="cboCurrency" class="box">
<?php echo $currency; ?>
    </select>   </td>
  </tr>
  <tr> 
   <td width="150" class="label">Shipping Cost</td>
   <td class="content"><input name="txtShippingCost" type="text" class="box" id="txtShippingCost" value="<?php echo $sc_shipping_cost; ?>" size="5"></td>
  </tr>
  <tr>
    <td class="label">Send Email on New Order </td>
    <td class="content"><input name="optSendEmail" type="radio" value="y" id="optEmail" <?php echo $sc_order_email == 'y' ? 'checked' : ''; ?> />
      <label for="optsEmail">Yes </label>
      <input name="optSendEmail" type="radio" value="n" id="optNoEmail" <?php echo $sc_order_email == 'n' ? 'checked' : ''; ?> />
      <label for="optNoEmail">No</label></td>
  </tr>
 </table>
 <p align="center"> 
  <input name="btnUpdate" type="submit" id="btnUpdate" value="Update Config" class="box">
 </p>
</form>

Thanks in advance!

Member Avatar for diafol

I'm confused, where in the page do you want the '$' to show? Which tags or php line is responsible for showing the currency symbol?

The thing is the different currencies are in the database. So basically they will show everywhere where the price is: product list, admin panel with add new product, modify product etc etc...
And actually this is the place where the currency is "defined":

$currency = '';
while ($row = dbFetchAssoc($result)) {
	extract($row);
	$currency .= "<option value=\"$cy_id\"";
	if ($cy_id == $sc_currency) {
		$currency .= " selected";
	}
 
	$currency .= ">$cy_code</option>\r\n";
}
Member Avatar for diafol

Why are you storing symbols in what should be a DECIMAL or BIGINT field? This makes all your amounts varchar or text?

Avoid FLOAT, as you get rounding errors. Also DECIMAL(10,2) is a bit iffy as some currencies are not based on 100 lesser units = 1 larger unit. Yen *I think* is just in integers.

I've seen some suggest using BIGINT. How about?

currency table
currency_id (auto)
currency_name (US Dollar, UK Pound, Egyptian Pound....)
currency_symbol ($,£,€)
symbol_position (0=pre | 1=suff)
decimal_places (0,2 etc)


symbol_position places symbols as prefix or suffix
decimal_places only req'd if you store amount as bigint.

/?EDIT

I apologise, just re-read your post, perhaps I got the wrong end of the stick.

The thing is, if the currency was only a $, I can kinda swallow that.
But with €, it really irritates me when the symbol is on the left. It just doesn't look right.
I've also read somewhere that there is something like :RIGHT, :LEFT that can be added, I just can't find where I read that.

"my shopping cart" == WHAT shopping cart ?

plaincart

Member Avatar for diafol

:RIGHT, :LEFT

Is this for plaincart ? Or php in general. You've given v.little info here for us to go on. I can't see how the currency symbol will be displayed - have you included that info in the php? Sorry, can't see it.

The thing is, I used plaincart as a base, but now, I've worked on it so much, that it doesn't even look like plaincart. So basically, I'm asking about php code, not something that is connected exclusively to plaincart.
What I gave you as a code is the ONLY file where the currency is "declared". On the other files, the currency is nowhere mentioned.

Member Avatar for diafol

Yeah well, the only thing your code does is place the currencies in a dropdown so the user can decide on his default currency. Is that right?

So, we are none the wiser. Have a look at the display pages, THEN post back.

Oh, sorry about that.
I think I found the file where the price connects with the currency.
Line 61

<?php
header ('Content-type: text/html; charset=utf-8');
require_once 'utf.php';
/*
	Contain the common functions 
	required in shop and admin pages
*/
require_once 'config.php';
require_once 'database.php';

/*
	Make sure each key name in $requiredField exist
	in $_POST and the value is not empty
*/
function checkRequiredPost($requiredField) {
	$numRequired = count($requiredField);
	$keys        = array_keys($_POST);
	
	$allFieldExist  = true;
	for ($i = 0; $i < $numRequired && $allFieldExist; $i++) {
		if (!in_array($requiredField[$i], $keys) || $_POST[$requiredField[$i]] == '') {
			$allFieldExist = false;
		}
	}
	
	return $allFieldExist;
}

function getShopConfig()
{
	// get current configuration
	$sql = "SELECT sc_name, sc_address, sc_phone, sc_email, sc_shipping_cost, sc_order_email, cy_symbol 
			FROM tbl_shop_config sc, tbl_currency cy
			WHERE sc_currency = cy_id";
	$result = dbQuery($sql);
	$row    = dbFetchAssoc($result);

    if ($row) {
        extract($row);
	
        $shopConfig = array('name'           => $sc_name,
                            'address'        => $sc_address,
                            'phone'          => $sc_phone,
                            'email'          => $sc_email,
				    'sendOrderEmail' => $sc_order_email,
                            'shippingCost'   => $sc_shipping_cost,
                            'currency'       => $cy_symbol);
    } else {
        $shopConfig = array('name'           => '',
                            'address'        => '',
                            'phone'          => '',
                            'email'          => '',
				    'sendOrderEmail' => '',
                            'shippingCost'   => '',
                            'currency'       => '');    
    }

	return $shopConfig;						
}

function displayAmount($amount)
{
	global $shopConfig;
	return $shopConfig['currency'] . number_format($amount);
}

/*
	Join up the key value pairs in $_GET
	into a single query string
*/
function queryString()
{
	$qString = array();
	
	foreach($_GET as $key => $value) {
		if (trim($value) != '') {
			$qString[] = $key. '=' . trim($value);
		} else {
			$qString[] = $key;
		}
	}
	
	$qString = implode('&', $qString);
	
	return $qString;
}

/*
	Put an error message on session 
*/
function setError($errorMessage)
{
	if (!isset($_SESSION['plaincart_error'])) {
		$_SESSION['plaincart_error'] = array();
	}
	
	$_SESSION['plaincart_error'][] = $errorMessage;

}

/*
	print the error message
*/
function displayError()
{
	if (isset($_SESSION['plaincart_error']) && count($_SESSION['plaincart_error'])) {
		$numError = count($_SESSION['plaincart_error']);
		
		echo '<table id="errorMessage" width="550" align="center" cellpadding="20" cellspacing="0"><tr><td>';
		for ($i = 0; $i < $numError; $i++) {
			echo '• ' . $_SESSION['plaincart_error'][$i] . "<br>\r\n";
		}
		echo '</td></tr></table>';
		
		// remove all error messages from session
		$_SESSION['plaincart_error'] = array();
	}
}

/**************************
	Paging Functions
***************************/

function getPagingQuery($sql, $itemPerPage = 10)
{
	if (isset($_GET['page']) && (int)$_GET['page'] > 0) {
		$page = (int)$_GET['page'];
	} else {
		$page = 1;
	}
	
	// start fetching from this row number
	$offset = ($page - 1) * $itemPerPage;
	
	return $sql . " LIMIT $offset, $itemPerPage";
}

/*
	Get the links to navigate between one result page to another.
	Supply a value for $strGet if the page url already contain some
	GET values for example if the original page url is like this :
	
	http://www.phpwebcommerce.com/plaincart/index.php?c=12
	
	use "c=12" as the value for $strGet. But if the url is like this :
	
	http://www.phpwebcommerce.com/plaincart/index.php
	
	then there's no need to set a value for $strGet
	
	
*/
function getPagingLink($sql, $itemPerPage = 10, $strGet = '')
{
	$result        = dbQuery($sql);
	$pagingLink    = '';
	$totalResults  = dbNumRows($result);
	$totalPages    = ceil($totalResults / $itemPerPage);
	
	// how many link pages to show
	$numLinks      = 10;

		
	// create the paging links only if we have more than one page of results
	if ($totalPages > 1) {
	
		$self = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'] ;
		

		if (isset($_GET['page']) && (int)$_GET['page'] > 0) {
			$pageNumber = (int)$_GET['page'];
		} else {
			$pageNumber = 1;
		}
		
		// print 'previous' link only if we're not
		// on page one
		if ($pageNumber > 1) {
			$page = $pageNumber - 1;
			if ($page > 1) {
				$prev = " <a href=\"$self?page=$page&$strGet/\">[Prev]</a> ";
			} else {
				$prev = " <a href=\"$self?$strGet\">[Prev]</a> ";
			}	
				
			$first = " <a href=\"$self?$strGet\">[First]</a> ";
		} else {
			$prev  = ''; // we're on page one, don't show 'previous' link
			$first = ''; // nor 'first page' link
		}
	
		// print 'next' link only if we're not
		// on the last page
		if ($pageNumber < $totalPages) {
			$page = $pageNumber + 1;
			$next = " <a href=\"$self?page=$page&$strGet\">[Next]</a> ";
			$last = " <a href=\"$self?page=$totalPages&$strGet\">[Last]</a> ";
		} else {
			$next = ''; // we're on the last page, don't show 'next' link
			$last = ''; // nor 'last page' link
		}

		$start = $pageNumber - ($pageNumber % $numLinks) + 1;
		$end   = $start + $numLinks - 1;		
		
		$end   = min($totalPages, $end);
		
		$pagingLink = array();
		for($page = $start; $page <= $end; $page++)	{
			if ($page == $pageNumber) {
				$pagingLink[] = " $page ";   // no need to create a link to current page
			} else {
				if ($page == 1) {
					$pagingLink[] = " <a href=\"$self?$strGet\">$page</a> ";
				} else {	
					$pagingLink[] = " <a href=\"$self?page=$page&$strGet\">$page</a> ";
				}	
			}
	
		}
		
		$pagingLink = implode(' | ', $pagingLink);
		
		// return the page navigation link
		$pagingLink = $first . $prev . $pagingLink . $next . $last;
	}
	
	return $pagingLink;
}
?>

it may be something as simple as

if($shopconfig['currency']=="€"||$shopconfig['currency']=="other currency with a preference for after") {
return  number_format($amount) . $shopConfig['currency']; } 
else { 
return $shopConfig['currency'] . number_format($amount);}

It didn't worked.
The only thing that this did was adding a ; between the price and the currency like this $;10

Member Avatar for diafol
return $shopConfig['currency'] . number_format($amount);

That looks like it. OK should be simple now then. so based on AB's:

return ($shopConfig['currency'] == '€') ?  number_format($amount) . $shopConfig['currency'] : $shopConfig['currency'] . number_format($amount);

Depends how the euro symbols are stored in your DB. If htmlencoded use &euro; else use the utf-8 symbol.

Well I solved it.
The only thing that I did was, I switched the places of the variables on line 64.
Instead of

return $shopConfig['currency'] . number_format($amount);

I made it

return number_format($amount) . $shopConfig['currency'];

Guess sometimes the stupid ideas work:-)
Thanks for the support guys!:-)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.