变量没有通过

时间:2011-10-19 00:36:58

标签: php codeigniter

我需要做的是将top5变量数组用于我的索引页面。截至目前,它说它是一个未定义的变量。因此,使用if语句分配页面变量时,它需要知道如何发送top5变量数组,如果它将加载它的索引页面。当我在控制器中执行top5变量的print_r时,它会显示如下:

Array ( [0] => Array ( [character_name] => Kid Wonder ) [1] => Array ( [character_name] => Ryu Satoshi ) [2] => Array ( [character_name] => Oriel ) [3] => Array ( [character_name] => "The Ladies Man" Luscious Landon ) [4] => Array ( [character_name] => "The Outlaw" Mike Mayhem ) ) 

控制器:

$siteInfo = $this->site->getSiteTitleAndSlogan();
$activeTemplate = $this->site->getTemplate();
if ($this->site->pageStatus('index', $activeTemplate[0]->id) == 1){
    $page = $this->load->view($activeTemplate[0]->short_name.'/pages/index', '', true); 
} else {
    $page = $this->load->view($activeTemplate[0]->short_name.'/pages/404', '', true); 
}
$footerLinks = $this->site->getFooterNav(); 
$top5 = $this->site->getTop5(); 
print_r($top5);
$this->template
->title($siteInfo[0]->site_name,$siteInfo[0]->site_slogan)
->prepend_metadata('<link rel="stylesheet" type="text/css" href="http://www.kansasoutlawwrestling.com/assets/css/'.$activeTemplate[0]->short_name.'.css" />')
->set('footerLinks', $footerLinks)
->set('page', $page)
->set('top5', $top5)               
->set_partial('header', $activeTemplate[0]->short_name.'/header')
->set_partial('navigation', $activeTemplate[0]->short_name.'/navigation')            
->set_partial('content', $activeTemplate[0]->short_name.'/content')
->set_partial('footer', $activeTemplate[0]->short_name.'/footer')
->build('kow');

编辑:

这是我的模板系统。

/views
/views/kow.php (template file)
/views/v1 (current template version)/
/views/v1/header.php
/views/v1/footer.php
/views/v1/navigation.php
/views/v1/content.php
/views/v1/pages/
/views/v1/pages/index.php(homepage)
/views/v1/pages/404.php(error page)

内容视图:

<div id="content">
    <?php 
    echo $page; 
    ?>
</div>

索引页面视图:

<div id="left">

<div id="spotlight">
    <img id="spotlight" src="assets/images/spotlight.png" alt="Kansas Outlaw Wrestling" />
</div>

<div id="top5">
    <ol>
        <?php 
        for ($i = 0; $i >= count($top5); $i++){
            foreach($top5[$i] as $row)
            {
                $seperator = ($elements == $count) ? '' : '<hr />'; 
                $name = (!isset($row['character_name'])) ? 'TBD' : $row['character_name'];
                $count++;
            ?>
                <li><span class="red"><?php echo $name; ?></span></li>
            <?php
         }
        }
        ?>

    </ol>
</div>

</div>

<div id="middle">
    <div id="topnews">
        <img id="topnewspic" src="assets/images/kelly.png" alt="Top News Pic" valign="right" />
        <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Etiam sit amet mauris et erat luctus faucibus. In hac habitasse platea dictumst. Nunc sollicitudin ultricies nisi vel blandit. Quisque tincidunt mattis lacinia. Maecenas porttitor magna eu velit hendrerit ornare. Donec ultrices porttitor tellus sed venenatis. Integer ipsum lacus, malesuada eget ornare vel, mollis vel metus. Fusce a orci sed dui accumsan luctus et id enim. Aenean ac dui orci. Nam fringilla rutrum libero eget laoreet. Mauris ac sem metus, a ultrices ante. Duis quam metus, rhoncus sed dictum vel, ultricies vel nibh. Pellentesque enim lorem, mollis ut lacinia ac, aliquet id magna. Proin ante sapien, molestie in tincidunt hendrerit, blandit ut dolor. In rhoncus convallis ullamcorper. Integer sed arcu vitae libero dapibus interdum.</p>
        <p>Sed nibh leo, consequat non tempor vitae, fringilla eget augue. Pellentesque libero turpis, lobortis euismod consectetur eu, ultrices non orci. Aliquam erat volutpat. Sed et nunc orci. Aliquam semper tortor in nunc placerat pulvinar. Etiam placerat ornare metus, id malesuada mi venenatis quis. Suspendisse dapibus, metus sollicitudin dictum aliquet, nisi dui tempus felis, a tristique sem est at ligula. Praesent commodo dolor ac ante ornare id cursus tellus luctus. Vivamus pretium metus ut dui hendrerit tincidunt.</p>
    </div>
</div>

<div id="right">

    <div id="upcomingevents">

        <ul>
            <li>Warpath<span class="red">October 31, 2011</span></li>
            <hr />
            <li>Warpath<span class="red">October 31, 2011</span></li>
            <hr />
            <li>Warpath<span class="red">October 31, 2011</span></li>
            <hr />
            <li>Warpath<span class="red">October 31, 2011</span></li>
            <hr />
            <li>Warpath<span class="red">October 31, 2011</span></li>
        </ul> 

    </div>

    <div id="poll">
        <p id="question" class="red">How is it coming along?</p>
    </div>  

</div>

编辑2:

控制器:

$siteInfo = $this->site->getSiteTitleAndSlogan();
        $activeTemplate = $this->site->getTemplate();
        if ($this->site->pageStatus('index', $activeTemplate[0]->id) == 1){
            $page = $this->load->view($activeTemplate[0]->short_name.'/pages/index', '', true);  
        } else {
            $page = $this->load->view($activeTemplate[0]->short_name.'/pages/404', '', true); 
        }
        $footerLinks = $this->site->getFooterNav(); 
        $toprankings = $this->site->getTop5();
        //$top5 = Array ( Array ( "character_name" => "Kid Wonder" ), Array ( "character_name" => "Ryu Satoshi" ), Array ( "character_name" => "Oriel" ), Array ( "character_name" => "\"The Ladies Man\" Luscious Landon" ), Array ( "character_name" => "\"The Outlaw\" Mike Mayhem" ));
        $data['$toprankings']=$toprankings;
        $this->template
        ->title($siteInfo[0]->site_name,$siteInfo[0]->site_slogan)
        ->prepend_metadata('<link rel="stylesheet" type="text/css" href="http://www.kansasoutlawwrestling.com/assets/css/'.$activeTemplate[0]->short_name.'.css" />')
        ->set('footerLinks', $footerLinks)
        ->set('page', $page)
        ->set('toprankings', $toprankings)               
        ->set_partial('header', $activeTemplate[0]->short_name.'/header')
        ->set_partial('navigation', $activeTemplate[0]->short_name.'/navigation')            
        ->set_partial('content', $activeTemplate[0]->short_name.'/content', $data)
        ->set_partial('footer', $activeTemplate[0]->short_name.'/footer')
        ->build('kow');

查看:

<?php

for($i=0;$i < count($toprankings); $i++) 
{
   echo "character_name: ".$toprankings[$i]['character_name']."<br>";

}

?>   

1 个答案:

答案 0 :(得分:1)

您的for循环是向后的。

for ($i = 0; $i >= count($top5); $i++){

应该是

for($i=0;$i<count($top5);$i++)

$i0开始,因此永远不会超过count,即使它相等也不会增加或迭代。


尝试删除视图中除top5部分之外的所有内容,并将其更改为:

<?php 
    for($i=0;$i<count($top5);$i++)
    {
        foreach($top5[$i] as $k=>$v)
        {
        echo $k."   =    ".$v."<BR>";
        }
    }
?>

只是为了看它是否有效。此外,它有助于了解您正在使用的模板系统。