Dersuss
Dersuss

Reputation: 1

PHP inner join categories

I got stuck on something with PHP., I am trying to JOIN two tables

$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.

Upvotes: 0

Views: 660

Answers (2)

Lars
Lars

Reputation: 600

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);

Upvotes: 0

Dave
Dave

Reputation: 3658

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.

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

Upvotes: 1

Related Questions