Solved

PHP Notice: Undefined Offset Erro

Posted on 2011-02-12
6
577 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 82

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 108

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
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 108

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 108

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

747 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

Need Help in Real-Time?

Connect with top rated Experts

7 Experts available now in Live!

Get 1:1 Help Now