简体   繁体   English

PHP内部联接类别

[英]PHP inner join categories

I got stuck on something with PHP., I am trying to JOIN two tables 我被PHP卡住了,我试图加入两个表

$statement = $database->prepare("SELECT categories.name as category_name ,pages.name as page_name FROM categories INNER JOIN pages ON categories.name = pages.category");
$statement->execute();
$fetch = $statement->fetchAll(PDO::FETCH_ASSOC);

$returnValues = '';
foreach($fetch as $item){
    if(isset($returnValue[$item->category_name]){
        array_push($returnValue[$item->category_name], $item->page_name);
    }else{
        $returnValue[$item->category_name][] = $item->page_name;
    }
}
echo "<pre>";
print_r($returnValue);

Basically I want to get an array which got the category name and behind it all the pages that belongs to that category. 基本上,我想获得一个数组,该数组具有类别名称,并在其后包含该类别的所有页面。 now on this PHP code, I get plenty of array which everyone holds category name and one page.. and I can't sort it out , thanks. 现在在此PHP代码上,我得到了很多数组,每个人都拥有类别名称和一页。.我无法对其进行分类,谢谢。

Alias the category name so that you can tell it apart from the page name. 别名类别名称,以便您可以区分页面名称。 This query will select the category name and all columns from table pages. 该查询将选择类别名称和表页面中的所有列。

$statement = $database->prepare("SELECT categories.name AS category_name, pages.* FROM categories INNER JOIN pages ON categories.name = pages.category");
$statement->execute();
$fetch = $statement->fetchAll(PDO::FETCH_ASSOC);

Group each row by category name. 将每行按类别名称分组。 Change $row['id'] to whatever you use as the primary ID for table pages. 将$ row ['id']更改为用作表页面的主要ID的任何内容。

$categoryPages = array();
foreach ( $fetch as $row ) {
    $categoryPages[$row['category_name']][$row['id']] = $row;
}
print_r($categoryPages);

This is modifications to your original code, should basically do what you want, there might be some small syntax changes needed. 这是对原始代码的修改,基本上应该做您想做的事,可能需要一些小的语法更改。

$statement = $database->prepare("SELECT categories.name as category_name ,pages.name as page_name FROM categories INNER JOIN pages ON categories.name = pages.category");
$statement->execute();
$fetch = $statement->fetchAll(PDO::FETCH_ASSOC);

$returnValues = '';
foreach($fetch as $item){
    if(isset($returnValue[$item->category_name]){
        array_push($returnValue[$item->category_name], $item->page_name);
    }else{
        $returnValue[$item->category_name][] = $item->page_name;
    }
}
echo "<pre>";
print_r($returnValue);

Something like this might work for you... I know it is not perfect and there are something thing missing in the query prepare, but it should be enough to get you going. 这样的事情可能对您有用...我知道它并不完美,查询准备中缺少一些东西,但足以使您顺利进行。

The compiled array should be formatted the way you want. 编译后的数组应采用所需的格式。

$statement = $database->prepare("SELECT categories.name FROM categories");
$statement->execute();
$fetch = $statement->fetchAll(PDO::FETCH_ASSOC);

$compiled = '';
foreach($fetch as $category){
    $statement = $database->prepare("SELECT page.name FROM pages WHERE pages.category = $category");
    $statement->execute();
    $pages = $statement->fetchAll(PDO::FETCH_ASSOC);
    foreach($pages as $page)
        array_push($compiled[$category], $page);
    }
}
echo "<pre>";
print_r($compiled);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM