0

I have a problem showing the field of the array 'notes' since it does not print the numbers generated by the 'addNotes' function.

The code loads the preloaded data and assigns random numbers with the 'addNotes' function to the 'notes' fields of the array and finally shows the data of the array in showStudentsList ().

<?php session_start();?>
<html>
<body>

<?php

        if(!existDataInSession()){

        initializePreloadedData();
    }

    function existDataInSession(){
        return $_SESSION['data'] != NULL;
    }

    function initializePreloadedData(){

        $person1= [
            'name' => 'person1',
            'notes' => []
        ];

        $person2= [
            'name' => 'person2',
            'notes' => []
        ];

        $data=[$person1,$person2];
        $_SESSION['data'] = $data;
    }

    function addNotes(){

        foreach ($data as $key => $value) {

            $data[$key]['notes'] = random_int(0,100);
        }
    }

    addNotes();
    showStudentsList();

    function showStudentsList(){

        $data = $_SESSION['data'];

        foreach ( $data as $student ) {

            echo $student['name'] . " ";
            echo $student['notes'];
            echo implode($student['notes']);
            echo "<br>";
        }
    }

    //Result

    //person1 Array
    //person2 Array

    ?>

</body>
</html>
WPU
  • 17
  • 4
  • You need to turn on error reporting. Then you will get all the messages about undefined variables. – Nick Jan 15 '19 at 12:38

2 Answers2

0

If I am not wrong to understand your requirement, $data is not defined in addNotes() function:

function addNotes(){
    $data = $_SESSION['data']; // fetch data from session
    foreach ($data as $key => $value) {

        $data[$key]['notes'] = random_int(0,100);
    }
    $_SESSION['data'] = $data; // assign into session again 
}

now use just echo without implode to get notes like echo $student['notes']

Hope this help.

Bhaskar Jain
  • 1,651
  • 1
  • 12
  • 20
0

There are several problems in your code.

  • You change between array and integer notes. The plural implies an array.
  • You write to a copy of $_SESSION
  • $_SESSION['data'] != null throws a notice when not defined

Fixed version using notes array:

if(!existDataInSession()){

  initializePreloadedData();
}

addNotes();
showStudentsList();

function existDataInSession(){
  // we check if data was set. just to make double sure we check for type array as well

  return isset($_SESSION['data']) && is_array($_SESSION['data']);
}

function initializePreloadedData(){

  $person1= [
    'name' => 'person1',
    'notes' => []
  ];

  $person2= [
    'name' => 'person2',
    'notes' => []
  ];

  $_SESSION['data'] = [ $person1, $person2 ];
}

function addNotes(){

  // operate on $_SESSION referencing the value since we want to ALTER the session data

  foreach ($_SESSION['data'] as &$value) {

    // since notes was defined as an ARRAY, we append here
    $value['notes'][] = random_int(0,100);
  }
}

function showStudentsList(){

  $data = $_SESSION['data'];

  foreach ( $data as $student ) {

    echo $student['name'] . " ";
    echo implode(', ', $student['notes']);
    echo "<br>", PHP_EOL;
  }
}
Pinke Helga
  • 6,378
  • 2
  • 22
  • 42