1. The forums will be archived and moved to a read only mode in about 2 weeks (mid march).

Foreach all player in dropdown and result

Discussion in 'Development' started by armagadon159753, Mar 17, 2018.

  1. armagadon159753

    armagadon159753 Zombie

    Messages:
    217
    GitHub:
    armagadon159753
    In this code, normally i must get all player online name but they won't work
    on the 3 player online the DropDown only shows 1
    PHP:
    if($data === 2){
                        
    $pk = new ModalFormRequestPacket();
                        
    $form = array();
                        foreach(
    $this->getServer()->getOnlinePlayers() as $online){
                            
    $name $online->getName();
                        }
                        
    $form["title"] = "Members";
                        
    $form["type"] = "custom_form";
                        
    $form["content"] = [["type" => "dropdown""text" => "Selectionez le joueur que vous vouliez rejoindre""options" => ["{$name}"]]];
                        
    $pk->formId 40;
                        
    $pk->formData json_encode($form);
                        
    $player->dataPacket($pk);
                    }
                }
                if(
    $id === 40){
                    if(
    $data === Null){
                        
    $player->sendMessage("null");
                    }
                    if(
    $data === 0){
                        
    $player->sendMessage("0");
                    }
                    if(
    $data === 1){
                        
    $player->sendMessage("1");
                    }
                    if(
    $data === 2){
                        
    $player->sendMessage("2");
                    }
                    if(
    $data === 3){
                        
    $player->sendMessage("3");
                    }
     

    Attached Files:

  2. XdmingXD

    XdmingXD Baby Zombie

    Messages:
    158
    GitHub:
    xdmingxd
    You didn't store all the $name?
     
  3. armagadon159753

    armagadon159753 Zombie

    Messages:
    217
    GitHub:
    armagadon159753
    Wath you mean by store
     
  4. MalakasPlayzMCPE

    MalakasPlayzMCPE Zombie Pigman

    Messages:
    667
    Why
    PHP:
    $name $online->getName();
    There is not only a player to use that...
     
  5. armagadon159753

    armagadon159753 Zombie

    Messages:
    217
    GitHub:
    armagadon159753
    Look at screenshot
     
  6. TheDragonRing

    TheDragonRing Witch

    Messages:
    64
    GitHub:
    dragonwocky
    Simple enough. With this, you're redefining the name to a new name every time:
    PHP:
    foreach($this->getServer()->getOnlinePlayers() as $online){
      
    $name $online->getName();
    }
    Instead, create an array, and push each name to the array, like so:
    PHP:
    $name = array();
    foreach(
    $this->getServer()->getOnlinePlayers() as $online){
      
    array_push($name$online->getName());
    }
    Then, if there are three players online with the names 'John', 'Fred' and 'George', $name will end up equalling the array ['John', 'Fred', 'George']

    OPTIONALLY

    If you just want the names as a string, you could add each new name to its own line, like so:
    PHP:
    $name '';
    foreach(
    $this->getServer()->getOnlinePlayers() as $online){
      
    $name .= '\n' $online->getName();
    }
    Other devs please correct me if I'm wrong. Haven't messed with PHP for a while.
     
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.