0

I have this function in my controller:

public function actionResource()
{
    $resourcegrporgs = Organization::find()->where(['groupid' => '1'])->all();
    $resmembers = '';
    foreach ($resourcegrporgs as $resourcegrporg) {
        $resmembers = $resourcegrporg->getMembers()->all();
    }
    return $this->render('resource', [
        'resourceMembers' => $resmembers,
        'resourceGroups' => $resourcegrporgs,
    ]);
}

and in my view i try to get the members for each group as follows:

    <?php foreach ($resourceGroups as $resourceGroup): ?>
<section class="team">
  <div class="container">
    <div class="row">
      <div class="col-md-10 col-md-offset-1">
        <div class="col-lg-12">
          <h4 class="description"><?= $resourceGroup->name ?></h4>
          <div class="row pt-md">
          <?php foreach ($resourceMembers as $resourceMember): ?>
            <div class="col-lg-3 col-md-3 col-sm-4 col-xs-12 profile">
              <div class="img-box">
                <img src=<?= $resourceMember->picture ?> class="img-responsive">
                <ul class="text-center">
                  <a href="#"><li><i class="fa fa-facebook"></i></li></a>
                  <a href="#"><li><i class="fa fa-twitter"></i></li></a>
                  <a href="#"><li><i class="fa fa-linkedin"></i></li></a>
                </ul>
              </div>
              <h1><?= Html::a(Html::encode($resourceMember->name),['memberdetail', 'id' => $resourceMember->id]) ?></h1>
              <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit.</p>
            </div>
          <?php endforeach; ?>
          </div>
        </div>
      </div>
    </div>
  </div>
</section>
<?php endforeach; ?>

And here is the organization model;

class Organization extends \yii\db\ActiveRecord
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'organization';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['name', 'active', 'groupid'], 'required'],
            [['active', 'groupid'], 'integer'],
            [['lastdatemodified'], 'safe'],
            [['name', 'logo', 'userid'], 'string', 'max' => 100]
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'name' => 'Name',
            'logo' => 'Logo',
            'active' => 'Active',
            'userid' => 'Userid',
            'lastdatemodified' => 'Lastdatemodified',
            'groupid' => 'Groupid',
        ];
    }

    /**
     * @return \yii\db\ActiveQuery
     */
    public function getMembers()
    {
        return $this->hasMany(Member::className(), ['organizationid' => 'id']);
    }

    /**
     * @return \yii\db\ActiveQuery
     */
    public function getGroup()
    {
        return $this->hasOne(Group::className(), ['id' => 'groupid']);
    }
}

However, the foreach loop for the members is returning the members of the last group for every other group thus my question:
How can I fix it to render the members of each group individually?

6
  • Why do you have the foreach loop in your Resource action? You are just overwriting that variable each time. When it is passed to your view it's just the last batch of members. Commented Mar 15, 2016 at 0:01
  • yeah i noticed that later on...should i rather ommit the foreach at the controller level and just use it in the view? Commented Mar 15, 2016 at 0:08
  • i need to find a way to get the members for each group individually...that's why i first thought about the foreach in the resource controller Commented Mar 15, 2016 at 0:14
  • Please show the code for your Organization model. I have an idea what it is, but before I fix your code I want to make sure. Commented Mar 15, 2016 at 0:18
  • ok..i will edit the question and add the organization model to it Commented Mar 15, 2016 at 0:24

1 Answer 1

1

Unless I'm missing something this is rather easy. Just change your action to the following:

public function actionResource()
{
    $resourcegrporgs = Organization::find()->where(['groupid' => '1'])->all();
    return $this->render('resource', [
        'resourceGroups' => $resourcegrporgs,
    ]);
}

And then in your view, the inner foreach loop should simply be this:

<?php foreach ($resourceGroup->members as $resourceMember): ?>
    <div class="col-lg-3 col-md-3 col-sm-4 col-xs-12 profile">
        <div class="img-box">
            <img src=<?= $resourceMember->picture ?> class="img-responsive">
            <ul class="text-center">
                <a href="#"><li><i class="fa fa-facebook"></i></li></a>
                <a href="#"><li><i class="fa fa-twitter"></i></li></a>
                <a href="#"><li><i class="fa fa-linkedin"></i></li></a>
            </ul>
        </div>
        <h1><?= Html::a(Html::encode($resourceMember->name),['memberdetail', 'id' => $resourceMember->id]) ?></h1>
        <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit.</p>
    </div>
<?php endforeach; ?>
Sign up to request clarification or add additional context in comments.

1 Comment

thanks a lot...i'm fairly new to yii2 i didn't know it could be done that way...well i really appreciate the help...i was stuck on that for 2 days

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.