I have 2 functions working successfully but unable to find a way so that a value in function 1 as made of $r can be used in function 2 (there are many other functions like function 2 with slight modified calculations)' so how to see that $r as fetched through function 1 is added in function 2 and others
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);
}
2nd Function where wish to use $r as fetched in 1st function
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);
}
CodePudding user response:
First of all, don't repeat your-code, make it a function and call it wherever required, for example, myFetchFunc(...)
.
After that, use caching technics inside said function.
But the "how and which" technic depends on how much you want to prevent repeating the query.
If repeating the query once per request is fine, use global variable/array as cache, like:
<?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;
}
Else, there are a lot of caching libraries, which can store values longer than a single requests life-time.