Page 1 of 1

Use output of one function in another of the same class

Posted: Wed Feb 17, 2010 8:30 am
by MCouche
Hello,

I would like to create a class to help building dynamic SQL Queries.
This class will contain, a.o., a function that will use the output of another function contained in the same class.
The code I have now is as follows:

<?php

class EMail_Helper
{

function Handle_Auto_EMail($User_Category, $User_ID, $List_Dossiers) {

$SQL_IN_Part = $This->Build_IN_SQL_Part($List_Dossiers);
....
}


function Build_IN_SQL_Part($List_Dossiers) {
$TestStr = explode('|', $List_Dossiers);
$i = 0;
$In_Statement = ' IN (';
while ($i < count($TestStr))
{
$In_Statement = $In_Statement . $TestStr[$i] . ', ';
$i++;
}
$In_Statement = trim($In_Statement);
$In_Statement = substr($In_Statement,0, strlen($In_Statement)-1);
$In_Statement = $In_Statement . ')';

Return $In_Statement;
}

}

?>

My objective is to build the query using one single call to the function i.e.:
...
$Cls_EMail_Helper = new EMail_Helper();
...
$Cls_EMail_Helper->Handle_Auto_EMail('VET',$current_vet, $Vet_Dossiers);
...

where $Vet_Dossiers is a pipe separated list.

The issue I face is that although Build_IN_SQL_Part provides the expected output string, I am unable to retrieve it in the $SQL_IN_Part variable of the first function.

Thnak you

Michel

Re: Use output of one function in another of the same class

Posted: Thu Feb 18, 2010 9:43 am
by xtiano77
I took the liberty to make some minor changes to your code, mainly so I could understand it better. I don't have my server available, but I think the problem lied on the code inside the loop. Your post showed "=" when it should be ".=" . I believe without it you were only getting the value from the last iteration. Hope it helps.

Code: Select all

 
<?php
class EMail_Helper {
    function Handle_Auto_EMail($User_Category, $User_ID, $List_Dossiers) {
        $SQL_IN_Part = $this->Build_IN_SQL_Part($List_Dossiers);
        ...
    }
 
    function Build_IN_SQL_Part($List_Dossiers) {
        $TestStr = explode('|', $List_Dossiers);
        $In_Statement = ' IN (';
        for($i = 0; $i < count($TestStr); $i++){
            $In_Statement .= $In_Statement . $TestStr[$i] . ', ';
        }
        $In_Statement = trim($In_Statement);
        $In_Statement = substr($In_Statement,0, strlen($In_Statement)-1);
        $In_Statement = $In_Statement . ')';
        return $In_Statement;
    }
}
?>