I am trying to use functions to query my DB to save time, code, and lines.
Two functions:
function 1 - queries DB and writes/returns values depending on values sent to it
function 2 - queries the DB using function 1 and works with the values and echos the result
Function 1 works fine by itself as long as I echo the intended values instead of returning them. However I want to return the values to function 2, but this is not happening. The returned values are null, resulting in a zero value in function 2 no matter what.
I have reviewed and implemented the examples in the documentation for Returning Values and References Explained . This has not helped me at all unless I just keep typing it wrong over and over again. 🙂
Is there a limitation or something else I have not read about yet?
Here is my code in case you want to see it:
<?php
function RunQuery($varQuery,$varWhich=0,$varWrite=0){
$varempty=money_format('%#4n',0.00);
if ($varWhich==1){
# Find Set-Up Fee
$sql=mysql_query("SELECT pl_sku,pl_assoc_sku
FROM ssd_pricelist
WHERE pl_sku = '$varQuery'")or die("Invalid Query: ".mysql_error());
while ($dataset=mysql_fetch_row($sql)){
$varQuery=$dataset[1];
}
}elseif($varWhich==2){
# Find Monthly Maintenance Fee
$sql=mysql_query("SELECT pl_sku,pl_mon_maint
FROM ssd_pricelist
WHERE pl_sku = '$varQuery'")or die("Invalid Query: ".mysql_error());
while ($dataset=mysql_fetch_row($sql)){
if ($varWrite==1){
return(money_format('%#4n',$dataset[1]));
}else{
echo money_format('%#4n',$dataset[1]);
return;
}
}
}
if ($varQuery!="none"){
$sql=mysql_query("SELECT pl_sku,pl_price
FROM ssd_pricelist
WHERE pl_sku = '$varQuery'") or die("Invalid Query:".mysql_error());
while ($dataset=mysql_fetch_row($sql)){
if ($varWrite==1){
return(money_format('%#4n',$dataset[1]));
}else{
echo money_format('%#4n',$dataset[1]);
}
}
}else{
if ($varWrite==1){
return $varempty;
}else{
echo $varempty;
}
}
} # End Function
function FigureTotal($varPkg,$varCost,$varIncMaint){
global $varPkg;
$varPkgPrice=RunQuery($varPkg,0,1);
#echo "\$varPkgPrice=".$varPkgPrice."<br />";
#echo "<b>Package Price:</b> ";
#print_r($varPkgPrice);
#echo "<br />";
$varSetUpFee=RunQuery($varPkg,1,1);
#echo "\$varSetupFee=".$varSetUpFee."<br />";
#echo "<b>Set Up Fee:</b> ";
#print_r($varSetUpFee);
#echo "<br />";
if ($varIncMaint==1){
$varMaintFee=RunQuery($varPkg,2,1);
#echo "\$varMaintFee=".$varMaintFee."<br />";
}else{
$varMaintFee=0.00;
}
#echo "<b>Maint Fee:</b> ";
#print_r($varMaintFee);
#echo "<br />";
$TotalDue=($varPkgPrice+$varSetUpFee+$varMaintFee);
#echo "\$TotalDue=".$TotalDue."<br />";
#echo "<b>Total Due 1:</b> ";
#print_r($TotalDue);
#echo "<br />";
if ($varCost==1){$TotalDue=($TotalDue/2);}
#echo "<b>Total Due 2:</b> ";
#print_r($TotalDue);
#echo "<br />";
echo money_format('%#4n',$TotalDue);
} # End Function
?>
Any help is GREATLY appreciated!