Yii2 – 在视图文件中未定义的变量

huangapple go评论61阅读模式
英文:

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: &euro; <?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()-&gt;all();
        return $this-&gt;render(&#39;index&#39;, [
            &#39;titolo&#39; =&gt; $titolo, &#39;tecnica&#39; =&gt; $tecnica, &#39;misura&#39; =&gt; $misura, &#39;anno&#39; =&gt; $anno,
            &#39;prezzo&#39; =&gt; $prezzo, &#39;note&#39; =&gt; $note, &#39;url_grandi&#39; =&gt; $url_grandi]);
    }

and here below is the code on the index page:

&lt;?php foreach ($Opere as $o): //var_dump($o); ?&gt;
          &lt;div class=&#39;grid-item&#39;&gt;
              &lt;img src=&#39;&lt;?php $url_grandi;?&gt;&#39; class=&#39;grid-item-img&#39;/&gt;
              &lt;div class=&#39;grid-item-overlay&#39;&gt;
                &lt;div class=&#39;text&#39;&gt;
                  &lt;div class=&#39;innerText&#39;&gt;
                    Titolo:  &lt;?php $titolo;?&gt;&lt;br /&gt;
                    Tecnica:  &lt;?php $tecnica;?&gt;&lt;br /&gt;
                    Misura:   &lt;?php $misura;?&gt;&lt;br /&gt;
                    Anno:  &lt;?php  $anno;?&gt;&lt;br /&gt;
                    Prezzo: &amp;euro;  &lt;?php $prezzo;?&gt;&lt;br /&gt;
                    Note: &lt;?php $note;?&gt;
                  &lt;/div&gt;
                &lt;/div&gt;
              &lt;/div&gt;
            &lt;/div&gt;
&lt;?php endforeach; ?&gt;

Thank you in advance

答案1

得分: 0

我找到了解决方案,我在我的foreach循环中添加了错误的变量。我试图调用变量$titolo,然而,在我的foreach循环中,我已经指定将变量$titolo作为$o。这就是为什么它没有识别变量$titolo。

以下是正确的代码字符串以输出数据库数据:

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:

&lt;?= $o-&gt;titolo;?&gt;

and not:

&lt;?php echo $titolo;?&gt;

hope this helps someone else with a similar problem.

huangapple
  • 本文由 发表于 2020年1月4日 00:38:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/59582145.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定