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:3 730)
[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:3 730)
[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:3 730)
[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:3 730)
Error script part -
// Start output Ad
-
$aktiv = ($user->data['user_id'] != ANONYMOUS) ? '1' : '3';
-
-
$sql = "SELECT code, ID, position FROM " . AD_TABLE ."
-
WHERE (activ = '" .$aktiv. "' OR activ = '1')
-
AND (max_views <= views)
-
AND (show_all_forums = '1')
-
ORDER BY rand() ";
-
-
$result = $db->sql_query($sql);
-
while($row = $db->sql_fetchrow($result))
-
{
-
$adcode[$row['position']] = html_entity_decode($row['code']);
-
$adID[$row['position']]['ID'] = $row['ID'];
-
}
-
-
// update views for every Ad
-
for ($i = 1; $i <= 4; $i++)
-
{
-
if ($adID[$i]['ID']) // line 132 error line
-
{
-
$db->sql_query('UPDATE ' . AD_TABLE . ' SET views = views +1 WHERE ID = ' . $adID[$i]['ID']);
-
}
-
}
-
// End output Ad
-
-
// Assign index specific vars
-
$template->assign_vars(array(
-
'AD_CODE4' => $adcode[4], //line 141 error line
-
'AD_CODE5' => $adcode[5], //line 142 error line
-
'TOTAL_POSTS' => sprintf($user->lang[$l_total_post_s], $total_posts),
-
'TOTAL_TOPICS' => sprintf($user->lang[$l_total_topic_s], $total_topics),
-
'TOTAL_USERS' => sprintf($user->lang[$l_total_user_s], $total_users),
-
-
9 32499 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.
Thanks for the reply.
I am newbie with php. Please you exactly tell me where and what i should change to solve the problem.
Atli 5,058
Recognized Expert Expert
Try changin this: -
// update views for every Ad
-
for ($i = 1; $i <= 4; $i++)
-
{
-
if ($adID[$i]['ID']) // line 132 error line
-
{
-
$db->sql_query('UPDATE ' . AD_TABLE . ' SET views = views +1 WHERE ID = ' . $adID[$i]['ID']);
-
}
-
}
-
Into this: -
// update views for every Ad
-
foreach($adID as $ad)
-
{
-
if ($ad['ID'])
-
{
-
$db->sql_query('UPDATE ' . AD_TABLE . ' SET views = views +1 WHERE ID = ' . $ad['ID']);
-
}
-
}
-
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.
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
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. -
'AD_CODE4' => $adcode[4], //line 141 error line
-
'AD_CODE5' => $adcode[5], //line 142 error line'
-
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.
pbmods 5,821
Recognized Expert Expert
Heya, simple12.
You can prevent these notices by doing this: -
'AD_CODE4' => isset($adcode[4]) ? $adcode[4] : 'default value',
-
'AD_CODE5' => isset($adcode[5]) ? $adcode[5] : 'default value',
-
And in PHP 6, you'll be able to shorten it to: -
'AD_CODE4' => $adcode[4] ?: 'default value',
-
'AD_CODE5' => $adcode[5] ?: 'default value',
-
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.
Hey guys you really are superb in php.
I fixed with your support all the problems
at last i used only this code -
'AD_CODE4' => isset($adcode[4]) ? $adcode[4] : '',
-
'AD_CODE5' => isset($adcode[5]) ? $adcode[5] : '',
-
Just curious to know if it would work fine or will be unstable.But lol its working.
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.
Sign in to post your reply or Sign up for a free account.
Similar topics |
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 work fine,
but with error reporting at all they are marked as errors. Why? What
am I doing wrong?
www.monkeyclaus.org
|
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 line 5
1) reset($kolom1);
2) while(list($cat,$lnk) = each($kolom1)){
3) kop($cat);
|
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 (mysqli_num_rows($QueryResult) > 0) {
$Row = mysqli_fetch_row($QueryResult);
$First = stripslashes($Row);
$Last = stripslashes($Row);
$Phone = stripslashes($Row);
$Address = stripslashes($Row);
$City = stripslashes($Row);
$State = stripslashes($Row);
|
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 advance.
Error: Undefined offset: 0 in /es/ePrintsStats/includes/inc.html.cumulative_usage.es.php on line 16
The code:
<?php
$current_year = date("Y");
$last_year = date("Y")-1;
|
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
function get_imgdim()
| |
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?
Here is the code:
<?php
// Include file for database connectivity
$db_server = "localhost";
|
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 how to overcome this error, so can someone please help me out?
thanks,
$string = "one two three";
$word_array = explode(" ",$string);
for ($i=count($word_array); $i>=1; $i--)
{
$word = $i == 1 ? " word" : " words";
echo "Strings containing...
|
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 ),
i have foreach loop
foreach($at as $v){
switch ($v){
|
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: Undefined offset: 1 in C:\wamp\www
Notice: Undefined offset: 2 in C:\wamp\www
i think scope of my Array variable : newsimg has a problem.
<div id="postedit" class="clearfix">
<h2 class="ico_mug">ADD NEWS</h2>
<form method="post" action="<?php echo...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
| |
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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 we have to send another system
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |