Question

Let me make it nice and simple:

as probably everybody else, I am using simple includes on the server for various things such as top menu or a side menu and so on...

Here is my example of code

<?php $number = 1; include("/var/www/vhosts/MyWebSite.com/httpdocs/trial/includes/header.php");?>

the $number =1 defines the correct selection of the tab depending on the page level as usual.

the code that I am including is as follows:

18 <?php $page[$number] = 'selected';?>
19 <!--=================== top navigation ==================-->
20 <nav>
21 <div id="menu">
22 <a href="/Page 1/" class="tab <?php echo $page[1] ?>" title="Page 1">Page 1</a>
23 <a href="/Page 2/" class="tab <?php echo $page[2] ?>" title="Page 2">Page 2</a>
24 <a href="/Page 3/" class="tab <?php echo $page[3] ?>" title="Page 3">Page 3</a>
25 <a href="/Page 4/" class="tab <?php echo $page[4] ?>" title="Page 4">Page 4</a>
26 <a href="/Page 5/" class="tab <?php echo $page[5] ?>" title="Page 5">Page 5</a>
27 <a href="/Page 6/" class="tab <?php echo $page[6] ?>" title="Page 6">Page 6</a>
28 <a href="/Page 3/" class="tab <?php echo $page[7] ?>" title="Page 7">Page 7</a>
29 </div><!-- end .menu -->
30 </nav><!-- end .navigation -->

clearly the css for .selected will change the colour of the background to show that you are on the that particular page so there is no need to explain it.

now the problem, I have just checked my error logs that happened to be pretty large to download and discovered the following errors:

[Sat Nov 24 17:50:50 2012] [error] [client 204.11.33.59] PHP Notice:  Undefined offset:  2 in /var/www/vhosts/MyWebSite.com/httpdocs/trial/includes/header.php on line 23
[Sat Nov 24 17:50:50 2012] [error] [client 204.11.33.59] PHP Notice:  Undefined offset:  3 in /var/www/vhosts/MyWebSite.com/httpdocs/trial/includes/header.php on line 24
[Sat Nov 24 17:50:50 2012] [error] [client 204.11.33.59] PHP Notice:  Undefined offset:  4 in /var/www/vhosts/MyWebSite.com/httpdocs/trial/includes/header.php on line 25
[Sat Nov 24 17:50:50 2012] [error] [client 204.11.33.59] PHP Notice:  Undefined offset:  5 in /var/www/vhosts/MyWebSite.com/httpdocs/trial/includes/header.php on line 26
[Sat Nov 24 17:50:50 2012] [error] [client 204.11.33.59] PHP Notice:  Undefined offset:  6 in /var/www/vhosts/MyWebSite.com/httpdocs/trial/includes/header.php on line 27
[Sat Nov 24 17:50:50 2012] [error] [client 204.11.33.59] PHP Notice:  Undefined offset:  7 in /var/www/vhosts/MyWebSite.com/httpdocs/trial/includes/header.php on line 28

I can not understand the reason for it. IMO if the $number is already predefined than why error?

Please help me to do it the right way.

Was it helpful?

Solution

The index of $page is not defined. You could change your code like this:

echo isset($page[1]) ? $page[1] : ""
//and so on

OTHER TIPS

You set just one element of your array but check more. By doing this:

$a = 3;
$page[$a] = 'selected';

only one entry exists with index 3 (unless you set it elsewhere but I assume you do not). So this code:

<?php echo $page[1]; ?>

refers to non existing array element, hence the warning. If the only purpose of $page is this from your code then get rid of it completely. Remove

<?php $page[$number] = 'selected';?>

and replace

<?php echo $page[7] ?>

with

If you need this array for any other reason, replace code

<?php echo $page[7] ?>

with

<?php echo isset($page[7]) ? 'selected' : '' ?>

Use this clean code inside your div#menu:

<?php 
$count = 7;
for($link = 1; $link <= $count; $link++){
    $extraClass = ($link == $number) ? "selected" : "" ;
    echo "<a href='/Page {$link}/' class='tab {$extraClass}' 
               title='Page {$link}'>Page {$link}</a>";
}
?>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top