PHP5获取所有类别的页面
这个问题来自我创建的另一个问题,你可以找到,但我已经更改了代码,所以现在看起来像这样: 汽车控制器PHP5获取所有类别的页面,php,Php,这个问题来自我创建的另一个问题,你可以找到,但我已经更改了代码,所以现在看起来像这样: 汽车控制器 public function indexAction(){ $category = new Application_Model_CarMapper(); $gcat = $this->getRequest()->getPathInfo(); //get id $id = $category->find_id_
public function indexAction(){
$category = new Application_Model_CarMapper();
$gcat = $this->getRequest()->getPathInfo();
//get id
$id = $category->find_id_by_name($gcat);
$this->view->title = $category->get_sub_cat_select($id);
}
这是我在mapper中创建的一个新函数:
public function find_id_by_name($name){
$select = $this->getDbTable()->query("SELECT * FROM car_category WHERE category_name = '{$name}'");
$result = $select->fetchAll();
if(!$result) {
return;
}
return $result[0]['ID'];
}
我正在测试它的标题,但它似乎根本没有显示出来。我希望它显示特定类别的下拉菜单,例如
car-live.local/cars/Volvo-->“欢迎来到Volvocarfinder”
car-live.local/cars/BMW-->“欢迎来到宝马寻车器”
我知道它不起作用,因为我不得不进一步拆分URL,因为现在它正在通过URL查找id,但我不确定如何做到这一点:it’如果您能对此有所了解,我将不胜感激。。谢谢
[编辑]
新代码:
public function indexAction(){
$category = new Application_Model_CarMapper();
$gcat = explode("/", $this->getRequest()->getPathInfo());
if(isset($gcat['category_name'])) {
$id = $category->find_id_by_name($gcat);
$this->view->title = $category->get_sub_cat_select($id);
}
}
我不确定我是否正确理解了您的问题,但从代码来看,似乎您在处理URL时遇到了问题。那么,让我们举一个例子:
car-live.local/car/Volvo
到达索引操作后
,您应该通过/
这是返回一个包含URL所有组件的数组,然后使用本例中的最后一个将是Volvo
,并将其发送到find\u id\u by\u name()
方法
我处理这些问题的通常方法如下:
car
并将其“路由”到一个cars类,方法是将请求和其余部分发送给构造函数car
类构造函数获取URLVolvo
的下一部分,并将其“路由”为适当的方法,如getVolvo()
或任何您需要的方法/cars/whatEverCar
=>应按类car
/bike/whatEverBike
=>应按类bike
希望这有帮助
编辑: 您的
find\u id\u by\u name()
/car/find\u id\u by\u name/Volvo
我解释得很好吗?你可以获取部分URL并直接调用方法…然后发送URL的其余部分
编辑2:代码示例
以下代码是解决您的问题的非常好的解决方案:
<?php
// Call this class as soon the site stars
class Router {
public function __construct() {
// In some really cool way get your full URL, for example I'm gonna use a string...
$fullURL = "example.com/Car/findCarById/Volvo";
array_shift($fullURL); // removes "example.com", obvious stuff...
$explodedUrl = explode("/", $fullURL);
$targetClass = $explodedUrl[0]; // Temporary store the target is "Car"
array_shift($fullURL); // Removes "Car", we don't need it anymore...
$target = new $targetClass($fullURL); // call the Car class responsible for handling all the "Car" related stuff!
}
}
class Car {
public function __construct($request) {
// $request is an array with "findCarById", "Volvo"
$targetMethod = $request[0]; // Temporary store the target is "findCarById"
array_shift($request);
$this->$targetMethod($request);
}
private function findCarById($parameter) {
// $parameter is an array with "Volvo"..
// Now to your query and other cool stuff with $parameter[0];
print "I'm the method findCarById() of the class Car called by the main router to find the ID for {$parameter}...";
}
}
?>
您不需要{$name}
因为您没有使用数组[index]
那么我该如何在当前代码中调整它呢?另外,我想改变的是索引,而不是查询本身。@TCB13:谢谢你。@TCB13:你说得有点道理,我理解如何分解URL,但之后一切都变得混乱了。。还有很多类别我不知道e为每种类型的汽车创建一个单独的类和getfunction..另外,这不是客户想要的结构,因为他希望按照自己的条件完成..我已经编辑了代码,现在可以在上面的编辑中看到这些代码,因此如果您能显示从那里开始的方向,我将非常感激?:@ash就像我在编辑中告诉您的那样,只需使用find\u id\u by\u name()
方法创建一个car
类,并将其发送到URL上,如我所述。MainRouter(gets/car/)构建类car
并发送/find_id_by_name/Volvo/然后构造函数从URL调用方法find_id_by_name(),并传递/Volvo part.oohh,技术术语抛出了我。。也许一个例子可以帮助你,或者一步一步?@ash检查我的编辑。花点时间想想,但我相信这才是你真正想要的。别忘了接受答案……谢谢。。我将更改我的代码,并用结果更新您:)