猿问

如何分别获取json结果以调用不同的模态函数?

我正在从团队数据库表中选择所有团队名称。在这里,我想在一个 html 表中显示团队名称。使用这个team_id我需要根据这个选择所有团队成员(另一个数据库表)team_id。


控制器


    public function create_team_memb()

    {

        //$data['memb'] = $this->mastertable_model->get_team_member();


        $data['team_name']         = $this->mastertable_model->get_team_name();

        $data['selectallstaff'] = $this->mastertable_model->selectallstaff();

        $cabang = array();

        foreach($data['team_name']->result() as $list){

          $item = array();

          $item['team_id'] = $list->team_id;

          $item['team_name'] = $list->team_name;

          $item['team_members'] = $this->mastertable_model->get_team_member($item['team_id']);

          array_push($cabang,$item);

         }

        //$json = json_encode($cabang); 

        //echo $json;

        $this->load->view('team_memb_creation', ['team_name' => $cabang]);

        //$this->load->view('team_memb_creation', $data);

    }

基于这个 json 结果如何获取所有团队名称并将每个 team_id 传递给另一个模态函数以获取团队成员属于每个团队?


隔江千里
浏览 162回答 1
1回答

一只名叫tom的猫

在 foreach 语句中添加 next$item['team_members'] = $this->mastertable_model->get_team_member($item['team_id']);第二部分是你的观点:<?php if(!empty($team_name)){ foreach($team_name as $tname){?>&nbsp; &nbsp; &nbsp; &nbsp; <div class="content">&nbsp; &nbsp; &nbsp; &nbsp; <form id="jobteam" name="jobteam[]" action="#" data-id="<?php echo $tname['team_id']; ?>" role="form" autocomplete="off">&nbsp; &nbsp; &nbsp; &nbsp; <div class="row">&nbsp; &nbsp; <div class="col-md-12">&nbsp; &nbsp; &nbsp; <div class="card card-user">&nbsp; &nbsp; &nbsp; &nbsp; <div class="card-header">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <h4 class="dropdate headteam<?php echo $tname['team_id'];?>"><?php echo $tname['team_name'];?></h4>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; <div class="form-group row">&nbsp; &nbsp; &nbsp; &nbsp; <label for="TeamName" class="col-sm-2 col-form-label cdropdate">Team Name</label>&nbsp; &nbsp; &nbsp; &nbsp; <div class="col-sm-10">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <input class="form-control cdropdate teamname"&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; id="teamname<?= $tname['team_id'];?>" type="text" data-id="<?php echo $tname['team_id'];?>"&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; name="teamname" value="<?php echo $tname['team_name'];?>"> <br/>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<table id="teammembertable" class="table table-striped table-bordered" style="width:100%">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <thead>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<tr>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <th>Staff name</th>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <th>Staff type</th>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <th>Action</th>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;</tr>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </thead>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <tbody>&nbsp; &nbsp; &nbsp; &nbsp; <?php $memb = json_decode(json_encode($tname['team_members']), True);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; foreach($memb as $member){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (!empty($member['Staff_name'])) { ?>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<tr>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td class="teamstaff_member"><?= $member['Staff_name'];?></td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td class="teamstafftype_member"><?= $member['Staff_type']; ?></td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td class="teamstafftype_member"><?= $member['Action']; ?></td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;</tr>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <?php } else{ ?>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<tr>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<td></td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<td>No records found.</td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<td></td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;</tr>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<?php }} ?>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </tbody>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <tfoot>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<tr>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;</tr>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </tfoot>&nbsp; &nbsp; &nbsp; &nbsp;</table>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<div id="container-fluid"&nbsp; class="float-right">&nbsp; &nbsp; &nbsp; <!-- for contract update button -->&nbsp; &nbsp; &nbsp; <div class="">&nbsp; &nbsp; &nbsp; &nbsp; <button type="button" id="saveteammembers" class="btn btn-success saveteammembers cdropdate">Save</button>&nbsp; &nbsp; &nbsp; &nbsp;<!-- <button type="button" id="backto_headcontract" class="btn btn-success">Back</button>-->&nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; </div>&nbsp; &nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; &nbsp; </form>&nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; &nbsp; <?php } } else{ ?>&nbsp; &nbsp; &nbsp; <p>No records found.</p>&nbsp; &nbsp; &nbsp; <?php } ?>我看不到你完整的新 json,我已经将它用作数组,但是,如果它看起来像这样它应该可以正常工作,你将替换<?= $memb['blabla']?>为$memb->blabla:$team_name = [&nbsp; &nbsp;[&nbsp; &nbsp; &nbsp;"team_id" => 1,&nbsp; &nbsp; &nbsp;"team_name" => "Water tank clean",&nbsp; &nbsp; &nbsp;"team_members" => [&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "team_id" => 2,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "Staff_id" => 14,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "Staff_type" => "Leader",&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "Staff_name" => "nimisha",&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "Action" => "eat"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ],&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "team_id" => 2,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "Staff_id" => 15,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "Staff_type" => "Player",&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "Staff_name" => "misha",&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "Action" => "play"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ]&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ]&nbsp; &nbsp;],&nbsp; &nbsp; [&nbsp; &nbsp; &nbsp;"team_id" => 2,&nbsp; &nbsp; &nbsp;"team_name" => "Water tank clean",&nbsp; &nbsp; &nbsp;"team_members" => [&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "team_id" => 2,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "Staff_id" => 14,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "Staff_type" => "Leader",&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "Staff_name" => "nimisha",&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "Action" => "eat"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ],&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "team_id" => 2,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "Staff_id" => 15,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "Staff_type" => "Player",&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "Staff_name" => "misha",&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "Action" => "play"&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ]&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ]&nbsp; &nbsp;]&nbsp;&nbsp;];现在,我将尝试使其与您的 json 一起使用。如果您将发送唯一的$cabangas$data怎么办?没有json_encode命令?做下一步,替换:$this->load->view('team_memb_creation', $data);&nbsp;在$this->load->view('team_memb_creation', ['team_name' => $cabang]);并使用我的版本。二、public function create_team_memb()&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; $data['team_name'] = $this->mastertable_model->get_team_name();&nbsp; &nbsp; &nbsp; &nbsp; $data['selectallstaff'] = $this->mastertable_model->selectallstaff();&nbsp; &nbsp; &nbsp; &nbsp; $cabang = array();&nbsp; &nbsp; &nbsp; &nbsp; foreach($data['team_name']->result() as $list){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $item = array();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $item['team_id'] = $list->team_id;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $item['team_name'] = $list->team_name;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $item['team_members'] = $this->mastertable_model->get_team_member($item['team_id']);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; array_push($cabang,$item);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;$selectallstaff= json_decode(json_encode($data['selectallstaff']), True);&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;$this->load->view('team_memb_creation', [&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;'team_name'&nbsp; &nbsp; &nbsp; => $cabang,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;'selectallstaff' => $selectallstaff&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;]);&nbsp; &nbsp; }看法&nbsp; &nbsp; <select class="form-control" id="" name="">&nbsp; &nbsp; &nbsp; &nbsp; <option value="">Select</option>&nbsp; &nbsp; &nbsp; &nbsp; <?php&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; foreach($selectallstaff as $staffname)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; { ?>&nbsp; &nbsp; &nbsp; &nbsp; <option value="<?= $staffname['Staff_id']; ?>"><?= $staffname['Staff_name']; ?></option>&nbsp; &nbsp; &nbsp; &nbsp; <?php }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ?>&nbsp; &nbsp; </select>
随时随地看视频慕课网APP
我要回答