v2.5.2
Giriş yap

Listeleme Sorunu

coder
171 defa görüntülendi ve 1 kişi tarafından değerlendirildi

Merhaba arkadaşlar.
Resimdeki gibi bir tablo yapım var.

Image

sections tablosu bölüm isimlerinin tuttulduğu tablo
categories tablosu kategori isimlerinin tuttulduğu tablo
category_sections tablosu ise hangi kategorinin hangi bölüme ait olduğunu tutan tablo

Aşağıdaki şekilde listele yapıyorum ama resimdeki gibi kategoriler çift oluyor. Where de kullandım ama yine aynı.
Bu listeleme işlemini nasıl yapabilirim?

Sorgular


// $bolumler
public function Bolumler()
{
    $sql = "SELECT * FROM sections";
    $query = $this->db->prepare($sql);
    $query->execute();
    $result = $query->fetchAll(PDO::FETCH_ASSOC);
    return $result;
}

// $kategoriler
public function Kategoriler()
{
    $sql = "SELECT * FROM `sections`
            INNER JOIN `category_sections` ON (`sections`.`section_id` = `category_sections`.`section_id`)
            INNER JOIN `categories` ON (`categories`.`category_id` = `category_sections`.`category_id`)";
    $query = $this->db->prepare($sql);
    $query->execute();
    $result = $query->fetchAll(PDO::FETCH_ASSOC);
    return $result;
}

Listeleme

<!-- Sections - Bolumler -->
<?php foreach ($bolumler as $bolum) : ?>
    <h1><?php echo $bolum["section_title"]; ?></h1>
    <table>
        <thead>
        <tr>
            <th>Kategori Adı</th>
        </tr>
        </thead>
        <tbody>
        <!-- Categories - Kategoriler -->
        <?php foreach ($kategoriler as $kategori) : ?>
            <tr>
                <td>
                    <?php echo $kategori["category_name"]; ?>
                </td>
            </tr>
        <?php endforeach; ?>
        <!-- Categories - Kategoriler Bitiş -->
        </tbody>
    </table>
<?php endforeach; ?>
<!-- Sections - Bolumler Bitiş -->

Çıktı

Image

Cevap yaz
Cevaplar (2)
serkan
12 gün önce
// Veritabanından bölüm ve kategori eşleşmelerini çekiyoruz
$sql = "SELECT sections.section_id, sections.section_title, categories.category_name 
        FROM sections
        INNER JOIN category_sections ON sections.section_id = category_sections.section_id
        INNER JOIN categories ON categories.category_id = category_sections.category_id";

$query = $this->db->prepare($sql);
$query->execute();
$data = $query->fetchAll(PDO::FETCH_ASSOC);

// Bölümleri ve kategorileri organize etmek için bir dizi oluşturuyoruz
$sections = [];
foreach ($data as $row) {
    $section_id = $row["section_id"];
    if (!isset($sections[$section_id])) {
        $sections[$section_id] = [
            "section_title" => $row["section_title"],
            "categories" => []
        ];
    }
    $sections[$section_id]["categories"][] = $row["category_name"];
}
?>

<!-- Sections - Bölümler -->
<?php foreach ($sections as $section) : ?>

<h1><?php echo $section["section_title"]; ?></h1>
<table>
    <thead>
    <tr>
        <th>Kategori Adı</th>
    </tr>
    </thead>
    <tbody>
    <!-- Categories - Kategoriler -->
    <?php foreach ($section["categories"] as $category_name) : ?>
        <tr>
            <td><?php echo $category_name; ?></td>
        </tr>
    <?php endforeach; ?>
    </tbody>
</table>

<?php endforeach; ?>
<!-- Sections - Bölümler Bitiş -->`

serkan
12 gün önce

<?php
// Veritabanından bölüm ve kategori eşleşmelerini çekiyoruz
$sql = "SELECT sections.section_id, sections.section_title, categories.category_name

    FROM sections
    INNER JOIN category_sections ON sections.section_id = category_sections.section_id
    INNER JOIN categories ON categories.category_id = category_sections.category_id";

$query = $this->db->prepare($sql);
$query->execute();
$data = $query->fetchAll(PDO::FETCH_ASSOC);

// Bölümleri ve kategorileri organize etmek için bir dizi oluşturuyoruz
$sections = [];
foreach ($data as $row) {

$section_id = $row["section_id"];
if (!isset($sections[$section_id])) {
    $sections[$section_id] = [
        "section_title" => $row["section_title"],
        "categories" => []
    ];
}
$sections[$section_id]["categories"][] = $row["category_name"];

}
?>

<!-- Sections - Bölümler -->
<?php foreach ($sections as $section) : ?>

<h1><?php echo $section["section_title"]; ?></h1>
<table>
    <thead>
    <tr>
        <th>Kategori Adı</th>
    </tr>
    </thead>
    <tbody>
    <!-- Categories - Kategoriler -->
    <?php foreach ($section["categories"] as $category_name) : ?>
        <tr>
            <td><?php echo $category_name; ?></td>
        </tr>
    <?php endforeach; ?>
    </tbody>
</table>

<?php endforeach; ?>
<!-- Sections - Bölümler Bitiş -->