变量没有被传递
我需要做的是让 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>";
}
?>
What I need to happen is have the top5 variable array get used for my index page. As of right now it says its an undefined variable. So with the if statement that assigns the page variable it needs to know to send the top5 variable array with it IF its the index page its going to load. When I do a print_r of the top5 variable in the controller it shows up like this:
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 ) )
Controller:
$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');
EDIT :
Here's my template system.
/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)
Content View:
<div id="content">
<?php
echo $page;
?>
</div>
Index Page View:
<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>
EDIT 2 :
Controller:
$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');
View:
<?php
for($i=0;$i < count($toprankings); $i++)
{
echo "character_name: ".$toprankings[$i]['character_name']."<br>";
}
?>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的
for
循环是向后的。应该是
$i
从0
开始,因此永远不会超过count
,即使它相等也不会递增或迭代。尝试删除视图中除 top5 部分之外的所有内容,并将其更改为:
只是为了看看它是否正常工作。此外,了解您正在使用的模板系统也会有所帮助。
Your
for
loop is backwards.should be
$i
starts at0
and therefore will never be more thancount
and even if it is equal will never increment nor iterate.Try deleting everything in your view except the top5 part, and change that to:
just to see if it's working. Also, it'd help to know what templating system you are using.