数组中的递归函数数据不符合预期

时间:2016-08-26 07:24:19

标签: php mysql arrays recursion

我想使用递归显示一些结果 首先是我的表结构

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

这是我的联系人,其中存储了所有联系人 现在我想要当contactid 244797登录时,显示所有显示244477报告的ID,以及报告任何与244797相关的id的联系人ID,例如448和542,因为它报告给438,543报告给542(如果有的话)如果有人报告不同的手段没有联系id循环终止和所有联系人存储在一个数组。 这是我的PHP代码

$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);

当我print_r($ printres)然后我只收到报告给244797的id,请检查我的代码并评估我的问题谢谢。 我收到的输出看起来像

 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
    )

   )

1 个答案:

答案 0 :(得分: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);