mladirazvijac
mladirazvijac

Reputation: 67

"Undefined offset" can't find reason PHP

I can't find reason for Undefined offset: 0 in error. I have array and I gave index in bounds :/ I'm new in PHP + SQL, but I have to do this for school task.

If I understand it correctly my get_searched_grad function returns array of arrays...

Error is in lines where I access to $grd array $grd[someNumber]

<?php
  foreach (get_searched_grad($search_key) as $grd) {
?>
  <tr>
    <th>
      <?php
        $grd[0];
      ?>                         
    </th>
    <th>
      <?php
        $grd[1];
      ?>                         
    </th>
    <th>
      <?php
        $grd[2];
      ?>                         
    </th>
    <th>
      <?php
        $grd[3];
      ?>                         
    </th>
  </tr>

<?php
  }
?>

And in get_searched_grad() function I have:

function get_searched_grad($searchWord) {

  $gradovi = array();

  if ($searchWord != 0) {
    $query = mysql_query("SELECT `NAZIV_GRADA`, `PTT`, `BROJ_STANOVNIKA`, `NAZIV_DRZAVE` FROM `grad`,`drzava` WHERE `NAZIV_GRADA` LIKE '%{$searchWord}%' AND drzava.ID_DRZAVE = grad.ID_DRZAVE");
  } else {
    $query = mysql_query("SELECT `NAZIV_GRADA`, `PTT`, `BROJ_STANOVNIKA`, `NAZIV_DRZAVE` FROM `grad`,`drzava` WHERE drzava.ID_DRZAVE = grad.ID_DRZAVE");
  }
  while ($row = mysql_fetch_assoc($query)) {
    $gradovi [] = $row;
  }
  return $gradovi;
}

Some variable and table row names are on Serbian :) Sorry for that...

array(2){
    [
        0
    ]=>array(4){
        [
            "NAZIV_GRADA"
        ]=>string(7)"Beograd"[
            "PTT"
        ]=>string(5)"11000"[
            "BROJ_STANOVNIKA"
        ]=>string(6)"400000"[
            "NAZIV_DRZAVE"
        ]=>string(6)"Srbija"
    }[
        1
    ]=>array(4){
        [
            "NAZIV_GRADA"
        ]=>string(9)"Zrenjanin"[
            "PTT"
        ]=>string(5)"23000"[
            "BROJ_STANOVNIKA"
        ]=>string(5)"12000"[
            "NAZIV_DRZAVE"
        ]=>string(6)"Srbija"
    }
}

Upvotes: 0

Views: 199

Answers (3)

Vipin Kumar Soni
Vipin Kumar Soni

Reputation: 834

foreach(get_searched_grad($search_key) as $grd) {
    if(is_array($grd)) {
        foreach($grd as $key => $value) {
            echo $grd[$key];
        }
    }
}

Upvotes: 0

Abhik Chakraborty
Abhik Chakraborty

Reputation: 44874

Your var dump shows

array(2) {
  [0]=> array(4) { 
             ["NAZIV_GRADA"]=> string(7) "Beograd" 
             ["PTT"]=> string(5) "11000" 
             ["BROJ_STANOVNIKA"]=> string(6) "400000" 
             ["NAZIV_DRZAVE"]=> string(6) "Srbija" 
   } 
   [1]=> array(4) { 
             ["NAZIV_GRADA"]=> string(9) "Zrenjanin" 
             ["PTT"]=> string(5) "23000" 
             ["BROJ_STANOVNIKA"]=> string(5) "12000" 
             ["NAZIV_DRZAVE"]=> string(6) "Srbija" 
   } 
}

So you should be doing something as

$data = get_searched_grad($search_key) ;
foreach ($data as $grd) {
                    ?>
                    <tr>
                        <th> <?php
                            $grd["NAZIV_GRADA"];
                            ?>                         
                        </th>
                        <th> <?php
                            $grd["PTT"];
                            ?>                         
                        </th>
                        <th> <?php
                            $grd["BROJ_STANOVNIKA"];
                            ?>                         
                        </th>
                        <th> <?php
                            $grd["NAZIV_DRZAVE"];
                            ?>                         
                        </th>
                    </tr>

                    <?php
                }

Why it did not work for u. Since you are looking

your_array[0][0]
your_array[0][1]
        etc and this will fail since its an associative array.

Upvotes: 0

Ryan
Ryan

Reputation: 14659

That most likely means your database didn't return any results. To check for that you can use the isset method which returns true only if a variable exists in scope and is not null. It also works on array subscripts.

So if we have an array:

$data = array(
  'stack' => 'overflow'
);

We don't always know for sure that a key exists in the array, so to find out you call isset on the array with the subscript.

if(isset($data['stack'])) {
  echo $data['stack'];
} else {
  echo 'stack was not a key inside the data array';
}

Your method could use some re-factoring. I'm assuming your're attempting to check if an empty string or null value has been passed into it by performing the != 0 statement. A better way to check if a string is not empty or null is to do this:

if( isset($searchword) && strlen($searchword) > 0) {
  // at this point we have at least one character to search for.
}

Your working with an array of arrays, so you need to traverse both of them. Finally, to answer your question. You need to change your implementation to this:

$data = get_searched_grad($search_key);
for($i = 0; $i < count($data) $i++) {
  $row = $data[$i];
  echo $row['NAZIV_GRADA'] . "<br />";
  echo $row['PTT'] . "<br />";
  //...
}

Upvotes: 1

Related Questions