Solved

PHP Notice: Undefined Offset Erro

Posted on 2011-02-12
6
582 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering 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

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn how to dynamically set the form action using jQuery.
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…

756 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