markerpower
markerpower

Reputation: 345

Displaying Data From Database

I'm trying to display data from a database and I want to know what I'm doing wrong.

Class:

class Information {

    protected $info;

    protected $sinfo;

    public function __construct()
    {
        $this->db = new Config;

        $r = $this->db->query('SELECT * FROM categories');

        while($row = $r->fetch_array())
        {
            $this->info[] = $row;
        }

    }

    public function getCategory()
    {
        return $this->info;
    }

    public function getCategoryTitle($category_title)
    {
        echo $category_title;
    }

    public function getListing($category_id)
    {

        $s = $this->db->query("SELECT * FROM listings WHERE category_id = $category_id");

        while($sow = $s->fetch_array())
        {
            $this->sinfo[] = $sow;
        }
        return $this->sinfo;
    }

    public function getListingTitle($listing_title)
    {
        echo $listing_title;
    }

}   

Display Info:

$display_listings = new Info();

foreach ($display_listings->getCategory() as $row) 
{
    $display_listings->getCategoryTitle($row['category_title']);

    foreach ($display_listings->getListing($row['category_id']) as $sow)
    {
        $display_listings->getListingTitle($sow['listing_title']);
    }
}

This shows:

Category 1

Listing 1

Category 2

Listing 1

Listing 2

Category 3

Listing 1

Listing 2

Listing 3

I want it to show:

Category 1

Listing 1

Category 2

Listing 2

Category 3

Listing 3

Upvotes: 0

Views: 119

Answers (1)

Cassie Smith
Cassie Smith

Reputation: 501

Maybe try putting this at the beginning of getListing:

$this->sinfo = '';

It looks like the array is keeping the previous values.

Upvotes: 2

Related Questions