开发者

Codeigniter multiple views for nested tab setup

开发者 https://www.devze.com 2023-02-28 16:35 出处:网络
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 o

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="c开发者_C百科ontainer">

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


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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号