Welcome Guest, Not a member yet? Register   Sign In
Example: Displaying FAQs
#1

[eluser]carvingCode[/eluser]
This is a bit of trivial code, but I thought it may come in handy for someone.

It is code used for displaying FAQs, i.e.: standard question/answer format. The code uses an array resulting from a db query:

SQL table

Code:
CREATE TABLE IF NOT EXISTS `page_faqs` (
  `id` int(10) NOT NULL AUTO_INCREMENT,
  `user_id` mediumint(8) NOT NULL DEFAULT '0',
  `created_on` int(10) NOT NULL DEFAULT '0',
  `modified_by` mediumint(8) NOT NULL DEFAULT '0',
  `modified_on` int(10) NOT NULL DEFAULT '0',
  `title` varchar(80) NOT NULL,
  `body` text NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=4 ;

SQL Query

Code:
function get_faqs() {
    $this->db->select('*');
    $this->db->order_by('created_on', 'asc'); // or whatever is desired

    $query = $this->db->get('page_faqs');

    return $query->result_array();
}


The array is then passed into the following view code:

Code:
<div id="page-faq">
    <h2>FAQs</h2>
    &lt;?php
    $str = "<div id=\"faq-questions\">\n";
    $str .= "<ul>\n";
    $i = 1;
    foreach ($faqs as $faq) {
        $str .= "<li><a >" . $faq['title'] . "</a></li>\n";
    }
    $str .= "</ul>\n";
    $str .= "</div>\n";

    $str .= "<div id=\"faq-answers\">\n";
    $i = 1;
    foreach ($faqs as $faq) {
        $str .= "<div class=\"faq-answer\">\n";
        $str .= "<h4><a >" . $faq['title'] . "</a></h4>\n";
        $str .= $faq['body'] . "\n";
        $str .= "<a >[top]</a>";
        $str .= "</div>\n";
    }
    $str .= "</div>\n";
    
    echo $str;
    ?&gt;
</div>

The result is a typical FAQ display with a list of questions, followed by the answers. Included also is the 'back to top' link. For my application, I placed the &lt;a name="top"&gt;&lt;/a&gt; code under my 'wrapper' div.

Again, nothing non-trivial here. Hope it is useful.




Theme © iAndrew 2016 - Forum software by © MyBB