通过DB和CI创建动态链接 [英] Creating Dynamic Links Through DB and CI

查看:45
本文介绍了通过DB和CI创建动态链接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试获取结果集并将其发送到视图,以便可以对其进行相应显示,并且在理解此处找到的文档时遇到了问题,我以某种方式拥有了我想要的模板视图像这样的显示:只是给您一个主意。在模板下面,或者不是我的控制器的代码,以及从数据库中获取类别名称的模型。还包括我目前对视图的看法(php语法错误) )。

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>

控制器:

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');
}

型号:

/**
 * 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(); 
} 

视图:

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

编辑:这是我的新代码。我收到以下错误消息:严重性:注意

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

消息:未定义变量:行

文件名: partials / header.php

Filename: partials/header.php

行号:34
遇到PHP错误

Line Number: 34 A PHP Error was encountered

严重性:警告

消息:为foreach()提供的参数无效

Message: Invalid argument supplied for foreach()

文件名:partials / header.php

Filename: partials/header.php

行号:34

控制器:

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');
}

型号:

/**
 * 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();
}   

查看

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


推荐答案

更改的最后一行get_menu_categories()返回$ query-> result_array(); $ row 此处的变量不会影响返回值或执行函数范围之外的任何操作。

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.

假定 $ this-> template-> ; set()将关联数组作为第二个参数,可能是这种情况,将其更改为:

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);

然后,您应该可以访问 $ rows menu_categories 视图中:

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

如注释中所述,使用 $ rows s

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

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

这篇关于通过DB和CI创建动态链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆