Jeff Davidson
Jeff Davidson

Reputation: 1929

Creating Dynamic Links Through DB and CI

I'm trying to take my result set and send it to the view so that it can be displayed accordingly and I"m having problems understanding the docs found here I have a template view of what I'd like it to somehow display like: That just gives you an idea. Below the template or what not is the code for my controller and the model that gets the category names from the database. Also is included is what I have currently for my view (php syntax is wrong).

http://codeigniter.com/user_guide/database/results.html

<div class="menu">
        <ul class="clear">
            <li class="active"><a href="/">Dashboard</a></li>
            <li><?php echo anchor('cpanel/modules/articles', 'Articles'); ?></li>
            <li><a href="styles.html">Styles</a></li>
            <li><a href="tables.html">Tables</a></li>
            <li><a href="charts.html">Charts</a></li>
            <li><a href="gallery.html">Image Gallery</a></li>
            <li><a href="settings.html">Settings</a></li>
        </ul>
    </div>

Controller:

function index()
{
    $id = $this->tank_auth->get_user_id();
    $data = $this->Dashboard->get_user_info($id);
    $rows = $this->Dashboard->get_menu_categories();
    $this->template->set_layout('cpanel')->enable_parser(false);
    $this->template->set('data', $data);
    $this->template->set('menu_categories', $rows);
    $this->template->set_partial('header', 'partials/header');  
    $this->template->set_partial('sidebar', 'partials/sidebar');  
    $this->template->set_partial('content', 'partials/content');      
    $this->template->set_partial('footer', 'partials/footer');
    $this->template->build('/cpanel/index');
}

Model:

/**
 * Get menu categories
 *
 * @param   none
 * @param   none
 * @return  object
 */
function get_menu_categories()
{
    $this->db->select('*');
    $this->db->from('menu_categories');
    $this->db->where('status_id', '1');
    $this->db->order_by('sort_order', 'desc'); 

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

    return $row = $query->result_array(); 
} 

View:

<div class="menu">
        <ul class="clear">
            <?php
            foreach ($row as $row)
            {
               echo "<li>" anchor('".$row['linkURL']."', '$row['category_name']')"</li>";
            }
            ?>                
        </ul>
    </div>

EDIT: Here is my new code. I"m getting the following error message: Severity: Notice

Message: Undefined variable: rows

Filename: partials/header.php

Line Number: 34 A PHP Error was encountered

Severity: Warning

Message: Invalid argument supplied for foreach()

Filename: partials/header.php

Line Number: 34

Controller:

function index()
{
    $id = $this->tank_auth->get_user_id();
    $data = $this->Dashboard->get_user_info($id);
    $menu_data['rows'] = $this->Dashboard->get_menu_categories();
    $this->template->set_layout('cpanel')->enable_parser(false);
    $this->template->set('data', $data);
    $this->template->set('menu_categories', $menu_data);

    $this->template->set_partial('header', 'partials/header');  
    $this->template->set_partial('sidebar', 'partials/sidebar');  
    $this->template->set_partial('content', 'partials/content');      
    $this->template->set_partial('footer', 'partials/footer');
    $this->template->build('/cpanel/index');
}

Model:

/**
 * Get menu categories
 *
 * @param   none
 * @param   none
 * @return  object
 */
function get_menu_categories()
{
    $this->db->select('*');
    $this->db->from('menu_categories');
    $this->db->where('status_id', '1');
    $this->db->order_by('sort_order', 'desc'); 

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

    return $query->result_array();
}   

View

<div class="menu">
        <ul class="clear">
            <?php
            foreach ($rows as $row)
            {
                echo '<li>'.anchor($row['category_name'], $row['category_name']).'</li>';
            }
            ?>                
        </ul>
    </div>

Upvotes: 0

Views: 569

Answers (1)

No Results Found
No Results Found

Reputation: 102745

Change the last line of get_menu_categories() to return $query->result_array();, the $row variable here does not affect the return value or do anything outside the scope of the function.

Assuming that $this->template->set() takes an associative array as the second parameter, which is likely the case, change it to:

$menu_data['rows'] = $this->Dashboard->get_menu_categories();
//          ^^^^ This is your variable name to be used in the view
$this->template->set('menu_categories', $menu_data);

Then you should be able to access $rows from the menu_categories view:

As noted in the comments, use $rows with an s:

foreach ($rows as $row)
{
   echo '<li>'.anchor($row['linkURL'], $row['category_name']).'</li>';
}

Upvotes: 2

Related Questions