【问题标题】:how to to obtain a determine values from a foreach如何从 foreach 中获取确定值
【发布时间】:2019-05-12 19:54:13
【问题描述】:

我正在创建用户树以将其保存在 .json 文件中,但在二级用户中找不到读取第三方用户的方法

通过 mysql 的二级用户读取我的 foreach,但它没有将它们与第三个用户对齐 我的桌子是

1.(用户名=juanreferedby=none)

2.(用户名=josereferedby=juan)

3.(用户名=albertoreferedby=juan)

4.(用户名=fernandoreferedby=jose)

`` php


$stmt = $mysqli->prepare("SELECT username FROM affiliateuser WHERE referedby = '$actualuser'");
$stmt->execute();
$array = [];
foreach ($stmt->get_result() as $row)
{
    $referedby[] = $row['username'];

}
$string = '';
$string2 = '';
foreach ($referedby as $key => $secundaryusers){
}` ``

我希望结果能给我类似的东西。

    { "name": "juan ", "children": [ { "name ": "jose", "children": [{ "name": "fernando", "children": [] }] } { "name": "alberto", "children": [] } ] },

【问题讨论】:

  • 欢迎来到stackoverflow。请描述您的表格并解释它们之间的关系。不清楚您所说的“二级用户”和“第三方”用户是什么意思。
  • 是一个例子,因为我需要通过[foreach]创建他们的“孩子”和他们的“孩子”的孩子来读取他们的孩子和他们的“孙子”
  • 编辑您的问题并改进解释。为您的表提供一些数据示例。我们很乐意提供帮助,但您必须先帮助自己。
  • @ryantxr 更好?
  • 使用DESC affiliateuser 描述您的表格并将其添加到问题中。然后添加一些示例数据并将其放入问题中。

标签: php mysql php-7


【解决方案1】:

这里的想法是创建一个 PHP 结构,该结构具有 JSON 中存在的数据,然后使用 json_encode() 将该结构转换为 JSON。

如果您查看class User,它代表一个用户和所有后代。 如果我可以用所有数据填充它,那么将其转换为 JSON 很容易。

请注意,表中的每个用户都有一个父级,该父级存储在列referredby_id 中。这是父用户的主键。 只要保证表中的用户名是唯一的,您就可以将其更改为用户名。 为此,请将referredby_id 列的类型更改为VARCHAR。如果数据量大,则索引用户名表。

表:

CREATE TABLE `affilitateuser` (
  `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
  `username` varchar(40) DEFAULT NULL,
  `referredby_id` int(11) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

数据:

INSERT INTO `affilitateuser` (`id`, `username`, `referredby_id`) VALUES
(1, 'sarah', NULL),
(2, 'james', 1),
(3, 'tom', 2),
(4, 'natalie', 3),
(5, 'juan', NULL),
(6, 'jose', 5),
(7, 'alberto', 5),
(8, 'fernando', 5),
(9, 'camila', 8),
(10, 'sean', 9),
(11, 'scotty', 9),
(12, 'robert', 9),
(13, 'montgomery', 12),
(14, 'jessie', 13),
(15, 'cole', 13),
(16, 'cary', 14),
(17, 'porter', 14),
(18, 'sandra', 5),
(19, 'lily', 6);

代码:

// A class to represent nodes on a tree
class User
{
    public $name;
    public $children = [];
    public function __construct($name)
    {
        $this->name = $name;
    }
    // Add a child to this User
    public function addChild($name)
    {
        $u = new User($name);
        $this->children[] = $u;
        // return the newly created object so we can use it later.
        return $u;
    }
}

// Class that does the extracting
class UserTreeExtractor
{
    protected $conn; // keep the database connection

    public function run()
    {
        $this->connect();
        // Extract Juan's tree
        // print_r($this->tree(5));
        // Save the JSON to a string
        $jsonString = json_encode($this->tree(5), JSON_PRETTY_PRINT);
        // Write it out to a file
        file_put_contents('output.json', $jsonString);
    }
    // { "name": "juan ", "children": [ { "name ": "jose", "children": [{ "name": "fernando", "children": [] }] } { "name": "alberto", "children": [] } ] },

    /**
     * Gets the children and downstream descendants for a user
     */
    protected function tree($id)
    {
        // First, get the user
        $sql1 = "SELECT username FROM affilitateuser WHERE id = {$id}";
        $stmt = $this->conn->prepare($sql1);
        if ( ! $stmt ) {
            die('query failed');
        }
        $stmt->execute();
        $top = $stmt->get_result()->fetch_assoc();
        // print_r($top); exit();

        // Now get the all descendents
        $sql = "SELECT  id, username, referredby_id 
        FROM    (SELECT * FROM affilitateuser
        ORDER BY referredby_id, id) users_sorted,
        (SELECT @pv := '{$id}') initialisation
        WHERE   find_in_set(referredby_id, @pv)
        AND     LENGTH(@pv := CONCAT(@pv, ',', id))";
        // "SELECT username FROM `affiliateuser` WHERE referedby_id = {$id}"
        $stmt = $this->conn->prepare($sql);
        $stmt->execute();
        $children = [];
        $tree = new User($top['username']);
        // Keep an index of where the objects are stored
        // so we can find them later to attach their children.
        $index[$id] = $tree;
        $parent = null;
        foreach ($stmt->get_result() as $row)
        {
            if ( isset($index[$row['referredby_id']]) ) {
                $new = $index[$row['referredby_id']]->addChild($row['username']);
                $index[$row['id']] = $new; // put the new user into the index
            } else {
                // referred by some user that does not exist
                die("Referred by non-existent user");
            }
            $children[] = ['username' => $row['username'], 'id' => $row['id'], 'referredby_id' => $row['referredby_id']];
        }
        return $tree;
    }
    // Connect to the database
    protected function connect()
    {
        // Change the connection credentials as needed.
        $this->conn = mysqli_connect("127.0.0.1", "app", "aaaa", "sss"); 
        if( ! $this->conn ) { 
            die("Database Connection Failed: ".mysql_error()); 
        }
    }
}

$obj = new UserTreeExtractor;
$obj->run();

【讨论】:

  • 一个java脚本?如何使用 ?我只知道很多php ...不存在填充用户名和从数据库中提取的用户名引用的方法将json文件放入代码中 $string2 = "{\"name\": \"$actualuser\", \ "儿童\": [$string5]}"; $string .= "{ \"name\": \"$value\", \"children\": [] }, ";
  • 这不是 JavaScript。这是 PHP。
  • 我现在修复它...如何保存到 json 文件?
  • 你可以给你发电子邮件或脸书吗?和你交流?我永远不会忘记这一点
  • 如何将 refferedv_id 更改为用户名?
猜你喜欢
  • 2018-02-17
  • 2021-10-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-02-11
  • 2012-08-25
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多