![](/img/trans.png)
[英]Passing an array and an object from a Controller to a View in CodeIgniter
[英]Passing Array from Controller to View Using CodeIgniter
我试图将键号为0到9的数组传递给我的视图。 但是,当我尝试回显特定键或所有键的foreach以显示数据时,什么也没有显示。
我的控制器:
$i=0;
$cleanarray = array();
foreach ($phrase as $innerphrase) {
if (is_array($innerphrase)) {
foreach ($innerphrase as $value) {
$cleanarray[$i] = $value;
$i++;
}
}
}
$this->load->view('blogview', $cleanarray);
输出:
Array
(
[0] => On Friday evenings in Thailand, sandwiched between the evening news and a popular soap opera, is a prime-time programme that has been running for three years, or ever since the military took power in a May 22, 2014 coup.
[1] => The Reds pulled off their biggest comeback in a year to finally end their pesky losing streak.
[2] => A co-founder of Twitter says he's sorry if the popular social media platform helped put Donald Trump in the White House, as the president has suggested.
[3] => Daniel Suarez is thrilled to be competing in the NASCAR All-Star race after struggling in his rookie season.
[4] => Lexi Thompson remained in position for her first victory since a rules infraction cost her a major title, shooting a 2-under 69 in tricky wind conditions Saturday to take a three-stroke lead over In Gee Chun into the final round of the Kingsmill Championship.
[5] => Boston Celtics guard Isaiah Thomas will miss the remainder of the NBA playoffs after aggravating a hip injury during Game Two of the Eastern Conference final, the team announced on Saturday.
[6] => Yankees manager Joe Girardi has been ejected during an animated argument and New York pitching coach Larry Rothschild and Tampa Bay starter Matt Andriese also have been tossed in a testy game at Tropicana Field.
[7] => Ed Carpenter turned a tough draw into a winning hand Saturday.
[8] => Cloud Computing pulled off a major surprise in the Preakness Stakes in Maryland on Saturday, charging down the stretch to overtake Classic Empire and win the second race of U.S. thoroughbred racing's Triple Crown.
[9] => Sometimes it pays to have a fresh horse.
)
我的观点:
<html>
<head>
</head>
<body>
<h1>I am terrible at PHP!</h1>
<h2><?php echo $0; ?></h2>
</body>
</html>
但是什么也没显示。 我也尝试过以相同的方式使用print_r。 我知道CodeIgniter不允许我在控制器中传递变量,而是允许我直接访问数组中的键。
最终,我想遍历数组并显示所有结果,但是我试图从“ small”开始,至少要使数组的单个元素传递到视图。 如果有人可以解释为什么它不起作用,然后解释使用for或foreach遍历数组的方式,我将不胜感激。
我知道这个问题可能已经回答了很多次,但是对于我一生来说,我无法将对这些问题的回答外推到适用于我的情况的解决方案中。
使用此代码:
控制器 :
$i=0;
$cleanarray = array();
foreach ($phrase as $innerphrase) {
if (is_array($innerphrase)) {
foreach ($innerphrase as $value) {
$cleanarray[$i] = $value;
$i++;
}
}
}
$array2 = array(); // create a new array
$array2['contents']= $cleanarray; // add $cleanarray to the new array
$this->load->view('blogview', $array2); // pass the new array as the parameter
内部视图文件 :
echo(“ Contents:”);
//打印$ cleanarray中的元素
foreach($ contents为$ val)
echo "$val<br/>";
注意 :我们在这里不能访问$ array2。 而是使用'$ contents []'。
据我所知,codeigniter不会在没有数组的情况下将数据从控制器传递给视图,因此您可以尝试以下代码,我必须确保它能正常工作。
在您的控制器中
$i=0;
$cleanarray = array();
foreach ($phrase as $innerphrase)
{
if (is_array($innerphrase))
{
foreach ($innerphrase as $value)
{
$cleanarray[$i] = $value;
$i++;
}
}
}
$data['cleanarray'] = $cleanarray
$this->load->view('blogview', $data);
在您的视图中,将数组打印如下: print_r($cleanarray);
我确信此代码将起作用。
快乐编码:)
那不是访问数组变量的方式。 $
不代表数组,它代表变量名的开头。 访问$cleanarray
的第一个元素的$cleanarray
是$cleanarray[0]
。
我承认我不是一个代码点火器编码器,所以我什至不知道那是否是您在“视图”中访问它的方式。 我猜不是,因为控制器中有$this
业务。 当然,其他人可以提供该细节。
在控制器中创建$ cleanarray后,应用此
$data['cleanarray'] = $cleanarray;//assign $cleanarray to $data
$this->load->view('blogview', $data);//pass $data to your view
并在视图中访问您的数组
<h2><?php echo $cleanarray[0]; ?></h2>
这是我的建议:
我不建议您使用数字作为变量名。 为了解决这个问题,您可以将其名称以“ _”开头。 例如: $_1
。
因此,在循环中更改$cleanarray[$i] = $value;
进入$cleanarray['_'.$i] = $value;
。
当然,您需要在视图echo $_0;
上调用它echo $_0;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.