Getting a value from a database table dynamically I am trying to create a forum using php in a MVC architecture.
This is my database setup:

Table: forum_categories



`forum_categories` (
`cat_id` INT(8) NOT NULL AUTO_INCREMENT,
`cat_title` VARCHAR(255) NOT NULL,
`cat_desc` TEXT NOT NULL,
PRIMARY KEY (`cat_id`),
UNIQUE KEY (`cat_title`)


Table: forum_topics

`forum_topics` (
`topic_id` INT(8) NOT NULL AUTO_INCREMENT,
`cat_id` INT(8) NOT NULL COMMENT 'foreign key with forum_categories table',
`user_id` INT(11) NOT NULL COMMENT 'foreign key with users table',
`topic_title` VARCHAR(255) NOT NULL,
`topic_desc` TEXT NOT NULL, 
`topic_date` DATETIME DEFAULT NULL,
PRIMARY KEY (`topic_id`),
FOREIGN KEY (`cat_id`) REFERENCES forum_categories (`cat_id`) ON DELETE CASCADE ON UPDATE CASCADE


Example of the functionality, I would like to achieve:


  Category 1 has cat_id = 1
  Category 2 has cat_id = 2  
  
  Topic 1 has cat_id = 1
  Topic 2 has cat_id = 2


Now when category 1 is selected I just want topic 1 to show.
If category 2 is selected I just want topic 2 to show.

The following code does that:

Controller



 /**
 * Show all the topics in the chosen category
 */
public function showForumTopicsCat1()
{
    $topic_model = $this-&gt;loadModel('Forum');
    $this-&gt;view-&gt;forum_topics = $topic_model-&gt;getForumTopics(1);
    $this-&gt;view-&gt;render('forum/viewTopics');
}

public function showForumTopicsCat2()
{
    $topic_model = $this-&gt;loadModel('Forum');
    $this-&gt;view-&gt;forum_topics = $topic_model-&gt;getForumTopics(2);
    $this-&gt;view-&gt;render('forum/viewTopics');
}

public function showForumTopicsCat3()
{
    $topic_model = $this-&gt;loadModel('Forum');
    $this-&gt;view-&gt;forum_topics = $topic_model-&gt;getForumTopics(3);
    $this-&gt;view-&gt;render('forum/viewTopics');
}

public function showForumTopicsCat4()
{
    $topic_model = $this-&gt;loadModel('Forum');
    $this-&gt;view-&gt;forum_topics = $topic_model-&gt;getForumTopics(4);
    $this-&gt;view-&gt;render('forum/viewTopics');
}


Model

/**
 * Gets an array that contains all the forum topics in the database.
 * Each array element is an object, containing a specific topic's data.
 * @return array All the forum topics
 */
public function getForumTopics($cat_id) 
{
    $sql = 'SELECT * FROM forum_categories fc JOIN forum_topics ft ON fc.cat_id = ft.cat_id WHERE fc.cat_id = :cat_id ORDER BY ft.topic_date DESC';
    $query = $this-&gt;db-&gt;prepare($sql);
    $query-&gt;execute(array(':cat_id' =&gt; $cat_id));

    return $query-&gt;fetchAll(); 
}


View

&lt;?php
    if ($this-&gt;forum_categories) {
            echo '&lt;a href="'. URL . 'forum/showForumTopicsCat1/"&gt;Category1&lt;/a&gt;';
            echo '&lt;a href="'. URL . 'forum/showForumTopicsCat2/"&gt;Category2&lt;/a&gt;';
            echo '&lt;a href="'. URL . 'forum/showForumTopicsCat3/"&gt;Category3&lt;/a&gt;';
            echo '&lt;a href="'. URL . 'forum/showForumTopicsCat4/"&gt;Category4&lt;/a&gt;';
    } else {
        echo '&lt;p&gt;There are no categories.&lt;/p&gt;';
    }
?&gt;


My Concern: Is there anyway to this more DRY and more reusable?
you could do something like that : 

public function showForumTopicsByCat($catId)
{
    $topic_model = $this-&gt;loadModel('Forum');
    $this-&gt;view-&gt;forum_topics = $topic_model-&gt;getForumTopics($catId);
    $this-&gt;view-&gt;render('forum/viewTopics');
}


and on view 

&lt;?php
if ($this-&gt;forum_categories) {
   for($i=o; $i &lt; $numberCat; $i++){
       echo '&lt;a href="'. URL . 'forum/showForumTopicsCat'.$i.'/"&gt;Category1&lt;/a&gt;';
    }
   } else {
    echo '&lt;p&gt;There are no categories.&lt;/p&gt;';
}
?&gt;