0

I'm selecting all team names from the team database table. Here I want to display team name in one html table. And using this team_id I need to select all team members (another database table) based on this team_id.

Controller

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

Based on this json result how to fetch all team name and to pass each team_id to another modal function to get the team members belongs to each team??

sethu
  • 49
  • 6

1 Answers1

0

Add next inside your foreach statement

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

The second part is your view:

<?php if(!empty($team_name)){ foreach($team_name as $tname){?>
        <div class="content">
        <form id="jobteam" name="jobteam[]" action="#" data-id="<?php echo $tname['team_id']; ?>" role="form" autocomplete="off">
        <div class="row">
    <div class="col-md-12">
      <div class="card card-user">
        <div class="card-header">
            <h4 class="dropdate headteam<?php echo $tname['team_id'];?>"><?php echo $tname['team_name'];?></h4>                         
        <div class="form-group row">
        <label for="TeamName" class="col-sm-2 col-form-label cdropdate">Team Name</label>
        <div class="col-sm-10">
          <input class="form-control cdropdate teamname" 
          id="teamname<?= $tname['team_id'];?>" type="text" data-id="<?php echo $tname['team_id'];?>" 
          name="teamname" value="<?php echo $tname['team_name'];?>"> <br/>

           <table id="teammembertable" class="table table-striped table-bordered" style="width:100%">
          <thead>
             <tr>
                <th>Staff name</th>
                <th>Staff type</th>
                <th>Action</th>
             </tr>
          </thead>
          <tbody>
        <?php $memb = json_decode(json_encode($tname['team_members']), True);
            foreach($memb as $member){

            if (!empty($member['Staff_name'])) { ?>

             <tr>
                <td class="teamstaff_member"><?= $member['Staff_name'];?></td>
                <td class="teamstafftype_member"><?= $member['Staff_type']; ?></td>
                <td class="teamstafftype_member"><?= $member['Action']; ?></td>
             </tr> 
            <?php } else{ ?>
             <tr>
             <td></td>
             <td>No records found.</td>
             <td></td>                                    
             </tr>
         <?php }} ?>

          </tbody>
          <tfoot>
             <tr>
             </tr>
          </tfoot>
       </table>
           <div id="container-fluid"  class="float-right">
      <!-- for contract update button -->
      <div class="">
        <button type="button" id="saveteammembers" class="btn btn-success saveteammembers cdropdate">Save</button>
       <!-- <button type="button" id="backto_headcontract" class="btn btn-success">Back</button>-->
      </div>
    </div>
        </div>
      </div>
      </div>
      </div>
      </div>
      </div>

      </form>
      </div>
      <?php } } else{ ?>
      <p>No records found.</p>
      <?php } ?>

I can't see your full new json, and I've used it as an array, but, if it looks like this it should work fine and you will replace <?= $memb['blabla']?> on $memb->blabla:

$team_name = [
   [
     "team_id" => 1,
     "team_name" => "Water tank clean",
     "team_members" => [
                [
                "team_id" => 2,
                "Staff_id" => 14,
                "Staff_type" => "Leader", 
                "Staff_name" => "nimisha", 
                "Action" => "eat"
                ],
                [
                "team_id" => 2,
                "Staff_id" => 15,
                "Staff_type" => "Player", 
                "Staff_name" => "misha", 
                "Action" => "play"
                ]
              ]
   ],
    [
     "team_id" => 2,
     "team_name" => "Water tank clean",
     "team_members" => [
                [
                "team_id" => 2,
                "Staff_id" => 14,
                "Staff_type" => "Leader", 
                "Staff_name" => "nimisha", 
                "Action" => "eat"
                ],
                [
                "team_id" => 2,
                "Staff_id" => 15,
                "Staff_type" => "Player", 
                "Staff_name" => "misha", 
                "Action" => "play" 
                ]
              ]
   ]  
];

Now I'll try to make it work with your json.

What if you will send the only $cabang as $data? Without json_encode command? Do next, replace:

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

on

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

And use my version.


II.

public function create_team_memb()
    {
        $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);
         }

         $selectallstaff= json_decode(json_encode($data['selectallstaff']), True);  

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

    }

View

    <select class="form-control" id="" name="">
        <option value="">Select</option>
        <?php 

          foreach($selectallstaff as $staffname)
          { ?>
        <option value="<?= $staffname['Staff_id']; ?>"><?= $staffname['Staff_name']; ?></option>
        <?php }
          ?>
    </select>
Aksen P
  • 4,564
  • 3
  • 14
  • 27
  • Plz check my full json result.In my view page first team name only showing and error showing as Fatal error: Uncaught Error: Cannot use object of type stdClass as array in ( here = $member['Staff_name'];?>) – sethu Jul 30 '19 at 12:10
  • Did you tried removing `json_encode` and rewriting your `load->view`? – Aksen P Jul 30 '19 at 12:13
  • Nop only team name first only showing and inside that error showing – sethu Jul 30 '19 at 12:19
  • Fatal error: Uncaught Error: Cannot use object of type stdClass as array in C:\inetpub\wwwroot\ci_into\application\views\team_memb_creation.php:229 Stack trace: #0 – sethu Jul 30 '19 at 12:19
  • Show me your `print_r($cabang)`... It works for me, weird – Aksen P Jul 30 '19 at 12:21
  • ah, I know, rewrite `view` like `foreach($memb as (array)$member){?>` – Aksen P Jul 30 '19 at 12:27
  • Parse error: syntax error, unexpected '(array)' (array) (T_ARRAY_CAST) in C:\inetpub\wwwroot\ci_into\application\views\team_memb_creation.php on line 226 – sethu Jul 30 '19 at 12:31
  • you need to [conver stdClass into array](https://stackoverflow.com/questions/19495068/convert-stdclass-object-to-array-in-php) – Aksen P Jul 30 '19 at 12:32
  • Try to rollback your `foreach` and edit this as `$memb = (array)$tname['team_members'];` or `$memb = json_decode(json_encode($tname['team_members']), True);` – Aksen P Jul 30 '19 at 12:38
  • ya added $memb = json_decode(json_encode($tname['team_members']), True); working now – sethu Jul 30 '19 at 12:43
  • but in team 4 have no record of team_members..Here showing error as Severity: Warning Message: Illegal string offset 'Staff_name – sethu Jul 30 '19 at 12:44
  • if record is not there why its showing illegal string offsetwarning – sethu Jul 30 '19 at 12:49
  • it's all ok now? – Aksen P Jul 30 '19 at 12:59
  • I have a another dought in this code. In my controller function,already i am fetching data $data['selectallstaff'] = $this->mastertable_model->selectallstaff(); How to pass this to view page.Now its not passing any values to viewpage – sethu Jul 31 '19 at 00:04
  • I tried to pass like this $selectallstaff= json_encode($data['selectallstaff']); $this->load->view('team_memb_creation', ['team_name' => $cabang, 'selectallstaff'=>$selectallstaff ] ); – sethu Jul 31 '19 at 03:06
  • Hi, show me `print_r($data['selectallstaff'])` and one more after `print_r($selectallstaff)`. Perhaps it will needs some action as `$memb` had – Aksen P Jul 31 '19 at 06:08
  • Try to use this construction as well `$selectallstaff= json_decode(json_encode($data['selectallstaff']), True);` – Aksen P Jul 31 '19 at 07:09
  • i Have updated the code with print_r($data['selectallstaff']).Plz check it – sethu Jul 31 '19 at 07:11
  • You see `stdClass Object` as it was before, with `$memb`, try to use the same solution – Aksen P Jul 31 '19 at 07:13
  • when i use $selectallstaff= json_decode(json_encode($data['selectallstaff']), True); its showing error Undefined variable: data in view page – sethu Jul 31 '19 at 07:16
  • You need to use it in php, and then `$this->load->view('team_memb_creation', ['team_name' => $cabang, 'selectallstaff'=>$selectallstaff ] );`. After, in view, you need to use another cunstruction for to get any values. Like `$selectallstaff['blabla']`. Not like `$selectallstaff->blabla` – Aksen P Jul 31 '19 at 07:18
  • No, `$selectallstaff= json_decode(json_encode($data['selectallstaff']), True);` – Aksen P Jul 31 '19 at 07:20
  • It's all ok now? – Aksen P Jul 31 '19 at 07:33
  • Make, please, a new question, it's question for `javascript`/`jquery`. Show the only ` – Aksen P Jul 31 '19 at 07:39
  • I see, ok, will take a look. – Aksen P Jul 31 '19 at 08:22
  • If it is possible,can u plz reply to this question??https://stackoverflow.com/questions/57131182/how-to-check-same-id-have-multiple-values-in-another-column – sethu Jul 31 '19 at 13:53
  • Plz reply me if u have any idea – sethu Jul 31 '19 at 15:33
  • I'm working with `Oracle DB` and I'm sending data via packages, not like you, sorry – Aksen P Jul 31 '19 at 18:39