Re: Class function calling another function in class
| From: | Chris | Date: | Thu, 19 May 2005 05:09:44 +0000 |
| Subject: | Re: Class function calling another function in class | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-215564@lists.php.net to get a copy of this message | ||
You keep on appending $this->str to itself. Comments are inline.
Charles Kline wrote:
Hi all, Not sure if I am doing this right and I can't figure it out, was hoping someone here can help. I have an organization table in mySQL. Pretty standard. My first function getDepts() is working as I intend and returning the tree just as I like it. The new piece I added in there that is not working now is the call to getPositions() within the first function. What I am trying to do is once I get a Department, I want to loop through the Positions table and get the positions that are under that Department. This code goes haywire and loops for ever printing a huge list to the screen. Eventually I need to return this data not to the screen but into an array. Anyone see what I might have wrong in my logic? I have a class and it contains these two functions.here you are appending the result of ::getPostions() to ->strfunction getDepts ( $parent = 0, $level = 1 ) { $sql = 'SELECT BudgetedOrganization.* '; $sql .= 'FROM BudgetedOrganization '; $sql .= 'WHERE BudgetedOrganization.boSuperiorOrgID = ' . $parent;$rs = $this->retrieveData($sql); if ($rs) { while($row = mysql_fetch_array($rs)){ $num = 1;while ($num <= $level) { $this->str .= "\t"; $num++; } $this->str .= $row['boOrgID'] . ", " . $row ['boOrgName'] . "\n";// just added this and it ain't working $this->str .= $this->getPositions($row['boOrgID']);
appending the row to ->str$this->getDepts($row['boOrgID'],$level+1); } } return($this->str); }function getPositions ( $org = 0 ) { $sql = 'SELECT BudgetedPosition.* '; $sql .= 'FROM BudgetedPosition '; $sql .= 'WHERE BudgetedPosition.bp_boOrgID = ' . $org; //echo $sql; $rs = $this->retrieveData($sql); if ($rs) { while($row = mysql_fetch_array($rs)){ $this->str .= " - " . $row['bpPositionID'] . "\n";
}
}
return($this->str);
from getPositions you are returning ->str , so getPositions is appending each row to ->str , then returning ->str , which is then being appended to ->str
Chris}Later.... $depts = $org->getDepts(); echo "<pre>" . $depts . "</pre>"; Thanks, Charles I'd say just don't append what ::getPositions() is returning.. and remove the return call if it makes sense to do so.