Solved

PHP Notice: Undefined Offset Erro

Posted on 2011-02-12
6
579 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
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 
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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Zip Folders Using Chilkat Routines 1 39
Bartender label printing - switch on and off graphics 3 38
Checking CSRF token within a function 36 74
echo paypal data on screen 5 50
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
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.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

776 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