1

i have this code:

$pr  =  $tst_db->mlm_get_parent(1);        // give me the value = 4 (parent_id of id 1)
$pr1 =  $tst_db->mlm_get_parent($pr);     // give me the value = 7 (parent_id of id 4)
$pr2 =  $tst_db->mlm_get_parent($pr1);   // give me the value = 5 (parent_id of id 7)
$pr3 =  $tst_db->mlm_get_parent($pr2);  // give me the value = 2 (parent_id of id 5)
$pr4 =  $tst_db->mlm_get_parent($pr3); // give me the value = 0 (becouse id 2 not have parent id)
echo $pr; echo $pr1; echo $pr2; echo $pr3; echo $pr4;

Its possible to make like a foreach loop
with automatic creation of ($pr(num) = $tst_db->mlm_get_parent($pr(num);)
and stop it when some $pr are equal to 0?
I need to echo too all parent_it created in this automatic loop.

This is what im expeting to have like results:
All parent_id of id 1 are: 4,7,5,2

Thanks in advance

GINO GINI
  • 35
  • 6

1 Answers1

1

This will print out all the parents of id 1 to the screen:

$id = 1;

while($id != 0) {
    $id  =  $tst_db->mlm_get_parent($id);
    echo $id;
}

// this will print: 47520

If you want the values to be stored in an array whose keys are of the exact format shown in the question:

$arr = array();
$id = 1;
$pr = 'pr';
$count = 0;

while($id != 0) {
    $id  =  $tst_db->mlm_get_parent($id);
    $arr[$pr] = $id;
    $ccount++;
    $pr = 'pr'.$count;
}

print_r($arr);  // prints: Array([pr] => 4 [pr1] => 7 [pr2] => 5 [pr3] => 2 [pr4] => 0)
echo join(",", $arr);   // prints 4,7,5,2,0

To produce independent variables based on the array keys you could use the extract() method:

extract($arr);

echo $pr;   // prints 4
echo $pr3;  // prints 2

Note:

  • The extract() method will overwrite any existing variables with names equal to the array's key values.
  • Do not use extract() on untrusted data, like user input (e.g. $_GET, $_FILES).

You can also create a foreach loop that iterates through the populated $arr array:

foreach($arr as $key => $val) {
    // Do something here.
    // Although this foreach is not necessary since you could
    // have already done what you want to do within the above 
    // while loop
}
Ivan86
  • 5,695
  • 2
  • 14
  • 30
  • Sorry @Ivan86 but parent_id are not in order like 2 - 3 - 4, some id have parent id different and not in order. – GINO GINI Mar 17 '21 at 22:51
  • @GINOGINI Sorry, I'm afraid I don't understand what you are exactly trying to accomplish. Please update your question to clarify what you are expecting as output. – Ivan86 Mar 17 '21 at 22:57
  • Thanks @Ivan86 for reply. I need to get all parent_id of 1 so the only way are creating the various pr that take id from previous parent_id and when results of parent_id are 0 the id have no parent. But i wanna make automatic this kind of. – GINO GINI Mar 17 '21 at 23:00
  • @GINOGINI I edited, this should now solve your problem. – Ivan86 Mar 18 '21 at 00:59
  • Thanks a lot @Ivan86 , you saved me. A little question, in this code ` $id = 1; while($id != 0) { $id = $tst_db->mlm_get_parent($id); echo $id; } ` Echo all parents of id 1, but echo too the 0 (the parent of last id) but i dont wanna the 0 in results, i can exclude it? Regards – GINO GINI Mar 18 '21 at 08:52
  • Hi @Ivan86 sorry but im not able for Upvote for now. – GINO GINI Mar 18 '21 at 23:54
  • Shure @Ivan86 no problem. Take a look to my new question :) maybe you can help me https://stackoverflow.com/questions/66700802/create-an-array-from-pairwisedifference-function – GINO GINI Mar 19 '21 at 00:02