1

you can suggest me a title after reading

So i have this 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 am gonna tell what i want to do as above since my english is not good. Please give me notice if you dont get it.

  • Carl's id2 = 0
  • mary'S id2 = 0
  • jake's id2 != 0 ----> right there i want to find jake's id2 inside id1 sections. (jack id2= 4592 and this is mary's id1). now i want to get mary's id2 again which is 0 which means i can look for next name (if mary's id2 would not equal to 0 i want to continue through loop)
  • john's id2 =0
  • shannon's id2 = 6495 ----> 6495 is john's id1 and john's id2 = 0 ----> loop ends
  • kalsey's id2 = 5820 -----> 5820 gives me jake—--> jake's id2 is not equal 0 agian(its 4592)--->continue to loop—--> 4592 gives me mary ---> marry's id2 = 0 ---> loop ends
  • scarlet's id2 = 2281 ----> takes us to kalsey -----> kalsey takes us to jake ----> jake takes us mary ---> loop ends.

How i am gonna write my program? İ tried something like this.

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

when this program reaches jake it repeat itself to infinity ------> carl , mary, jake, mary, jake, mary, jake, mary, jake, mary, jake, .....

user3301042
  • 352
  • 2
  • 14

2 Answers2

0

This seems fun, as other's have said in the comments, you need recursion, not a regular loop.

<?php

function answer($data, $rowNumber) {
  // Echo so you can see the path
  echo $data[$rowNumber]['name'] . " --> ";

  // Get id2
  $id2 = $data[$rowNumber]['id2'];

  // If id2 is 0, then return success
  if ($id2 == 0) {
    echo "done\n";
    return true;
  }

  // If id2 isn't 0, look up the next name
  foreach ($data as $rowNum => $d) {
    if ($d['id1'] == $id2) {
      return answer($data, $rowNum);
    }
  }

  // No next name had id2=0, failure
  echo "failure, no path ends with 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);
Parris Varney
  • 11,320
  • 12
  • 47
  • 76
0

I don't have your database so I'm using an array, you just copy-paste and run next code and let me know if this is your expected result, it's recursive so it's kind of hard to explain, but I will explain it after you test it:

<?php
echo "Right sequence : " .
     "carl-mary-jake-mary-john-shannon-john-kalsey-jake-mary-scarlet-kalsey-jake-mary" .
     "<br/>" .
     "New sequence : ";
$database = Array( Array( "name" => "carl",    "id1"  => "1154", "id2"  => "0"    ),
                   Array( "name" => "mary",    "id1"  => "4592", "id2"  => "0"    ),
                   Array( "name" => "jake",    "id1"  => "5820", "id2"  => "4592" ),
                   Array( "name" => "john",    "id1"  => "6495", "id2"  => "0"    ),
                   Array( "name" => "shannon", "id1"  => "1047", "id2"  => "6495" ),
                   Array( "name" => "kalsey",  "id1"  => "2281", "id2"  => "5820" ),
                   Array( "name" => "scarlet", "id1"  => "4419", "id2"  => "2281" )
                 );

recursive( 0,TRUE ); // START PROCESS.

// $I    : POINTS TO EACH ELEMENT IN ARRAY.
// $NEXT : TRUE  = JUMP TO NEXT ELEMENT.
//         FALSE = DON'T JUMP TO NEXT ELEMENT.
function recursive ( $i,$next )  {
global $database;
if ( $i < count( $database ) ) // IF NOT BEYOND LIMITS...
   { echo $database[ $i ][ "name" ] . "-";
     if ( $database[ $i ][ "id2" ] != "0" ) // IF HAS LINKED ELEMENT...
        recursive( find_id2( $database[ $i ][ "id2" ] ),false ); // FALSE = DONT VISIT NEXT ELEMENT.
     if ( $next )
        recursive( ++$i,true ); // TRUE = VISIT NEXT ELEMENT.
   }
}

function find_id2 ( $id2 ) {
global $database;
for ( $i = 0; $i < count( $database ); $i++ )
  if ( $database[ $i ][ "id1" ] == $id2 )
     return $i;
return -1; // NOT FOUND.
}

?>

Parameter $next does the magic ("sometimes can be continuous and sometimes not"), thanks to it we decide if the next element must be visited or not. For example, "carl" must visit the next, "mary" (TRUE), but "jake" jumps to "mary" which jumps to "jake", thanks to $next we know that "mary" must not jump to "jake" (FALSE) or we will loop forever.