【问题标题】:Recursive function data in array not comes as expected数组中的递归函数数据未按预期出现
【发布时间】:2016-08-26 07:24:18
【问题描述】:

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

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登录时显示所有向244797报告的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
    )

   )

【问题讨论】:

  • 您的陈述不清楚。你能再解释一下吗。可能基于输入您想要的预期输出,这将更清楚
  • 你希望你的结果数组是什么样子的?显示所需的输出以及您在此处获得的输出。
  • 亲爱的@Anant 基于表我想要所有与 244797 相关的 id 及其由 id 报告的 id 以及由 id 报告的 id
  • 我非常怀疑显示的当前输出是真实的。没有任何键的数组元素(无论是生成的还是手动分配的)都是主要错误,这就是您的输出所暗示的。
  • @MarcinOrlowski 先生来自我的代码输出就像你看到的,如果有任何错误请更正我的代码

标签: php mysql arrays recursion


【解决方案1】:

这应该可以解决问题:
请考虑使用 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_* 版本(假设 connexion 已经打开),但请记住 不推荐

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

【讨论】:

  • 如果我使用 mysql 那么你的代码应该是什么样的请回复
  • “如果我使用 mysql...”:那么你现在应该远离它。这不是一项艰巨的任务。
  • $reports = array_merge($reports, [$row], getReports($row['id']));什么是 [$row]
  • 我没有找到 id 向 438 报告的联系人
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-09-03
  • 2018-12-05
  • 1970-01-01
  • 1970-01-01
  • 2013-02-24
  • 2012-01-21
  • 2016-04-08
相关资源
最近更新 更多