Frage

I have a table which stores posts created by user on a particular topic.each of these posts belong to one and only one topic. there is a column in the posts table which stores the id of the topic. I would like to display all title of the posts in a page categorized under topic name

example:

Animals

Post 1

Post 2

Post 3

Plants&Trees

Post 1

Post 2

Post 3

My 'posts' table is in following format id posttitle posttext topic_id

My 'topics' table is in following format id topicname

What relationship have to define so that i can access the topicname and id in the following name.

foreach($posts as $post)
echo $post->topics->topicname
War es hilfreich?

Lösung

To say it in words, each topic has many posts and each post belongs to a topic - that is exactly what relationships you need.

When you have defined them, as described in the documentation, you can do something like this:

$topics = ORM::factory("topic")->find_all();
foreach ($topics as $topic) {
    // $topic->topicname
    foreach ($topic->posts as $post) {
        // $post->posttitle
    }
}
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top