问题描述
我一直在调试一个PHP的网站,实现在页面标题navsection的顶部和侧面导航栏标题sidenavcat导航栏。在运行时我navsection显示的是:
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未定义的变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!