php - establishing a loop that sometimes can be continuous and sometimes not -


you can suggest me title after reading

so have database

 name      id1    id2   carl     1154    0  mary     4592    0  jake     5820    4592  john     6495    0  shannon  1047    6495  kalsey   2281    5820  scarlet  4419    2281 

i gonna tell want above since english not good. please give me notice if dont it.

  • carl's id2 = 0
  • mary's id2 = 0
  • jake's id2 != 0 ----> right there want find jake's id2 inside id1 sections. (jack id2= 4592 , mary's id1). want mary's id2 again 0 means can next name (if mary's id2 not equal 0 want continue through loop)
  • john's id2 =0
  • shannon's id2 = 6495 ----> 6495 john's id1 , john's id2 = 0 ----> loop ends
  • kalsey's id2 = 5820 -----> 5820 gives me jake—--> jake's id2 not equal 0 agian(its 4592)--->continue loop—--> 4592 gives me mary ---> marry's id2 = 0 ---> loop ends
  • scarlet's id2 = 2281 ----> takes kalsey -----> kalsey takes jake ----> jake takes mary ---> loop ends.

how gonna write program? İ tried this.

//first while gets id2 while($row = mysqli_fetch_array($command)){     while ($row[id2] != 0){         //get id1          // find id2      } } 

when program reaches jake repeat infinity ------> carl , mary, jake, mary, jake, mary, jake, mary, jake, mary, jake, .....

this seems fun, other's have said in comments, need recursion, not regular loop.

<?php  function answer($data, $rownumber) {   // echo can see path   echo $data[$rownumber]['name'] . " --> ";    // id2   $id2 = $data[$rownumber]['id2'];    // if id2 0, return success   if ($id2 == 0) {     echo "done\n";     return true;   }    // if id2 isn't 0, next name   foreach ($data $rownum => $d) {     if ($d['id1'] == $id2) {       return answer($data, $rownum);     }   }    // no next name had id2=0, failure   echo "failure, no path ends id2=0\n";   return false; }  $array = array (  array ('name' => 'mary',    'id1' => 4592, 'id2' => 0),  array ('name' => 'jake',    'id1' => 5820, 'id2' => 4592),  array ('name' => 'kalsey',  'id1' => 2281, 'id2' => 5820),  array ('name' => 'scarlet', 'id1' => 4419, 'id2' => 2281), );  answer($array, 3); 

Comments

Popular posts from this blog

python - No exponential form of the z-axis in matplotlib-3D-plots -

php - Best Light server (Linux + Web server + Database) for Raspberry Pi -

c# - "Newtonsoft.Json.JsonSerializationException unable to find constructor to use for types" error when deserializing class -