Store depth first seach value into array in php

2016-08-12 php depth-first-search

Currently I'm working on a project, which using the depth first search to retrieve value, but I'm only able to echo the value, but I don't know how to store the value into the variable.

Here is my code

function calculate_ttl_member_agent ($conn, $id) {
    $id_val = $level = "";
    $search_dl_sql = "select * from table where foreign_ID = ".$id;
    $search_dl_exe = mysqli_query($conn, $search_dl_sql);

    while($result = mysqli_fetch_assoc($search_dl_exe)) {
        $level = $result['level'];
        $id_val = $result['ID'];
        echo ",'".$level."'";
        calculate_ttl_member_agent ($conn, $id_val);
    }
}

I have been trying the use return, but it only give the first level value..

Answers

Store values in an array as commented by Alexanderp and use after

// inside while

 $level = $result['level'];
  $id_val = $result['ID'];
  $level[]=  $level;

or

$arr['level'][] = $result['level']; 
$arr['id_val'][] = $result['ID'];

Create an array and add values to it.

function calculate_ttl_member_agent ($conn, $id) {
  $id_val = $level = "";
  $search_dl_sql = "select * from table where foreign_ID = ".$id;
  $search_dl_exe = mysqli_query($conn, $search_dl_sql);
  // define an empty array
  $arr = [];
  while($result = mysqli_fetch_assoc($search_dl_exe)){
    $level = $result['level'];
    $id_val = $result['ID'];
    echo ",'".$level."'";
  calculate_ttl_member_agent ($conn, $id_val);
  array_push( $arr, [$level, $id_val, 'add anything else needed']);

  return $arr;

}}

Related