SQL选择WHERE& LEFT JOIN?

时间:2016-07-02 11:15:42

标签: php mysql

我们的网站建立在TWIG模板上,根据数据库中的表格内容生成类似的页面。根据另一个表中的值选择条目时会出现问题。

从同一个模板生成4个不同的页面(目的地),我想用"为每个"添加到子页面(包)内的链接。标记,由哪个页面(目标)当前打开过滤。包的数据库结构包含字段destination_id。

如果destination_id设置为常量值,它就像魅力一样,但只显示具有设置ID的包,如下例所示。

$sql = "SELECT `id`,`name`,`price`,`days`,`preview`,`permalink`,`main_image` FROM package WHERE `destination_id` = 1 ORDER BY `od` DESC";

我尝试了WHERE和LEFT JOIN子句的多种变体;但是,我仍然无法找到使其按预期工作的方法。

完整代码:

public function DestinationSeperate() {
    global $dbh,$twig,$data;
    $seperate = Process::getSingle('destination',$data['path']['argument'][1],'permalink');
    if($seperate){
        $sql = "SELECT `id`,`name`,`main_image`,`videoid` FROM destinationimage WHERE `destination_id` = ? AND `disabled_bool` = 0 ORDER BY `od` DESC";
        $query = $dbh->prepare($sql);
        $query->execute(array($seperate['id']));
        $images = $query->fetchAll();

        $sql = "SELECT `id`,`name`,`permalink`,`main_image` FROM destination WHERE `disabled_bool` = 0 ORDER BY `od` DESC";
        $destination = array();
        foreach ($dbh->query($sql) as $row)
        {
            $destination[] = array(
                'id' => $row['id'],
                'name' => $row['name'],
                'permalink' => $row['permalink'],
                'image' => $row['main_image'],
                );
        }
        $sql = "SELECT `id`,`name`,`price`,`days`,`preview`,`permalink`,`main_image` FROM package WHERE `destination_id` = 1 ORDER BY `od` DESC";
        $package = array();
        foreach ($dbh->query($sql) as $row)
        {
            $package[] = array(
                'id' => $row['id'],
                'name' => $row['name'],
                'price' => $row['price'],
                'days' => $row['days'],
                'preview' => $row['preview'],
                'permalink' => $row['permalink'],
                'image' => $row['main_image'],
                );
        }
        print $twig->render('default/destination.seperate.html.twig', array('data' => $data,'seperate' => $seperate,'destination' => $destination,'images' => $images, 'package' => $package));
  }

0 个答案:

没有答案