上的index.php未定义的变量 [英] Undefined variable on index.php

查看:244
本文介绍了上的index.php未定义的变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在调试一个PHP的网站,实现在页面标题navsection的顶部和侧面导航栏标题sidenavcat导航栏。在运行时我navsection显示的是:


 注意:未定义的变量:navsection在
/Applications/XAMPP/xamppfiles/htdocs/NOALivingWebsite/header.php在线25上
招待费
注意:未定义的变量:navsection在/Applications/XAMPP/xamppfiles/htdocs/NOALivingWebsite/header.php第28行
可持续发展
注意:未定义的变量:navsection在/Applications/XAMPP/xamppfiles/htdocs/NOALivingWebsite/header.php在线31
活动
注意:未定义的变量:navsection在/Applications/XAMPP/xamppfiles/htdocs/NOALivingWebsite/header.php在线34
preSS
注意:未定义的变量:navsection在/Applications/XAMPP/xamppfiles/htdocs/NOALivingWebsite/header.php在线37
产品目录
注意:未定义的变量:navsection在/Applications/XAMPP/xamppfiles/htdocs/NOALivingWebsite/header.php第40行
位置
注意:未定义的变量:navsection在/Applications/XAMPP/xamppfiles/htdocs/NOALivingWebsite/header.php在线43
网上购物


在header.php中以下相应的线如下

 <立LT; PHP的echo($ navsection =='款待')? '类=当前':''; ?> >
   < A HREF =HTTP://localhost/NOALivingWebsite/$c$c/hospitality.php>酒店及LT; / A>
< /李>
<立LT; PHP的echo($ navsection =='可持续性')? '类=当前':''; ?> >
   &所述; A HREF =HTTP://localhost/NOALivingWebsite/$c$c/sustainability.php>可持续发展及下; / A>
< /李>
<立LT; PHP的echo($ navsection =='事件')? '类=当前':''; ?> >
   &所述; A HREF =HTTP://localhost/NOALivingWebsite/$c$c/events.php>活动与所述; / A>
< /李>
<立LT; PHP的echo($ navsection =='preSS')? '类=当前':''; ?> >
   &所述; A HREF =HTTP://localhost/NOALivingWebsite/$c$c/$p$pss.php> preSS&下; / A>
< /李>
<立LT; PHP的echo($ navsection =='目录')? '类=当前':''; ?> >
   &所述; A HREF =HTTP://localhost/NOALivingWebsite/$c$c/catalogue.php>编目&下; / A>
< /李>
<立LT; PHP的echo($ navsection =='零售')? '类=当前':''; ?> >
   < A HREF =HTTP://localhost/NOALivingWebsite/$c$c/locations.php>地点和LT; / A>
< /李>
<立LT; PHP的echo($ navsection =='登记')? '类=当前':''; ?> >
   < A HREF =HTTP://localhost/NOALivingWebsite/index.php>网上商店< / A>
< /李>

您可以借给任何意见将大大AP preciate。谢谢你的时间


解决方案

该错误消息,而不是直接的。在您的执行三元运营商的时候,变量 $ navsection 没有定义。
一个可能的解决方案是定义一个变量对话变量和访问它像即:

  $ _ SESSION [navsection]

所以你的code看起来就像是:

 <立LT; PHP的echo($ _SESSION [navsection] =='注册')? '类=当前':''; ?> >
   < A HREF =HTTP://localhost/NOALivingWebsite/index.php>网上商店< / A>
< /李>

注意:确保你的会议变量 navsection 前的上述定义检查,可能在另一个的.php 文件。

您将需要使用此内容之一,也是在你的HTML文件。这应该在你的HTML文件一样,顶部用:

 < PHP
  在session_start();
  $ _SESSION [navsection] =注册; //临时的解决办法。
?>
< HTML和GT;



所以,你可以访问你的会话变量。

I have been working on debugging a php site that implements a nav bar at the top of the page title navsection and a side nav bar titled sidenavcat. Upon run-time my navsection is displaying:

Notice: Undefined variable: navsection in 
/Applications/XAMPP/xamppfiles/htdocs/NOALivingWebsite/header.php on line 25
Hospitality 
Notice: Undefined variable: navsection in /Applications/XAMPP/xamppfiles/htdocs/NOALivingWebsite/header.php on line 28
Sustainability 
Notice: Undefined variable: navsection in /Applications/XAMPP/xamppfiles/htdocs/NOALivingWebsite/header.php on line 31
Events 
Notice: Undefined variable: navsection in /Applications/XAMPP/xamppfiles/htdocs/NOALivingWebsite/header.php on line 34
Press 
Notice: Undefined variable: navsection in /Applications/XAMPP/xamppfiles/htdocs/NOALivingWebsite/header.php on line 37
Catalogues 
Notice: Undefined variable: navsection in /Applications/XAMPP/xamppfiles/htdocs/NOALivingWebsite/header.php on line 40
Locations 
Notice: Undefined variable: navsection in /Applications/XAMPP/xamppfiles/htdocs/NOALivingWebsite/header.php on line 43
Shop Online

The following corresponding lines in header.php are as follows

<li <?php echo ($navsection == 'hospitality') ? ' class="current"' : ''; ?> >
   <a href="http://localhost/NOALivingWebsite/code/hospitality.php">Hospitality</a>
</li>
<li <?php echo ($navsection == 'sustainability') ? ' class="current"' : ''; ?> >
   <a href="http://localhost/NOALivingWebsite/code/sustainability.php">Sustainability</a>
</li>
<li <?php echo ($navsection == 'events') ? ' class="current"' : ''; ?> >
   <a href="http://localhost/NOALivingWebsite/code/events.php">Events</a>
</li>
<li <?php echo ($navsection == 'press') ? ' class="current"' : ''; ?> >
   <a href="http://localhost/NOALivingWebsite/code/press.php">Press</a>
</li>
<li <?php echo ($navsection == 'catalogue') ? ' class="current"' : ''; ?> >
   <a href="http://localhost/NOALivingWebsite/code/catalogue.php">Catalogues</a>
</li>
<li <?php echo ($navsection == 'retail') ? ' class="current"' : ''; ?> >
   <a href="http://localhost/NOALivingWebsite/code/locations.php">Locations</a>
</li>
<li <?php echo ($navsection == 'register') ? ' class="current"' : ''; ?> >
   <a href="http://localhost/NOALivingWebsite/index.php">Shop Online</a>
</li>

Any advice you may lend will be greatly appreciate. Thank you for your time

解决方案

The error message is rather than straight-forward. At the time where your ternary operator is executed, the variable $navsection is not defined. A possible solution is to define that variable as Session variable and access it like that:

$_SESSION["navsection"]

So your code will look like that:

<li <?php echo ($_SESSION["navsection"] == 'register') ? ' class="current"' : ''; ?> >
   <a href="http://localhost/NOALivingWebsite/index.php">Shop Online</a>
</li>

Note: Make sure that your session variable navsection is defined before the above check, possibly in another .php file.

You will need to use this one as well in your html file. This should be used at the top of your html file like that:

<?php
  session_start( );
  $_SESSION["navsection"] = "register"; // Temporary fix.
?>
<html>
.
.
.

So that you can access your session variable.

这篇关于上的index.php未定义的变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆