-2

With great difficulty i was able to finish a project, however i get this error -

PHP: Undefined offset 1

error, although my code is correct(i think).

Can you please help me in finding what is the mistake am doing...

<?php
for ($i = 1; $i <= count($res1); $i++) {
    for ($j = 1; $j <= count($res); $j++) {
        if ($res1['LIEFERANT'][$i] == $res['LIEFERANT'][$j]) {
            echo $res1['LIEFERANT'][$i] = TRUE;
            echo $res1['LIEFERANT'][$j] = TRUE;
        } else {
            echo $res1['LIEFERANT'][$i] = FALSE;
            echo $res1['LIEFERANT'][$j] = FALSE;
        }
    }
}

The error occurs at if condition. Please help in finding the error.

Thank you in advance!!

Alive to die - Anant
  • 70,531
  • 10
  • 51
  • 98
Auto123
  • 9
  • 5
  • What values does $res1 and $res have? – Muhammad Saqlain Dec 04 '18 at 08:20
  • The error is quite clear, the offset `1` of `$res1['LIEFERANT']` or `$res['LIEFERANT']` is not defined. How is your array defined ? Also you can var_dump it to see the content, might help. – Jules R Dec 04 '18 at 08:21
  • can we have an example of what's in your arrays? one obvious thing though, if you use `[$i]` on `$res1['LIEFERANT']`, you should use `count($res1['LIEFERANT'])` instead of `count($res1)`, or maybe did you inverse keys and should use `$res1[$i]['LIEFERANT']`? – Kaddath Dec 04 '18 at 08:21
  • @Muhammad - $query1 = "Select LIEFERANT1 AS LIEFERANT FROM NVESCANNEN WHERE LIEFERANT2 = '".$lieferant."' UNION ALL Select LIEFERANT2 AS LIEFERANT FROM NVESCANNEN WHERE LIEFERANT1 = '".$lieferant."'"; $row = oci_fetch_all($stid,$res); $query2 = "SELECT DISTINCT LIEFERANT FROM SVBKOPF WHERE ID IN (SELECT BID FROM SVBDETAIL WHERE NVE0 = '".$_SESSION['nvenummer']."')"; $res1 = array($stid); $row = oci_fetch_all($stid,$res1); – Auto123 Dec 04 '18 at 08:33
  • @Jules - $res1 is the result of the query1 "Select LIEFERANT1 AS LIEFERANT FROM NVESCANNEN WHERE LIEFERANT2 = '".$lieferant."' UNION ALL Select LIEFERANT2 AS LIEFERANT FROM NVESCANNEN WHERE LIEFERANT1 = '".$lieferant."'"; and $res is the result of query2 - "SELECT DISTINCT LIEFERANT FROM SVBKOPF WHERE ID IN (SELECT BID FROM SVBDETAIL WHERE NVE0 = '".$_SESSION['nvenummer']."')"; – Auto123 Dec 04 '18 at 08:34
  • Quite unreadable, can you edit the question ? – Jules R Dec 04 '18 at 08:40
  • if result of a query using fetch_assoc, then you definetly switched the keys, first is the row, second is the table column: use `$res1[$i]['LIEFERANT']`. By the way, i think you should count from 0: `for($i=0; $i – Kaddath Dec 04 '18 at 08:43
  • @Kaddath - I tried but it still gives me undefined offset error from 0 to n – Auto123 Dec 04 '18 at 09:08
  • @jules - i get this undefined offset 0 error for the below code -----------for($i=0; $i<=count($res1); $i++) { for($j=0; $j<=count($res); $j++) { if ($res1[$i]['LIEFERANT'] == $res[$j]['LIEFERANT']) { echo $res1[$i]['LIEFERANT'] = TRUE; echo $res[$j]['LIEFERANT'] = TRUE; } else{ echo $res1[$i]['LIEFERANT'] = FALSE; echo $res[$j]['LIEFERANT'] = FALSE; } } } – Auto123 Dec 04 '18 at 09:09

1 Answers1

0

You're using $i variable as an index of $res1 which has count($res1) elements. The problem is that $i can be equal to count($res1) but array indexes are 0-based. The same goes for $j and $res. I think this should work better:

for($i=0; $i<count($res1); $i++)
{
  for($j=0; $j<count($res); $j++)
  {
    if ($res1['LIEFERANT'][$i] == $res['LIEFERANT'][$j])
    {
      echo $res1['LIEFERANT'][$i] = TRUE;
      echo $res1['LIEFERANT'][$j] = TRUE;
    }
    else{
      echo $res1['LIEFERANT'][$i] = FALSE;
      echo $res1['LIEFERANT'][$j] = FALSE; 
    }
  }   
}
Karol Samborski
  • 2,757
  • 1
  • 11
  • 18