Php 致命错误:调用未定义的函数money_format()
每次我尝试运行此代码时,都会有一条消息说:Php 致命错误:调用未定义的函数money_format(),php,cart,shopping,Php,Cart,Shopping,每次我尝试运行此代码时,都会有一条消息说: Fatal error: Call to undefined function money_format() 出现该问题的线路有: $pricetotal = money_format("%10.2n", $pricetotal); 及 你能解释一下发生这种情况的原因吗 $cartOutput = ""; $cartTotal = ""; $pp_checkout_btn = ''; $product_id_array = ''; if (!iss
Fatal error: Call to undefined function money_format()
出现该问题的线路有:
$pricetotal = money_format("%10.2n", $pricetotal);
及
你能解释一下发生这种情况的原因吗
$cartOutput = "";
$cartTotal = "";
$pp_checkout_btn = '';
$product_id_array = '';
if (!isset($_SESSION["cart_array"]) || count($_SESSION["cart_array"]) < 1) {
$cartOutput = "<h2 align='center'>Your shopping cart is empty</h2>";
} else {
// Start PayPal Checkout Button
$pp_checkout_btn .= '<form action="https://www.paypal.com/cgi-bin/webscr" method="post">
<input type="hidden" name="cmd" value="_cart">
<input type="hidden" name="upload" value="1">
<input type="hidden" name="business" value="you@youremail.com">';
// Start the For Each loop
$i = 0;
foreach ($_SESSION["cart_array"] as $each_item) {
$item_id = $each_item['item_id'];
$sql = mysql_query("SELECT * FROM products WHERE id='$item_id' LIMIT 1");
while ($row = mysql_fetch_array($sql)) {
$product_name = $row["product_name"];
$price = $row["price"];
$details = $row["details"];
}
$pricetotal = $price * $each_item['quantity'];
$cartTotal = $pricetotal + $cartTotal;
setlocale(LC_MONETARY, "en_US");
$pricetotal = money_format("%10.2n", $pricetotal);
// Dynamic Checkout Btn Assembly
$x = $i + 1;
$pp_checkout_btn .= '<input type="hidden" name="item_name_' . $x . '" value="' . $product_name . '">
<input type="hidden" name="amount_' . $x . '" value="' . $price . '">
<input type="hidden" name="quantity_' . $x . '" value="' . $each_item['quantity'] . '"> ';
// Create the product array variable
$product_id_array .= "$item_id-".$each_item['quantity'].",";
// Dynamic table row assembly
$cartOutput .= "<tr>";
$cartOutput .= '<td><a href="product.php?id=' . $item_id . '">' . $product_name . '</a><br /><img src="inventory_images/' . $item_id . '.jpg" alt="' . $product_name. '" width="40" height="52" border="1" /></td>';
$cartOutput .= '<td>' . $details . '</td>';
$cartOutput .= '<td>$' . $price . '</td>';
$cartOutput .= '<td><form action="cart.php" method="post">
<input name="quantity" type="text" value="' . $each_item['quantity'] . '" size="1" maxlength="2" />
<input name="adjustBtn' . $item_id . '" type="submit" value="change" />
<input name="item_to_adjust" type="hidden" value="' . $item_id . '" />
</form></td>';
//$cartOutput .= '<td>' . $each_item['quantity'] . '</td>';
$cartOutput .= '<td>' . $pricetotal . '</td>';
$cartOutput .= '<td><form action="cart.php" method="post"><input name="deleteBtn' . $item_id . '" type="submit" value="X" /><input name="index_to_remove" type="hidden" value="' . $i . '" /></form></td>';
$cartOutput .= '</tr>';
$i++;
}
setlocale(LC_MONETARY, "en_US");
$cartTotal = money_format("%10.2n", $cartTotal);
$cartTotal = "<div style='font-size:18px; margin-top:12px;' align='right'>Cart Total : ".$cartTotal." USD</div>";
// Finish the Paypal Checkout Btn
$pp_checkout_btn .= '<input type="hidden" name="custom" value="' . $product_id_array . '">
<input type="hidden" name="notify_url" value="https://www.yoursite.com/storescripts/my_ipn.php">
<input type="hidden" name="return" value="https://www.yoursite.com/checkout_complete.php">
<input type="hidden" name="rm" value="2">
<input type="hidden" name="cbt" value="Return to The Store">
<input type="hidden" name="cancel_return" value="https://www.yoursite.com/paypal_cancel.php">
<input type="hidden" name="lc" value="US">
<input type="hidden" name="currency_code" value="USD">
<input type="image" src="http://www.paypal.com/en_US/i/btn/x-click-but01.gif" name="submit" alt="Make payments with PayPal - its fast, free and secure!">
</form>';
}
?>
$cartOutput=”“;
$cartotal=“”;
$pp_checkout_btn='';
$product\U id\U array='';
如果(!isset($_会话[“购物车阵列”])| |计数($_会话[“购物车阵列”])<1){
$cartOutput=“您的购物车是空的”;
}否则{
//启动贝宝结账按钮
$pp\U结帐\U btn.='
';
//为每个循环启动
$i=0;
foreach($\会话[“购物车数组”]作为$each项目){
$item_id=$each_item['item_id'];
$sql=mysql\u query(“从产品中选择*,其中id='$item\u id'限制1”);
while($row=mysql\u fetch\u数组($sql)){
$product_name=$row[“product_name”];
$price=$row[“price”];
$details=$row[“details”];
}
$pricetotal=$price*$每件物品['quantity'];
$cartTotal=$pricetotal+$cartTotal;
setlocale(LC_MONETARY,“en_US”);
$pricetotal=货币单位格式(“%10.2n”,$pricetotal);
//动态检测Btn组件
$x=$i+1;
$pp\U结帐\U btn.='
';
//创建产品数组变量
$product_id_array.=“$item_id-”$each_item['quantity']。”,“;
//动态表行组装
$cartOutput.=“”;
$cartOutput.='
';
$cartOutput.=''.$details';
$cartOutput.='$'.$price';
$cartOutput.='
';
//$cartOutput.=''.$each_item['quantity'.'”;
$cartOutput.=''.$pricetotal';
$cartOutput.='';
$cartOutput.='';
$i++;
}
setlocale(LC_MONETARY,“en_US”);
$cartTotal=货币格式(“%10.2n”,$cartTotal);
$cartTotal=“购物车总计:”.$cartTotal.“美元”;
//完成贝宝结帐Btn
$pp\U结帐\U btn.='
';
}
?>
如果您使用的是基于windows的系统,则此功能将不可用
仅当系统具有strfmon功能时,才定义money_format()函数。例如,Windows没有,因此在Windows中未定义money_format()。
(Mike W在评论中也指出了这一点)
有人指出,您可能没有此功能,因为它并不存在于所有操作系统上(请参阅Mike W的评论或学员的回答) 由于该函数在您的系统上不存在,因此您可以基于编写自己的函数。您似乎希望将数字格式化为美元,因此默认值(十进制=
和千位数=,
应该适合您)
与
更新后的代码将负号放在货币符号前面,即使是负数(根据@kunal的评论)。函数货币格式($formato,$valor){
如果(setlocale(LC_,0)='C'){
返回编号_格式($valor,2);
}
$locale=localeconv();
$regex='/^.//Inicio da Expressao
“%”//Caractere%
“(?:”。//Inicio das Flags opcionais
“\=([\w\040])。//标志=f
'|'.
“([\^])”。//标志^
'|'.
“(\+\\()”//Flag+ou(
'|'.
“(!)”。//旗帜!
'|'.
“(-”//Flag-
“)*”。//职能指令手册
“(?:([\d]+)?)”//W拉古拉·德坎波斯
“(?:#([\d]+)?”//#n Precisao esquerda
“(?:\([\d]+)?”/.p Precisao direita
“([in%])。//Caractere de conversao
“$/”;//Fim da Expressao
如果(!preg_match($regex,$formato,$matches)){
触发错误('Formato invalido:'。$Formato,E_用户警告);
返回$valor;
}
$opcoes=数组(
'先兆'=>($matches[1]!==''?$matches[1]:'',
'nao_agrupar'=>($matches[2]=='^'),
'usar_sinal'=>($matches[3]=='+'),
'usar_parenteses'=>($matches[3]=='('),
'ignorar_simbolo'=>($matches[4]=='!'),
'alinhamento_esq'=>($matches[5]=='-'),
'largura_campo'=>($matches[6]!=='')(int)$matches[6]:0,
'precisao_esq'=>($matches[7]!='')(int)$matches[7]:false,
'precisao_dir'=>($matches[8]!='')(int)$matches[8]:$locale['int\u frac\u digits'],
“conversao”=>$matches[9]
);
如果($opcoes['usar\u sinal']&&$locale['n\u sign\u posn']==0){
$locale['n\u sign\u posn']=1;
}elseif($opcoes['usar_parenteses']){
$locale['n\u sign\u posn']=0;
}
如果($opcoes['precisao_dir']){
$locale['frac_digits']=$opcoes['precisao_dir'];
}
如果($opcoes['nao_agrupar']){
$locale['mon\u-sip']='';
}
$tipo_sinal=$valor>=0?'p':'n';
如果($opcoes['ignorar_simbolo']){
$simbolo='';
}否则{
$simbolo=$opcoes['conversao']='n'?$locale['currency\u symbol']
:$locale['int\u curr\u symbol'];
}
$numero=数字格式(abs($valor)、$locale['frac_digits']、$locale['mon_decimal_point']、$locale['mon_数千_sep');
$sinal=$valor>=0?$locale['positive_-sign']:$locale['negative_-sign'];
$simbolo_antes=$locale[$tipo_sinal.“'u cs_在前面”];
$espaco1=$locale[$tipo_sinal.''sep_by_space']==1?'':;
$espaco2=$locale[$tipo_sinal.''sep_by_space']==2?'':;
$formatado='';
开关($locale[$tipo_sinal.''u sign_posn']){
案例0:
如果($simbolo_antes){
$formatado='('.$simbolo.$espaco1.$numero');
}否则{
$formatado='('.$numero.$espaco1.$simbolo.');
}
打破
C
$cartOutput = "";
$cartTotal = "";
$pp_checkout_btn = '';
$product_id_array = '';
if (!isset($_SESSION["cart_array"]) || count($_SESSION["cart_array"]) < 1) {
$cartOutput = "<h2 align='center'>Your shopping cart is empty</h2>";
} else {
// Start PayPal Checkout Button
$pp_checkout_btn .= '<form action="https://www.paypal.com/cgi-bin/webscr" method="post">
<input type="hidden" name="cmd" value="_cart">
<input type="hidden" name="upload" value="1">
<input type="hidden" name="business" value="you@youremail.com">';
// Start the For Each loop
$i = 0;
foreach ($_SESSION["cart_array"] as $each_item) {
$item_id = $each_item['item_id'];
$sql = mysql_query("SELECT * FROM products WHERE id='$item_id' LIMIT 1");
while ($row = mysql_fetch_array($sql)) {
$product_name = $row["product_name"];
$price = $row["price"];
$details = $row["details"];
}
$pricetotal = $price * $each_item['quantity'];
$cartTotal = $pricetotal + $cartTotal;
setlocale(LC_MONETARY, "en_US");
$pricetotal = money_format("%10.2n", $pricetotal);
// Dynamic Checkout Btn Assembly
$x = $i + 1;
$pp_checkout_btn .= '<input type="hidden" name="item_name_' . $x . '" value="' . $product_name . '">
<input type="hidden" name="amount_' . $x . '" value="' . $price . '">
<input type="hidden" name="quantity_' . $x . '" value="' . $each_item['quantity'] . '"> ';
// Create the product array variable
$product_id_array .= "$item_id-".$each_item['quantity'].",";
// Dynamic table row assembly
$cartOutput .= "<tr>";
$cartOutput .= '<td><a href="product.php?id=' . $item_id . '">' . $product_name . '</a><br /><img src="inventory_images/' . $item_id . '.jpg" alt="' . $product_name. '" width="40" height="52" border="1" /></td>';
$cartOutput .= '<td>' . $details . '</td>';
$cartOutput .= '<td>$' . $price . '</td>';
$cartOutput .= '<td><form action="cart.php" method="post">
<input name="quantity" type="text" value="' . $each_item['quantity'] . '" size="1" maxlength="2" />
<input name="adjustBtn' . $item_id . '" type="submit" value="change" />
<input name="item_to_adjust" type="hidden" value="' . $item_id . '" />
</form></td>';
//$cartOutput .= '<td>' . $each_item['quantity'] . '</td>';
$cartOutput .= '<td>' . $pricetotal . '</td>';
$cartOutput .= '<td><form action="cart.php" method="post"><input name="deleteBtn' . $item_id . '" type="submit" value="X" /><input name="index_to_remove" type="hidden" value="' . $i . '" /></form></td>';
$cartOutput .= '</tr>';
$i++;
}
setlocale(LC_MONETARY, "en_US");
$cartTotal = money_format("%10.2n", $cartTotal);
$cartTotal = "<div style='font-size:18px; margin-top:12px;' align='right'>Cart Total : ".$cartTotal." USD</div>";
// Finish the Paypal Checkout Btn
$pp_checkout_btn .= '<input type="hidden" name="custom" value="' . $product_id_array . '">
<input type="hidden" name="notify_url" value="https://www.yoursite.com/storescripts/my_ipn.php">
<input type="hidden" name="return" value="https://www.yoursite.com/checkout_complete.php">
<input type="hidden" name="rm" value="2">
<input type="hidden" name="cbt" value="Return to The Store">
<input type="hidden" name="cancel_return" value="https://www.yoursite.com/paypal_cancel.php">
<input type="hidden" name="lc" value="US">
<input type="hidden" name="currency_code" value="USD">
<input type="image" src="http://www.paypal.com/en_US/i/btn/x-click-but01.gif" name="submit" alt="Make payments with PayPal - its fast, free and secure!">
</form>';
}
?>
function asDollars($value) {
if ($value<0) return "-".asDollars(-$value);
return '$' . number_format($value, 2);
}
$pricetotal = money_format("%10.2n", $pricetotal);
$pricetotal = asDollars($pricetotal);
if ( ! function_exists( 'money_format' ) ) {
function money_format($format, $number)
{
$regex = '/%((?:[\^!\-]|\+|\(|\=.)*)([0-9]+)?'.
'(?:#([0-9]+))?(?:\.([0-9]+))?([in%])/';
if (setlocale(LC_MONETARY, 0) == 'C') {
setlocale(LC_MONETARY, '');
}
$locale = localeconv();
preg_match_all($regex, $format, $matches, PREG_SET_ORDER);
foreach ($matches as $fmatch) {
$value = floatval($number);
$flags = array(
'fillchar' => preg_match('/\=(.)/', $fmatch[1], $match) ?
$match[1] : ' ',
'nogroup' => preg_match('/\^/', $fmatch[1]) > 0,
'usesignal' => preg_match('/\+|\(/', $fmatch[1], $match) ?
$match[0] : '+',
'nosimbol' => preg_match('/\!/', $fmatch[1]) > 0,
'isleft' => preg_match('/\-/', $fmatch[1]) > 0
);
$width = trim($fmatch[2]) ? (int)$fmatch[2] : 0;
$left = trim($fmatch[3]) ? (int)$fmatch[3] : 0;
$right = trim($fmatch[4]) ? (int)$fmatch[4] : $locale['int_frac_digits'];
$conversion = $fmatch[5];
$positive = true;
if ($value < 0) {
$positive = false;
$value *= -1;
}
$letter = $positive ? 'p' : 'n';
$prefix = $suffix = $cprefix = $csuffix = $signal = '';
$signal = $positive ? $locale['positive_sign'] : $locale['negative_sign'];
switch (true) {
case $locale["{$letter}_sign_posn"] == 1 && $flags['usesignal'] == '+':
$prefix = $signal;
break;
case $locale["{$letter}_sign_posn"] == 2 && $flags['usesignal'] == '+':
$suffix = $signal;
break;
case $locale["{$letter}_sign_posn"] == 3 && $flags['usesignal'] == '+':
$cprefix = $signal;
break;
case $locale["{$letter}_sign_posn"] == 4 && $flags['usesignal'] == '+':
$csuffix = $signal;
break;
case $flags['usesignal'] == '(':
case $locale["{$letter}_sign_posn"] == 0:
$prefix = '(';
$suffix = ')';
break;
}
if (!$flags['nosimbol']) {
$currency = $cprefix .
($conversion == 'i' ? $locale['int_curr_symbol'] : $locale['currency_symbol']) .
$csuffix;
} else {
$currency = '';
}
$space = $locale["{$letter}_sep_by_space"] ? ' ' : '';
$value = number_format($value, $right, $locale['mon_decimal_point'],
$flags['nogroup'] ? '' : $locale['mon_thousands_sep']);
$value = @explode($locale['mon_decimal_point'], $value);
$n = strlen($prefix) + strlen($currency) + strlen($value[0]);
if ($left > 0 && $left > $n) {
$value[0] = str_repeat($flags['fillchar'], $left - $n) . $value[0];
}
$value = implode($locale['mon_decimal_point'], $value);
if ($locale["{$letter}_cs_precedes"]) {
$value = $prefix . $currency . $space . $value . $suffix;
} else {
$value = $prefix . $value . $space . $currency . $suffix;
}
if ($width > 0) {
$value = str_pad($value, $width, $flags['fillchar'], $flags['isleft'] ?
STR_PAD_RIGHT : STR_PAD_LEFT);
}
$format = str_replace($fmatch[0], $value, $format);
}
return $format;
}
}