-2

As far as I know I have defined the variable, however I am getting the following error :

Notice : Undefined index: headerid in ..

function group_items($db_results) {
  $data = array();
  while($row = mysqli_fetch_assoc($db_results)) {
    $title_id = htmlentities($row['TitleID']);
    $title = htmlentities($row['MainTitle']);

    // group Main Title by title id
    $data[$title_id]['title'] = htmlentities($row['MainTitle']);

    // group data by title id
    $data[$title_id]['data'][] = array( 
      'headerid' => $row['headerid'],
      'title'     => htmlentities($row['title']),
      'content'   => htmlentities($row['content'])
      );
  }

// testing
if (isset($data['headerid'])) {
        echo "Yes";
    } else {
         echo "NO";
     }

      return $data;
    }

When I tested using both isset() and array_key_exists for true / false it resulted in false.

When using print_r I get the following: Array ( [1] => Array ( [title] => Promising practices regarding general principles of community policing [data] => Array ( [0] => Array ( [a] => 1 [title] => Initiating and developing coordination and collaboration actions between law enforcement agen [content] => Good cooperation and coordination relations between .... more content here

ChriChri
  • 153
  • 1
  • 12

1 Answers1

0

$data['headerid'] is not defined in you code.

Try Print_r($data); to check array elements.

At testing you need to edit your code to

isset($data[$title_id]['data'][0]['headerid']))
Muhammad Hassaan
  • 7,296
  • 6
  • 30
  • 50
  • Thanks for your edit , I answered from phone so I can't style the answer. – Mohamed Nakhlawy Jun 24 '18 at 19:39
  • This helped with the testing since now the variable isset is true. However the error is still there :/ – ChriChri Jun 24 '18 at 20:09
  • use Print_r($data); to check array elements and understand whats the array form. do you still see this Notice : Undefined index: headerid !? – Mohamed Nakhlawy Jun 24 '18 at 20:13
  • I used that and everything seems ok but the error still shows – ChriChri Jun 24 '18 at 20:18
  • if you still see this Notice : Undefined index: headerid the reason is : 'headerid' => $row['headerid'], headerid is not exist in your table or may be you forget to add this column at select headerid from table – Mohamed Nakhlawy Jun 24 '18 at 20:22
  • I used Select * to get the content from the table. I am also getting 2 more columns from the same table and don't get an error. – ChriChri Jun 24 '18 at 20:28
  • Can you write the result to print_r($row); here – Mohamed Nakhlawy Jun 24 '18 at 20:32
  • I'll put it as a comment above – ChriChri Jun 24 '18 at 20:34
  • Array ( [1] => Array ( [title] => Promising [data] => Array ( [0] => Array ( [a] => 1 [title] => Initiating [content] => Good cooperation . ) [1] => Array ( [a] => 2 [title] => Improve [content] => ) [2] => Array ( [a] => 3 [title] => Increase [content] => Raising public.. ) ) ) [2] => Array ( [title] => Another title [data] => Array ( [0] => Array ( [a] => 11 [title] => Title [content] => There's no name2 column in either CUSTOMER table - you need to rearrange the customer name columns, swapping for null to match the desired output. ) ... ) ) ) ) – ChriChri Jun 24 '18 at 20:45
  • print_r($row); not print_r($data); you should write while($row = mysqli_fetch_assoc($db_results)) { print_r($row); – Mohamed Nakhlawy Jun 24 '18 at 20:50