php undefined offset error

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • simple12
    New Member
    • Oct 2007
    • 5

    php undefined offset error

    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
    [code=php]
    // 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_forum s = '1')
    ORDER BY rand() ";

    $result = $db->sql_query($sql );
    while($row = $db->sql_fetchrow($ result))
    {
    $adcode[$row['position']] = html_entity_dec ode($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('UPD ATE ' . AD_TABLE . ' SET views = views +1 WHERE ID = ' . $adID[$i]['ID']);
    }
    }
    // End output Ad

    // Assign index specific vars
    $template->assign_vars(ar ray(
    '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),

    [/code]
  • Atli
    Recognized Expert Expert
    • Nov 2006
    • 5062

    #2
    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.

    Comment

    • simple12
      New Member
      • Oct 2007
      • 5

      #3
      Thanks for the reply.
      I am newbie with php. Please you exactly tell me where and what i should change to solve the problem.

      Comment

      • Atli
        Recognized Expert Expert
        • Nov 2006
        • 5062

        #4
        Try changin this:
        [code=php]
        // update views for every Ad
        for ($i = 1; $i <= 4; $i++)
        {
        if ($adID[$i]['ID']) // line 132 error line
        {
        $db->sql_query('UPD ATE ' . AD_TABLE . ' SET views = views +1 WHERE ID = ' . $adID[$i]['ID']);
        }
        }
        [/code]

        Into this:
        [code=php]
        // update views for every Ad
        foreach($adID as $ad)
        {
        if ($ad['ID'])
        {
        $db->sql_query('UPD ATE ' . AD_TABLE . ' SET views = views +1 WHERE ID = ' . $ad['ID']);
        }
        }
        [/code]

        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.

        Comment

        • simple12
          New Member
          • Oct 2007
          • 5

          #5
          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

          Comment

          • Atli
            Recognized Expert Expert
            • Nov 2006
            • 5062

            #6
            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.
            [code=php]
            'AD_CODE4' => $adcode[4], //line 141 error line
            'AD_CODE5' => $adcode[5], //line 142 error line'
            [/code]

            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.

            Comment

            • pbmods
              Recognized Expert Expert
              • Apr 2007
              • 5821

              #7
              Heya, simple12.

              You can prevent these notices by doing this:
              [code=php]
              'AD_CODE4' => isset($adcode[4]) ? $adcode[4] : 'default value',
              'AD_CODE5' => isset($adcode[5]) ? $adcode[5] : 'default value',
              [/code]

              And in PHP 6, you'll be able to shorten it to:
              [code=php]
              'AD_CODE4' => $adcode[4] ?: 'default value',
              'AD_CODE5' => $adcode[5] ?: 'default value',
              [/code]

              Comment

              • simple12
                New Member
                • Oct 2007
                • 5

                #8
                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.

                Comment

                • simple12
                  New Member
                  • Oct 2007
                  • 5

                  #9
                  Hey guys you really are superb in php.
                  I fixed with your support all the problems
                  at last i used only this code
                  [code=php]
                  'AD_CODE4' => isset($adcode[4]) ? $adcode[4] : '',
                  'AD_CODE5' => isset($adcode[5]) ? $adcode[5] : '',
                  [/code]

                  Just curious to know if it would work fine or will be unstable.But lol its working.

                  Comment

                  • pbmods
                    Recognized Expert Expert
                    • Apr 2007
                    • 5821

                    #10
                    Heya simple12.

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

                    Comment

                    Working...