how to get each user list in array

Viewed 152
public function getChildren($parent, $level=0,$getlevel=array()) { 

    $criteria = new CDbCriteria;
    $criteria->condition='referred_by_user_id=:id';
    $criteria->params=array(':id'=>$parent);
    $count = array(0=>0);
    $model = $this->findAll($criteria);

    // $levelcount = array();
    $user=User::model()->findbypk(Yii::app()->user->id);
    if($user->level!=$level)
    {
         foreach ($model as $key) {
            $count[0]++;            
            $index=1;
            // echo str_repeat(' — ', $level) . $key->name . "<br />";
            $children= $this->getChildren($key->id, $level+1);
            // pr($children);die;
            foreach ($children as $child)
            {
                // pr($child);
                if ($child==0)
                  continue;
                if (isset($count[$index]))
                  $count[$index] += $child;
                else    
                  $count[$index] = $child;
                $index++;
            }
        }
    }
   return $count; 
}

I can output only the number of users. How do I get the user id for each level in an array?

2 Answers

I think the problem is you must make $index variable outside of for

I made something similiar a while ago:

public static function getChildren($idParent) {
    $elem      = [];
    $childrens = Arbol::model()->findAll("estado = 1 and id_parent = " . $idParent . ' ORDER BY level');
    foreach ($childrens as $key => $child) {
        $persona = Persona::model()->findbyPk($child->id_child);
        if ((int) $persona->id_superior === $idParent) {
            $nombre     = explode(" ", $persona->nombres);
            $elem[$key] = [
                "id"    => $persona->id_persona,
                "image" => Yii::app()->baseUrl . '/images/user-icon' . ($persona->pago_inscripcion ? '-activo.png' : '.png'),
                "text"  => [
                    'title' => $nombre[0] . ' ' . substr($persona->apellido_paterno, 0, 1) . '.'
                ]
            ];
        } else {
            $keySUperior = array_search($persona->id_superior, array_column($elem, "id"));
            if (is_int($keySUperior)) {
                $elem[$keySUperior]["children"] = self::getChildren((int) $persona->id_superior);
            }
        }
    }

    return $elem;
}

I hope i give you any help

Related