Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

PHP Notice: Undefined Offset Erro

Posted on 2011-02-12
6
581 Views
Last Modified: 2012-05-11
Here is my error:

PHP Notice: Undefined offset: 4 in /var/www/vhosts/httpdocs/layout/sort-menu.php on line 6

sort-menu.php is a included file in another php page.  The Parent page includes it this way:

<?php $current = 1; include("../../layout/sort-menu-following.php"); ?>

In the file i have this code:

<?php $active[$current] = "class=menu-sort-active";?>
            <ul>
            	<li <?php echo $active[1] ?> id="menu-sort-1"><a href="">menu 1</a></li>
            	<li <?php echo $active[3] ?> id="menu-sort-2"><a href="">menu 2</a></li>
                <li <?php echo $active[4] ?> id="menu-sort-1"><a href="">Menu 3</a></li>
                <li <?php echo $active[5] ?> id="menu-sort-4"><a href="">menu 4</a></li>
                <li <?php echo $active[6] ?> id="menu-sort-5"><a href="">menu 5</a></li>
            </ul>

Open in new window



any reason why i im getting an error?

0
Comment
Question by:jporter80
6 Comments
 
LVL 83

Expert Comment

by:Dave Baldwin
ID: 34880999
The error means that $active[4] doesn't exist.  The '4' is the offset it's referring to.  Check the code to see what it's supposed to be and make sure 5 and 6 are there also so they don't cause errors.
0
 

Expert Comment

by:tkmluv
ID: 34881300
If they may be un-defined, you can use an if statement to only run it if set.

<?php if (isset($active[4])){ echo $active[4];} ?>

Open in new window

0
 
LVL 109

Expert Comment

by:Ray Paseur
ID: 34882810
This is not an error, it is a Notice - these are very different.  In the default installation of PHP, the Notice messages are suppressed.  This message suppression allows you to use undefined variables and undefined array indexes as if the value was NULL.  

You can use error reporting(E_ALL ^ E_NOTICE) to suppress the messages.

You can also test the variables to see if they are defined, or you can pre-populate the array with empty strings, etc.  

If you expected the array to contain data in $active[5] (which is what is shown on line 6 of the code snippet) then you might want to use var_dump() to print out the array and see what is there and what is missing.
0
Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 109

Expert Comment

by:Ray Paseur
ID: 34882839
An example that illustrates what you've found in the code above.  Install this and run it.  Pay particular attention to what happens on line 18 and 34.
<?php // RAY_temp_jporter80.php
echo "<pre>";



// SET DEFAULT ERROR REPORTING LEVELS
error_reporting(E_ALL ^ E_NOTICE);

// CREATE AND VISUALIZE AN ARRAY
$thing = array('A', 'B', 'C', 'D', 'E');
var_dump($thing);

// REMOVE AN ELEMENT
unset($thing[3]);
var_dump($thing);

// TRY TO USE THE REMOVED ELEMENT
$x = $thing[3];



// SET FULL ERROR REPORTING LEVEL
error_reporting(E_ALL);

// CREATE AND VISUALIZE AN ARRAY
$thing = array('A', 'B', 'C', 'D', 'E');
var_dump($thing);

// REMOVE AN ELEMENT
unset($thing[3]);
var_dump($thing);

// TRY TO USE THE REMOVED ELEMENT
$x = $thing[3];

Open in new window

0
 
LVL 5

Accepted Solution

by:
onemadeye earned 500 total points
ID: 34909243
Or perhaps use this to replace <ul>...</ul>
<ul>
<li <?php echo ( isset($active[1]) ? $active[1] : '' ); ?> id="menu-sort-1"><a href="">menu 1</a></li>
<li <?php echo ( isset($active[3]) ? $active[3] : '' ); ?> id="menu-sort-2"><a href="">menu 2</a></li>
<li <?php echo ( isset($active[4]) ? $active[4] : '' ); ?> id="menu-sort-1"><a href="">menu 3</a></li>
<li <?php echo ( isset($active[5]) ? $active[5] : '' ); ?> id="menu-sort-4"><a href="">menu 4</a></li>
<li <?php echo ( isset($active[6]) ? $active[6] : '' ); ?> id="menu-sort-5"><a href="">menu 5</a></li>
</ul>

Open in new window

0
 
LVL 109

Expert Comment

by:Ray Paseur
ID: 34929603
You might also want to check the numbers that are used to index the $active array, that are used in the id fields and that are displayed in the list items.  You might have what you want in those fields, but the collection looks kind of counterintuitive.
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
How is this connection happening? 3 20
Currency in SQL? 2 29
Wordpress Security 29 46
Force PDF to open inline as opposed to dowload 16 21
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
This article discusses four methods for overlaying images in a container on a web page
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.

792 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question