2016-08-26 10 views
1

を予想通り、私はここに最初の再帰 を使用して、いくつかの結果を表示したい来ていない私のテーブルの構造は、これはすべての連絡先は、今私がときcontactidたい を記憶して、私のテーブルの接触である再帰関数データ

contactid  name  reportsto 
244797   ankit   9876 
438    Mukti   244797 
445    Moorthy  244797 
446    P K Roy  244797 
448    Suruchi  438 
542    Lalit Kumar 438 
543    Balkrishan 542 

です244797はログインした後、244797へのレポートが表示されているすべてのIDと、244797に関連するIDのいずれかに報告された連絡先ID(例448と542は438に報告され、543は542に報告される)接触していないIDループは終了し、すべての接点はアレイに保存されます。 私はその後、私はその244797に報告された私のコードをチェックして、私の問題を評価してくださいおかげIDのみを受け取る($ printresを)しますprint_rとき、これは

$contactid=$_SESSION['customer_id']; 
$i=0; 
function contactId($contactid='') { 
    $sqlq="select contactid,firstname,lastname,reportsto from contactdetails where reportsto=".$contactid.""; 
    $res=mysql_query($sqlq); 
    //$contacts=array(); 
    $links = array(); 
if($row = mysql_num_rows($res) > 0){ 

    while($rows = mysql_fetch_assoc($res)) { 
    //echo "<pre>"; print_r($rows); 
      $links[$i]['id'] = $rows['contactid']; 
      $links[$i]['firstname'] = $rows['firstname']; 
      $links[$i]['lastname'] = $rows['lastname']; 
      $links[$i]['reportsto'] = $rows['reportsto']; 
      contactId($rows['contactid']); 
      $i++; 
    } 

} 

return $links; 

} 


$printres=contactId($contactid); 

私のPHPコードです。私が受けた 出力は

Array 
(
[] => Array 
    (
     [id] => 438 
     [firstname] => Mukti 
     [lastname] => Srivastava 
     [reportsto] => 244797 
    ) 

[1] => Array 
    (
     [id] => 445 
     [firstname] => Moorthy 
     [lastname] => NA 
     [reportsto] => 244797 
    ) 

[2] => Array 
    (
     [id] => 446 
     [firstname] => P K Roy 
     [lastname] => Choudhary 
     [reportsto] => 244797 
    ) 

) 

Array 
(
[0] => Array 
    (
     [id] => 438 
     [firstname] => Mukti 
     [lastname] => Srivastava 
     [reportsto] => 244797 
    ) 

[1] => Array 
    (
     [id] => 445 
     [firstname] => Moorthy 
     [lastname] => NA 
     [reportsto] => 244797 
    ) 

[2] => Array 
    (
     [id] => 446 
     [firstname] => P K Roy 
     [lastname] => Choudhary 
     [reportsto] => 244797 
    ) 
[3] => Array 
    (
     [id] => 448 
     [firstname] => suruchi 
     [lastname] => Choudhary 
     [reportsto] => 438 
    ) 
[4] => Array 
    (
     [id] => 542 
     [firstname] => lalit kumar 
     [lastname] => Choudhary 
     [reportsto] => 438 
    ) 
[5] => Array 
    (
     [id] => 543 
     [firstname] => balkrishan 
     [lastname] => Choudhary 
     [reportsto] => 542 
    ) 

    ) 
+0

あなたの文が明確ではありません。もう少し説明できますか?あなたが望む出力が何であるか入力に基づいているかもしれません、それはそれをもっとクリアします –

+0

あなたの結果配列をどのように見せたいですか?目的の出力とここに表示されている出力を表示します。 –

+0

親愛なる@Anantはテーブルに基づいています。私は244797に関連するすべてのidとidによって報告されたidとIDのIDを – phpdev

答えて

0

これはトリックを行う必要がある出力私が望むようになっています
PDO機能の代わりのため廃止されましたmysql_*の機能を使用することを検討してくださいセキュリティ上の理由から質問の所有者が尋ねたよう

<?php 

function getReports($contactId, $pdo) 
{ 
    $reports = []; 
    $stmt = $pdo->prepare(
     'SELECT contactid as id, firstname, lastname, reportsto '. 
     'FROM contactdetails '. 
     'WHERE reportsto = :contactId'); 
    $stmt->bindValue(':contactId', $contactId); 
    $stmt->execute(); 
    $rows = $stmt->fetchAll(PDO::FETCH_ASSOC); 

    foreach ($rows as $row) { 
     $reports = array_merge($reports, [$row], getReports($row['id'], $pdo)); 
    } 

    return $reports; 
} 

$pdo = new \PDO('mysql:host=HOSTNAME;dbname=DBNAME', 'USERNAME', 'PASSWORD'); 

$contactId = $_SESSION['customer_id']; 
$reports = getReports($contactId, $pdo); 


は、mysql_*バージョンそれはをお勧めしません(と仮定するとコネクションが既に開いている)が、を覚えている:

<?php 

function getReports($contactId) 
{ 
    $reports = []; 
    $res = mysql_query(
     'SELECT contactid as id, firstname, lastname, reportsto '. 
     'FROM contactdetails '. 
     'WHERE reportsto = "' . mysql_real_escape_string($contactId) . '"'); 

    if (mysql_num_rows($res) > 0) { 
     while($row = mysql_fetch_assoc($res)) { 
      $reports = array_merge($reports, [$row], getReports($row['id'])); 
     } 
    } 

    return $reports; 
} 

$contactId = $_SESSION['customer_id']; 
$reports = getReports($contactId); 
+0

もし私がmysqlを使用していたら、あなたのコードの見た目はお返事ください。 – phpdev

+0

@phpdev編集が完了しました – jbrtrnd

+1

* "私はmysqlを使用しています..." *:あなた***は***から移動する必要があります。それは難しい作業ではありません。 – trincot