我有 2 個函式成功運行,但無法找到一種方法,以便函式 1 中由 $r 構成的值可以用于函式 2(還有許多其他函式,例如函式 2,計算略有修改)'那么如何查看通過函式 1 獲取的 $r 添加到函式 2 和其他函式中
if($NewCarVariantDetail){
$db = JFactory::getDBO();
$query = $db->getQuery(true);
$query->select($db->qn(array('id','v_price','v_fuel_type','v_capacity')))
->from($db->qn('#__newprod_variants'))
->where([$db->qn('state') . ' = 1',
$db->qn('id') . ' = ' . (int)$NewCarVariantDetail])
->order('v_price/2 asc','v_fuel_type desc');
$db->setQuery($query);
$rows = $db->loadObject();
$a = $rows->v_price;
$b = $rows->v_fuel_type;
$eng = $rows->v_capacity;
$z=.02034;
$p=.01809;
$q=.01779;
$qr=.01737;
$sma=7325;
$mid=11975;
$lar=27930;
if ($eng < 1000)
{
$r = (($z*$a) $sma);
}
else if((($eng >= 1000) and ($eng < 1500)))
{
$r = (($p*$a) $mid);
}
else if(($eng >= 1500) and ($a < 1900000))
{
$r = (($q*$a) $lar);
}
else if(($eng >= 1500) and ($a > 1900000))
{
$r = (($qr*$a) $lar);
}
$list='
<div >
<div >Basic Prem</div>
<div >
<input name="voluntary_excess" type="text" value="Rs. '.(round($r)) .'" readonly=""/>
</div>
</div>
';
die($list);
}
第二個函式希望使用第一個函式中獲取的 $r
if($RtoDetail){
$db = JFactory::getDBO();
$query = $db->getQuery(true);
$query->select($db->qn(array('id','v_price','v_fuel_type','v_capacity')))
->from($db->qn('#__newprod_variants'))
->where([$db->qn('state') . ' = 1',
$db->qn('id') . ' = ' . (int)$RtoDetail]);
$db->setQuery($query);
$rows = $db->loadObject();
$a = $rows->v_price;
$b = trim($rows->v_fuel_type);
$eng = $rows->v_capacity;
$calculatedtax = '';
if (($a < 400000) && ($b == 'Petrol'))
{
$calculatedtax = (.04*$a);
}
else if((($a >= 400000) and ($a < 600000)) and ($b == 'Petrol'))
{
$calculatedtax = (.05*$a);
}
else if((($a >= 600000) and ($a < 1000000)) and ($b == 'Petrol'))
{
$calculatedtax = (.07*$a);
}
else if((($a >= 1000000)) and ($b == 'Petrol'))
{
$calculatedtax = (.10*$a);
}
$list='
<h2><u> Fees </u></h2>
<div >
<div >Total Fee </div>
<div >
<input name="roadtax" type="text" value="Rs. '.(round($calculatedtax)) .'" readonly=""/>
</div>
</div>
<div >
<div >Basic Prem</div>
<div >
<input name="voluntary_excess" type="text" value="Rs. '.(round($r)) .'" readonly=""/>
</div>
</div>
<div >
<div >Final Price </div>
<div >
<input name="reg" type="text" value="Rs. '.((round($a $calculatedtax $r))) .'" readonly=""/>
</div>
</div>
';
die($list);
}
uj5u.com熱心網友回復:
首先,不要重復你的代碼,把它變成一個函式并在需要的地方呼叫它,例如,myFetchFunc(...).
之后,在所述函式內使用快取技術。
但是“如何以及使用哪種”技術取決于您希望防止重復查詢的程度。
如果每個請求重復查詢一次沒問題,請使用全域變數/陣列作為快取,例如:
<?php
function myFetchFunc($NewCarVariantDetail) {
// Validate arguments.
if ( ! $NewCarVariantDetail) {
return NULL;
}
// Load from cache (if called before).
$uniqueKey = 'myFetchResult_variant-' . $NewCarVariantDetail;
$cache = & $GLOBALS[$uniqueKey];
if ($cache) {
return $cache;
}
$db = JFactory::getDBO();
$query = $db->getQuery(true);
$query->select($db->qn(array('id', 'v_price', 'v_fuel_type', 'v_capacity')))
->from($db->qn('#__newprod_variants'))
->where([$db->qn('state') . ' = 1',
$db->qn('id') . ' = ' . (int) $NewCarVariantDetail])
->order('v_price/2 asc', 'v_fuel_type desc');
$db->setQuery($query);
$rows = $db->loadObject();
$a = $rows->v_price;
$b = $rows->v_fuel_type;
$eng = $rows->v_capacity;
$z = .02034;
$p = .01809;
$q = .01779;
$qr = .01737;
$sma = 7325;
$mid = 11975;
$lar = 27930;
$r = 0; // Default value here.
if ($eng < 1000) {
$r = (($z * $a) $sma);
} else if ((($eng >= 1000) and ($eng < 1500))) {
$r = (($p * $a) $mid);
} else if (($eng >= 1500) and ($a < 1900000)) {
$r = (($q * $a) $lar);
} else if (($eng >= 1500) and ($a > 1900000)) {
$r = (($qr * $a) $lar);
}
// Save to cache.
$cache = $r;
return $r;
}
否則,有很多快取庫,可以存盤比單個請求生命周期更長的值。
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/535375.html
標籤:PHP数据库计算
