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

发布于 2024-11-03 10:47:48 字数 3370 浏览 0 评论 0原文

我正在努力处理通过 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 -->

I am struggling with nested tab markup generated via Codeigniter model calls and would welcome any insightful comments. The difficulty is that the markup generated has unwanted repetitions of blocks of project data. The use of multiple views which are not straightforwardly inter-connected is probably where the problems lie.

Here's the controller:

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

The model:

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

    }

Here are the views which are in two parts. I suspect this is where it's all going wrong. There's an imperfect join between the two views which I am having a hard time thinking about.

view 1 called '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>

And the second view 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 -->

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

雨的味道风的声音 2024-11-10 10:47:48
foreach ( $data['cats'] as $item )
    {
    $data2['projects']=$this->msm_projects->catproj($item['catid'])->result_array();
    $this->load->view('vup_projects2', $data2);
    }

是你的问题,你正在循环 $data['cats'] 中每个项目的视图...你还对每个 $item 进行查询。您应该执行一个查询,然后相应地循环遍历结果。但要解决您的问题,请尝试以下操作:

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

is your problem, you're looping the views for every item in $data['cats']... you're also doing a query for every $item. You should do one query and then loop through the results accordingly. But to fix your problem, try this:

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

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

then change your second view to

<?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 -->
昨迟人 2024-11-10 10:47:48

因此,我仔细研究了您想要完成的任务,我认为我将其合并为单个查询和单个视图。也许这会有所帮助

Controller:

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

Model:

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

View:

<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 循环中所做的操作来获取主要类别,然后将页面从结果数组中拉出,这些页面 == 到您正在查找的类别。

So I took a closer look at what your trying to accomplish and I think I consolidated it into a single query and a single view. Maybe this will help

Controller:

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

Model:

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

View:

<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 } ?>

There are a lot of tricks to learn about optimizing the MVC process in CI, a lot of which I wish I learned earlier. Hopefully this helps some, let me know if you have questions. As for the pagination, try doing something similar to what i did in the foreach loop to get the main categories, and then subsequently pulling the pages out of the result array which are == to the category you're looking for.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文