1
您好,我正在尝试构建我的第一个宁静的Web服务,并使用来自lorna jane mitchell博客的指令。将变量从一个类传递给另一个PhP
如果REQ来通过以下链接:http://localhost:8888/lorna/index.php/tree/getpath?node_id=75 我调用该函数的getPath传球NODE_ID 功能获得路径是这样的:
class NestedSet
{
public function getPath($id) {
$sql = "SELECT p." . $this->pk . ", p." . $this->name . " FROM ". $this->table . " n, " . $this->table . " p WHERE n.lft BETWEEN p.lft AND p.rgt AND n." . $this->pk ." = " . $id . " ORDER BY p.lft;";
$result = $this->db->query($sql);
if ($result->num_rows == 0) {
return $this->error(1, true);
}
$path = array();
$i = 0;
while ($row = $result->fetch_assoc()) {
$path[$i] = $row;
$i++;
}
return $path;
}
}
现在我想这个变量$路径传递给该类JsonView看起来像这样:
class JsonView extends ApiView {
public function render($path) {
header('Content-Type: application/json; charset=utf8');
echo json_encode($path);
return true;
}
}
class ApiView {
protected function addCount($data) {
if(!empty($data)) {
// do nothing, this is added earlier
} else {
$data['meta']['count'] = 0;
}
return $data;
}
}
任何想法,我怎么可以通过此JsonView类传递变量$路径或任何其他变量。 非常感谢您的宝贵时间:)
UPDATE这是创建嵌套类对象
public function getAction($request) {
$data = $request->parameters;
if(isset($request->url_elements[2])) {
switch ($request->url_elements[2]) {
case 'getpath':
$id = $data['node_id'];
$nested = new NestedSet();
$nested->getPath($id);
$api = new JsonView();
$api->render($path);
break;
default:
# code...
break;
}
} else {
$nested = new NestedSet();
echo $nested->treeAsHtml();
}
}
感谢您的快速响应生病试试吧:) –
@ akhilp255我尝试过了,我得到这个错误注意:未定义的变量:路径/Applications/MAMP/lorna/controllers/TreeController.php在线66 –
更正上面提到的错误。 $ path仅在类中可用。调用函数getPath()后,路径中的值将返回。您必须将其保存在一个变量中,然后传递给下一个函数。 '$ nested = new NestedSet(); $ path = $ nested-> getPath($ id); $ api = new JsonView(); $ api-> render($ path);' – akhilp2255