0
$result = db2_getsub( array('campaigns_id' => $this_id) );

if (!is_null($result))
    {
          $numsub = 0;
    while ( $row = $result->fetch_object() )
        {
            $numsub = $numsub +1;
            array_push($arrFornavn, $row->sub_name);
                    array_push($arrEtternavn, $row->sub_code);
                    }
          $formcountfld=$numsub;
                }
        else
        {
        $numsub=1;
        $formcountfld=1;
        }

            $i = 1;

            if (1==1) {

                for ($i = 1; $i <= $numsub; $i++)
                {
        ?>
          <div class="fieldrow_horz">

            <div class="fieldgroup">
              <input type="text" id="fornavn_<?= $i ?>" name="fornavn_<?= $i ?>" value="<?= $arrFornavn[$i-1] ?>" />
            </div>

            <div class="fieldgroup">
              <input type="text" id="etternavn_<?= $i ?>" name="etternavn_<?= $i ?>" value="<?= $arrEtternavn[$i-1] ?>" />
            </div>

          </div>

          <?php

                    }

            } else {

        ?>

I am getting an error as Notice: Undefined offset: 0 can anybody help .thanks in advance looking forward for an answer.

Jeff Noel
  • 7,500
  • 4
  • 40
  • 66

2 Answers2

2

It means you are trying to access a value that does not exist, which in your case is $arrEtternavn[0].

Small example:

$array = array();
$array[1] = 'one';
$array[2] = 'two';
$array[4] = 'four';

echo $array[0]; // This will give a notice, $array[0] does not exist.
echo $array[1]; // 'one'
echo $array[2]; // 'two'
echo $array[3]; // This will give a notice, $array[3] does not exist.
echo $array[4]; // 'four'

A very easy fix would me something like this:

value="<?= isset($arrEtternavn[$i-1]) ? $arrEtternavn[$i-1] : '' ?>"

This is a short elseif-> (condition) ? if_True : if_False

Edit: I'd like to add that a notice isnt a very bad thing. No notices would be the best thing, but it should not keep you awake at night.

Martijn
  • 15,791
  • 4
  • 36
  • 68
  • i am getting error in this line –  Jun 25 '13 at 12:33
  • That is because you use $i-1. $i starts with value=1, and so $i-1 makes 0. It's good practice to make all array's start at key 0, php does the same thing. – Martijn Jun 25 '13 at 12:38
  • 1
    value="= isset($arrEtternavn[$i-1]) ? $arrEtternavn[$i-1] : '' ?>" this works perfectly –  Jun 25 '13 at 12:40
1

My guess would be this... In the case where $result is empty you are setting $numsub = 1 in your else clause. However, in this case you have not added anything to the array. Your later loop however defines the continuing condition as $numsub <= 1, so will execute and try to print out array index 0, which because $result was NULL doesn't contain because the array is empty.

Changing your for loop to be

for ($i = 0; $i < $numsub; $i++) // Notice now strictly less than
{
    <div class="fieldrow_horz">
        <div class="fieldgroup">
          <input type="text" id="fornavn_<?= $i+1 ?>" name="fornavn_<?= $i+1 ?>" 
                                              ^^^^                       ^^^^ 
                 value="<?= $arrFornavn[$i] ?>" />
                                        ^^^
        </div>

        <div class="fieldgroup">
          <input type="text" id="etternavn_<?= $i+1 ?>" name="etternavn_<?= $i+1 ?>" 
                                               ^^^^                       ^^^^ 
                  value="<?= $arrEtternavn[$i] ?>" />
                                           ^^^
        </div>
      </div>

As Martijn suggests, you can always use isset() to find out if an array index exists. From the logic you've implemented I think you're trying to assure that the array index in that loop is in fact guaranteed to be set, so I'm guess its the small logic error described above that causing the problem.

Hope it helps...

Jimbo
  • 4,352
  • 3
  • 27
  • 44