返回适当参数的值

时间:2014-08-18 06:48:12

标签: php mysql

这里有没有可能为$ strValues和$ strThumbImages写单一回报我需要每次都返回$ strValues但是返回$ strThumbImages就在它可用的时候..

 function doSelectBranchRecords($objArray,$ImageId = NULL)
        {
            global $global_config;
            $strWhereClause = '';
            if ($objArray['frmNameSearch']) {
                $strWhereClause.= " AND A.branch_ident = '".$objArray['frmNameSearch']."' ";
            }
            if ($objArray['frmLoanSearch']) {
                $strWhereClause.= " AND A.loan_ident = '".$objArray['frmLoanSearch']."' ";
            }
            if ($objArray['frmBeneficiarySearch']) {
                $strWhereClause.= " AND A.beneficiary_idents = '".$objArray['frmBeneficiarySearch']."' ";
            }
            if ($objArray['frmDateSearch']) {

                $strDate = explode("-", $objArray['frmDateSearch']);
                $strAccountstarted = $strDate[2].'-'.$strDate[1].'-'.$strDate[0];

                $strWhereClause.= " AND A.account_started = '".$strAccountstarted."' ";
                /*printArray($strWhereClause); exit;*/
            }

            if ($ImageId) {

                $strThumbImages = $global_config["SiteGlobalUploadPath"].$ImageId;
                return $strThumbImages;
            }


            $strSqlSelect = "SELECT A.*,B.branch_name FROM tbl_companydetails as A,tbl_branchdetails as B where A.branch_ident=B.branch_id $strWhereClause order by company_id DESC";
            $strValues = SelectQry($strSqlSelect);
            return $strValues;

        }

3 个答案:

答案 0 :(得分:1)

php函数无法返回多个变量。您可以通过返回包含一个或两个键的数组来获取函数的两个结果,然后检查您的调用代码以查看已设置的内容。所以你的功能结束了:

        ...
        }

        $output = array();

        if ($ImageId) {
            $strThumbImages = $global_config["SiteGlobalUploadPath"].$ImageId;
            $output['thumbImages'] = $strThumbImages;
        }

        $strSqlSelect = "SELECT A.*,B.branch_name FROM tbl_companydetails as A,tbl_branchdetails as B where A.branch_ident=B.branch_id $strWhereClause order by company_id DESC";
        $strValues = SelectQry($strSqlSelect);
        $output['strValues'] = $strValues;
        return $output;

    }

答案 1 :(得分:0)

如上所述@Simon Brahan说你不能返回多个变量。

所以你可以使用不同键的数组。但是你也应该检查是否设置了$ ImageId而不是null。

 if (isset($ImageId) && !is_null($ImageId)) {
            $strThumbImages = $global_config["SiteGlobalUploadPath"].$ImageId;
            $output['thumbImages'] = $strThumbImages;
        }

        $strSqlSelect = "SELECT A.*,B.branch_name FROM tbl_companydetails as A,tbl_branchdetails as B where A.branch_ident=B.branch_id $strWhereClause order by company_id DESC";
        $strValues = SelectQry($strSqlSelect);
        $output['strValues'] = $strValues;
        return $output;

答案 2 :(得分:0)

@Simon和@Suchit是正确的,因为你不能返回多个变量。但是在php中,返回包含多个项目的列表是非常直接的:

function getXYZ(){
  return array(4,5,6);
}

list($x,$y,$z) = getXYZ();

所以在你的情况下,这可能是这样的:

$strThumbImages  = null;
if ($ImageId) {
  $strThumbImages = $global_config["SiteGlobalUploadPath"].$ImageId;
}
$strSqlSelect = "SELECT A.*,B.branch_name FROM tbl_companydetails as A,tbl_branchdetails as B where     A.branch_ident=B.branch_id $strWhereClause order by company_id DESC";
$strValues = SelectQry($strSqlSelect);
return array($strValues, $strThumbImages);