473,473 Members | 2,151 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

php undefined offset error

5 New Member
Hello

I have a script which have the facility of entering any code to some part of a webpage. I have some problems with it. When i put some code in the script then their is no error shown. When i try to not use a code and leave it empty the following php errors happens. I dont understand why. Help me.

Error shown are: -

[phpBB Debug] PHP Notice: in file /index.php on line 132: Undefined offset: 4
[phpBB Debug] PHP Notice: in file /index.php on line 141: Undefined offset: 4
[phpBB Debug] PHP Notice: in file /index.php on line 142: Undefined offset: 5
[phpBB Debug] PHP Notice: in file /includes/functions.php on line 4250: Cannot modify header information - headers already sent by (output started at /includes/functions.php:3730)
[phpBB Debug] PHP Notice: in file /includes/functions.php on line 4252: Cannot modify header information - headers already sent by (output started at /includes/functions.php:3730)
[phpBB Debug] PHP Notice: in file /includes/functions.php on line 4253: Cannot modify header information - headers already sent by (output started at /includes/functions.php:3730)
[phpBB Debug] PHP Notice: in file /includes/functions.php on line 4254: Cannot modify header information - headers already sent by (output started at /includes/functions.php:3730)


Error script part
Expand|Select|Wrap|Line Numbers
  1. // Start output Ad
  2. $aktiv = ($user->data['user_id'] != ANONYMOUS) ? '1' : '3';
  3.  
  4. $sql = "SELECT code, ID, position FROM " . AD_TABLE ." 
  5.     WHERE (activ = '" .$aktiv. "' OR activ = '1')
  6.     AND  (max_views <= views)
  7.     AND (show_all_forums = '1')
  8.     ORDER BY rand()    ";
  9.  
  10. $result = $db->sql_query($sql);
  11. while($row = $db->sql_fetchrow($result))
  12. {
  13.     $adcode[$row['position']] = html_entity_decode($row['code']);
  14.     $adID[$row['position']]['ID'] = $row['ID'];
  15. }
  16.  
  17. // update views for every Ad
  18. for ($i = 1; $i <= 4; $i++)
  19. {
  20.     if ($adID[$i]['ID'])     // line 132 error line
  21.     {
  22.         $db->sql_query('UPDATE ' . AD_TABLE . ' SET views = views +1 WHERE ID = ' . $adID[$i]['ID']);
  23.     }
  24. }
  25. // End output Ad
  26.  
  27. // Assign index specific vars
  28. $template->assign_vars(array(
  29.       'AD_CODE4'      => $adcode[4],   //line 141 error line
  30.     'AD_CODE5'      => $adcode[5],   //line 142 error line
  31.     'TOTAL_POSTS'    => sprintf($user->lang[$l_total_post_s], $total_posts),
  32.     'TOTAL_TOPICS'    => sprintf($user->lang[$l_total_topic_s], $total_topics),
  33.     'TOTAL_USERS'    => sprintf($user->lang[$l_total_user_s], $total_users),
  34.  
  35.  
Oct 4 '07 #1
9 32483
Atli
5,058 Recognized Expert Expert
Hi. Welcome to The Scripts!

The problem there is that your for loop is counting up to 4. Your array only has 3 elements. So, naturally, PHP prints a Notice telling you about it.

This leads to the header() problem. Headers need to be sent before any output. So when PHP prints the Notice, all headers are sent and no more headers will be accepted. Which leads to the failure of your header() functions and the last bunch of Notices.
Oct 4 '07 #2
simple12
5 New Member
Thanks for the reply.
I am newbie with php. Please you exactly tell me where and what i should change to solve the problem.
Oct 4 '07 #3
Atli
5,058 Recognized Expert Expert
Try changin this:
Expand|Select|Wrap|Line Numbers
  1. // update views for every Ad
  2. for ($i = 1; $i <= 4; $i++)
  3. {
  4.     if ($adID[$i]['ID'])     // line 132 error line
  5.     {
  6.         $db->sql_query('UPDATE ' . AD_TABLE . ' SET views = views +1 WHERE ID = ' . $adID[$i]['ID']);
  7.     }
  8. }
  9.  
Into this:
Expand|Select|Wrap|Line Numbers
  1. // update views for every Ad
  2. foreach($adID as $ad) 
  3. {
  4.     if ($ad['ID'])  
  5.     {
  6.         $db->sql_query('UPDATE ' . AD_TABLE . ' SET views = views +1 WHERE ID = ' . $ad['ID']);
  7.     }
  8. }
  9.  
This way your code won't assume that there are four entries in the $adID array. It will simply go through each element, regardless of how many there are.
Oct 4 '07 #4
simple12
5 New Member
Thanks ,fixed 1st one.

The following problems are still their. Any hint.

[phpBB Debug] PHP Notice: in file /index.php on line 141: Undefined offset: 4
[phpBB Debug] PHP Notice: in file /index.php on line 142: Undefined offset: 5
Oct 4 '07 #5
Atli
5,058 Recognized Expert Expert
These notices are basically the same as the other one.
Your code is assuming that there are a fourth and a fifth element in the $adcode array, but there aren't, thus it prints the notices.
Expand|Select|Wrap|Line Numbers
  1. 'AD_CODE4'      => $adcode[4],   //line 141 error line
  2. 'AD_CODE5'      => $adcode[5],   //line 142 error line'
  3.  
The code is obviously supposed to be fetching these elements from your database, but they don't exists there so the elements are empty.

You could simply ignore them. Your code will execute without them, but it may cause some side-effects.
Oct 4 '07 #6
pbmods
5,821 Recognized Expert Expert
Heya, simple12.

You can prevent these notices by doing this:
Expand|Select|Wrap|Line Numbers
  1. 'AD_CODE4'      => isset($adcode[4]) ? $adcode[4] : 'default value',
  2. 'AD_CODE5'      => isset($adcode[5]) ? $adcode[5] : 'default value',
  3.  
And in PHP 6, you'll be able to shorten it to:
Expand|Select|Wrap|Line Numbers
  1. 'AD_CODE4'      => $adcode[4] ?: 'default value',
  2. 'AD_CODE5'      => $adcode[5] ?: 'default value',
  3.  
Oct 4 '07 #7
simple12
5 New Member
Thanks for the support.

yes i got rid of the notices now. But now if i left empty the code box(place where any extra code is inserted) it is showing value "default" written on webpage.
I want to know that can i put some code to prevent this script from showing error if it founds no data in database.
Oct 5 '07 #8
simple12
5 New Member
Hey guys you really are superb in php.
I fixed with your support all the problems
at last i used only this code
Expand|Select|Wrap|Line Numbers
  1. 'AD_CODE4'      => isset($adcode[4]) ? $adcode[4] : '',
  2. 'AD_CODE5'      => isset($adcode[5]) ? $adcode[5] : '', 
  3.  
Just curious to know if it would work fine or will be unstable.But lol its working.
Oct 5 '07 #9
pbmods
5,821 Recognized Expert Expert
Heya simple12.

The '? :' is called a ternary operator. It's generally accepted as the standard way to assign default values.
Oct 5 '07 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: lawrence | last post by:
I just switched error_reporting to ALL so I could debug my site. I got a huge page full of errors. One of the most common was that in my arrays I'm using undefined offsets and indexes. These still...
2
by: Steven | last post by:
Hi All, I am moving some php code from a Linux machine to a Windows 2000 machine with the code belowe I get the following error : Notice: Undefined offset: 1 in c:\inetpub\wwwroot\test.php on...
3
by: delusion7 | last post by:
I am getting this error "Undefined offset: 9 in C:\Course Technology\1687-5\Chapter.10\UpdateContactInfo.php on line 36" this is the code I am recieving the error on: if...
6
by: nicy12 | last post by:
Hi! my name is Peter. iam working on the php platform. while trying to run and compile a program i get the undefined offset error iam nto much familiar with this error . Please help me . Thanks in...
2
by: neridaj | last post by:
Hello, I'm trying to figure out how to get rid of these errors: Notice: Undefined offset: 1 in output_fns.php on line 315 Notice: getimagesize() : Read error! in output_fns.php on line 315...
6
by: raydiamond4u | last post by:
Hello. My name is Raymond. My code is generating the following error: Notice: Undefined offset: 2 in C:\wamp\www\imagegallery\supportfile\include\config.php on line 25 How do i correct the error?...
1
by: atang | last post by:
Hi, I found the code here, it's excately what i am looking for, ie8 give this error "Notice: Undefined offset: 49 in.." which is this line "if ($num_of_chars==$required)", i'm a newbie, i don't know...
1
by: Mary meer | last post by:
i am trying to switch array value but i get this error message: Undefined offset 2 $at=array( array('Title:','title',2,10), array('First Name:','fname',2,10 ), ...
6
by: tbebest | last post by:
Hi , i have a form and it has A problem: i upload my 3 image and it uploaded in definded path but in insert in db filed there are empty: Notice: Undefined offset: 0 in C:\wamp\www Notice:...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.