英文:
Yii2 - Undefined variable in View file
问题
在siteController中,我试图通过siteController将变量传递到查看页面,但是每次尝试时都会出现未定义变量错误。
以下是siteController中的代码:
public function actionIndex(){
$Opere = Opere::find()->all();
return $this->render('index', [
'titolo' => $titolo, 'tecnica' => $tecnica, 'misura' => $misura, 'anno' => $anno,
'prezzo' => $prezzo, 'note' => $note, 'url_grandi' => $url_grandi
]);
}
以下是index页面上的代码:
<?php foreach ($Opere as $o): ?>
<div class='grid-item'>
<img src='<?php $url_grandi;?>' class='grid-item-img'/>
<div class='grid-item-overlay'>
<div class='text'>
<div class='innerText'>
Titolo: <?php $titolo;?><br />
Tecnica: <?php $tecnica;?><br />
Misura: <?php $misura;?><br />
Anno: <?php $anno;?><br />
Prezzo: € <?php $prezzo;?><br />
Note: <?php $note;?>
</div>
</div>
</div>
</div>
<?php endforeach; ?>
提前感谢您。
英文:
I am trying to pass the variable to view the page through the siteController, however when I try, I always get the error undefined Variable.
Here is my code in the siteController:
public function actionIndex(){
$Opere = Opere::find()->all();
return $this->render('index', [
'titolo' => $titolo, 'tecnica' => $tecnica, 'misura' => $misura, 'anno' => $anno,
'prezzo' => $prezzo, 'note' => $note, 'url_grandi' => $url_grandi]);
}
and here below is the code on the index page:
<?php foreach ($Opere as $o): //var_dump($o); ?>
<div class='grid-item'>
<img src='<?php $url_grandi;?>' class='grid-item-img'/>
<div class='grid-item-overlay'>
<div class='text'>
<div class='innerText'>
Titolo: <?php $titolo;?><br />
Tecnica: <?php $tecnica;?><br />
Misura: <?php $misura;?><br />
Anno: <?php $anno;?><br />
Prezzo: &euro; <?php $prezzo;?><br />
Note: <?php $note;?>
</div>
</div>
</div>
</div>
<?php endforeach; ?>
Thank you in advance
答案1
得分: 0
我找到了解决方案,我在我的foreach循环中添加了错误的变量。我试图调用变量$titolo,然而,在我的foreach循环中,我已经指定将变量$titolo作为$o。这就是为什么它没有识别变量$titolo。
以下是正确的代码字符串以输出数据库数据:
= $o->titolo;?>
而不是:
希望这能帮助其他遇到类似问题的人。
英文:
I found the solution, I added the incorrect variable in my foreach loop. I was trying to call the variable $titolo, however, in my foreach loop I had specified that the variable $titolo as $o. This is why it did not recognize the variable $titolo.
The following below is the correct code string to output the db data:
<?= $o->titolo;?>
and not:
<?php echo $titolo;?>
hope this helps someone else with a similar problem.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论