1

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 class="common-box">
                    <div class="common-box-left">Basic Prem</div>
                    <div class="common-box-right">
                        <input name="voluntary_excess" type="text" class="text-box"  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 class="common-box">
                    <div class="common-box-left">Total Fee </div>
                    <div class="common-box-right">
                        <input name="roadtax" type="text" class="text-box"  value="Rs. '.(round($calculatedtax)) .'" readonly=""/>
                    </div>
                </div>

                <div class="common-box">
                    <div class="common-box-left">Basic Prem</div>
                    <div class="common-box-right">
                        <input name="voluntary_excess" type="text" class="text-box"  value="Rs. '.(round($r)) .'" readonly=""/>
                    </div>
                </div>

                    
                                <div class="common-box">
                    <div class="common-box-left">Final Price </div>
                     <div class="common-box-right">
                    <input name="reg" type="text" class="text-box"  value="Rs. '.((round($a+ $calculatedtax + $r))) .'" readonly=""/>
                   
                    </div>
                </div>                                  
                ';  
    die($list);
}           
1
  • You haven't shared any function definitions in your code. Also, is this problem really related to MySQL? Commented Nov 16, 2022 at 12:40

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks, it looks promising but somehow getting error Notice: Undefined variable: r in F:\wamp64\www\14november2022\taxmodel.php on line 443
n yes query is raised once per request only.
If that line is inside myFetchFunc's logic, then see edit (code needs a default value for $r, as none of the conditions may be met). Otherwise, if you remove duplicate logic, ensure to set $r, like $r = myFetchFunc(...);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.