Codeigniter多个视图用于嵌套选项卡设置

时间:2011-04-26 17:02:41

标签: codeigniter tabs

我正在努力使用通过Codeigniter模型调用生成的嵌套选项卡标记,并欢迎任何有见地的评论。困难在于生成的标记具有不必要的项目数据块重复。使用多个不直接相互连接的视图可能就是问题所在。

这是控制器:

function projects() {

$this->load->model('msm_projects');
$data['cats']=$this->msm_projects->catid()->result_array();
$this->load->view('vup_projects', $data);

foreach ( $data['cats'] as $item )
    {
    $data2['projects']=$this->msm_projects->catproj($item['catid'])->result_array();
    $this->load->view('vup_projects2', $data2);
    }   
}

模特:

function catid() {

    return $this->db->query("SELECT DISTINCT catid, cat FROM category INNER JOIN projects ON catid = projcat WHERE projpub=1 ORDER BY catid ASC");

    }

function catproj($catid) {

  return $this->db->query("SELECT catid, cat, projcat, projid, projtit  FROM projects INNER JOIN category ON projcat = catid WHERE projcat = $catid AND projpub=1 ORDER BY catid ASC");

    }

以下是两部分的观点。我怀疑这是一切都出错的地方。两个观点之间存在不完美的联系,我很难思考。

视图1名为'vup_projects'

<div id="wrapper">
<div class="yui3-g">

    <div class="yui3-u-1"><div id="topbloc"><img src="http://localhost/getop/base-images/topbloc.gif" width="800" height="50" align="middle"></div></div>

<div class="yui3-u-1">

    <div id="navbloc">
       <div id="linx">
           <ul >
               <li id="about"><?php echo anchor('cu_tya/about', 'about'); ?></li>
               <li id="ourwork"><?php echo anchor('cu_projects/projects', 'projects'); ?></li>
               <li id="contact"><?php echo anchor('cu_tya/contact', 'contact'); ?></li>
               <li id="member"><?php echo anchor('cu_sites/pager', 'your page'); ?></li>
           </ul>  
       </div>
    </div>

</div>

    <div class="yui3-u-1">

    <div id="container">

    <ul>            
        <?php 
            foreach ( $cats as $item )  // top tabs 
                {
                    echo '<li><a href=#fragment-'.$item['catid'].'><span>'.$item['cat'].'</span></a></li>';   
                }
        ?>
    </ul>

第二个视图vup_projects2

    <?php foreach ( $cats as $item ) { ?> <!-- main divs -->                    

    <div id="fragment-<?php echo $item['catid'];?>">

    <ul>

        <?php foreach ( $projects as $project )

            {   ?>

                <li>
                <a href="#fragment-<? echo $project['projid']?>a"><span><?php echo $project['projtit'];?></span></a></li>

        <?php   }   ?>

    </ul>

        <?php foreach ( $projects as $project ) 

            { ?> 

                <div id="fragment-<?php echo $project['projid'];?>a" > 

                <?php echo $project['projtit'].'  hooray';?>

                </div>

        <?php   }   ?>

    </div>  

<?php   }   ?>

            </div>  <!-- container -->

        </div>  <!--  YUI-UNIT-1-->

    </div>  <!-- YUI-GRID -->

 </div> <!-- wrapper -->

2 个答案:

答案 0 :(得分:1)

foreach ( $data['cats'] as $item )
    {
    $data2['projects']=$this->msm_projects->catproj($item['catid'])->result_array();
    $this->load->view('vup_projects2', $data2);
    }

是你的问题,你正在为$ data ['cats']中的每个项目循环查看...你也在为每个$项目进行查询。您应该执行一个查询,然后相应地循环结果。但要解决您的问题,请尝试以下方法:

foreach ( $data['cats'] as $item )
{
    $data2['projects'][$item['catid']] = $this->msm_projects->catproj($item['catid'])
                                         ->result_array();
}

$this->load->view('vup_projects2', $data2);

然后将您的第二个视图更改为

<?php foreach ( $cats as $item ) { ?> <!-- main divs -->                    

<div id="fragment-<?php echo $item['catid'];?>">

<ul>

        <?php foreach ( $projects[$item['catid']] as $project )

            {   ?>

                <li>
                <a href="#fragment-<? echo $project['projid']?>a"><span><?php echo $project['projtit'];?></span></a></li>

        <?php   }   ?>

    </ul>

        <?php foreach ( $projects[$item['cat_id']] as $project ) 

            { ?> 

                <div id="fragment-<?php echo $project['projid'];?>a" > 

                <?php echo $project['projtit'].'  hooray';?>

                </div>  

        <?php   }   ?>

            </div>  <!-- container -->

        </div>  <!--  YUI-UNIT-1-->

    </div>  <!-- YUI-GRID -->

 </div> <!-- wrapper -->

答案 1 :(得分:1)

所以我仔细研究了你想要完成的事情,我认为我将它整合到一个查询和一个视图中。也许这会有所帮助

控制器:

class Projects extends CI_Controller {

function __construct(){
    parent::__construct();
}

function projects() {

    $cats = array(); // Unique cateogires
    $projects = array(); // Projects that will go underneath

    $this->load->model('msm_projects');
    $query = $this->msm_projects->get_all();

    // Make sure there are results
    if ($query !== FALSE)
    {   
        // Loop through once to find distinct categories for tabs
        foreach ($query as $k => $v)
        {
            if ( ! array_key_exists($v['catid'], $tabs))
            {
                $tabs[$v['catid']] = $v['cat'];
            }
        }

        // Loop through all of the results and group the items
        // into arrays by their categories
        foreach ($query as $k => $v)
        {
            $projects[$v['catid']][] = $v;
        }

        $data = array(
                    'cats' => $cats,
                    'projects' => $projects
                    );

        $this->load->view('main', $data);
    }
    else
    {
        echo 'No results';
    }
}
}

型号:

class Msm_projects extends CI_Model {

/* Returns everything you need */
function get_all() 
{
    $q = $this->db->select('c.catid, c.cat, p.projcat, p.projid, p.projtit')
             ->join('projects as p', 'c.catid = p.projcat', 'INNER')
             ->where('projpub = 1')
             ->order_by('c.catid')
             ->get('category as c');

    if ($q->num_rows > 0)
    {
        return $q->result_array();
    }
    else
    {
        return FALSE;
    }
}
}

查看:

<div id="wrapper">
    <div class="yui3-g">
        <div class="yui3-u-1">
            <div id="topbloc">
            <img src="<?=base_url() /* Good not to hardcode url's if you can avoid it */;?>getop/base-images/topbloc.gif" width="800" height="50" align="middle" />
        </div>
    </div>
    <div class="yui3-u-1">
    <div id="navbloc">
       <div id="linx">
           <ul >
               <li id="about"><?php echo anchor('cu_tya/about', 'about'); ?></li>
               <li id="ourwork"><?php echo anchor('cu_projects/projects', 'projects'); ?></li>
               <li id="contact"><?php echo anchor('cu_tya/contact', 'contact'); ?></li>
               <li id="member"><?php echo anchor('cu_sites/pager', 'your page'); ?></li>
           </ul>  
       </div>
    </div>
</div>
<div class="yui3-u-1">

<div id="container">
<ul>            
    <?php foreach($cats as $id => $cat){ ?>
        <li><a href="#fragment-<?=$id;?>"><span><?=$cat['cat'];?></span></a></li>
    <?php } ?>
</ul>

<?php foreach ($cats as $id => $cat ) { ?> <!-- main divs -->
    <div id="fragment-<?php echo $id;?>">
        <ul>
            <?php foreach($projects[$id] as $project){ ?>
            <li>
                <a href="#fragment-<? echo $project['projid']?>a"><span><?php echo $project['projtit'];?></span></a>
            </li>
            <?php } ?>
        </ul>
        <?php foreach ($projects[$id] as $project) { ?> 
        <div id="fragment-<?php echo $project['projid'];?>a" > 
            <?php echo $project['projtit'].'  hooray';?>
        </div>
        <?php } ?>
    </div>  
<?php } ?>

在CI中优化MVC过程有很多技巧,我希望我之前学到很多。希望这会有所帮助,如果您有疑问,请告诉我。至于分页,尝试做类似于我在foreach循环中做的事情来获取主要类别,然后将页面从结果数组中拉出==到您正在寻找的类别。