Solved

PHP Notice: Undefined Offset Erro

Posted on 2011-02-12
6
584 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
[X]
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
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 110

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
WordPress Tutorial 4: Recommended Plugins

Now that you have WordPress installed, understand the interface, and know how to install new parts, let’s take a look at our recommended plugins.

 
LVL 110

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 110

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

Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

Question has a verified solution.

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

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…
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

623 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