开发者

List object properties delimited by comma in CodeIgniter with DataMapper OverZealous Edition

I have 2 classes - Students and Gr开发者_开发问答oups with many-to-many relationship. On a student page, I want to show all his details and list all groups he belongs to, delimited by comma. This is my Students controller:

class Students extends Controller {

  function  __construct() {
      parent::__construct();
  }

  function index() {
      $this->get_all_students();
  }

  function get_all_students() {
      $s = new Student();
      $data['students'] = $s->select('id, name, email')->get();

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

  function view($id) {
      $s = new Student();
      $s->get_by_id($id);
      $s->groups->get();

      $data['student'] = $s;

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

I can get student's details like this in student_view:

Name: <?php echo $student->name; ?>
E-mail: <?php echo $student->email; ?>
Groups: 
<?php foreach ($student->groups as $group) : ?>
  <?php echo anchor("/groups/$group->id", $group->name) ?>
<?php endforeach; ?>

So, how can I list groups delimited by comma? I tried adding group names to an array in the controller and then just <?php echo implode(', ', $groups); ?> in the view. But this way I cannot make a link using group IDs.


<?php echo anchor("/groups/$group->id", $group->name) ?> 

should become ( see the comma at the end of the line )

<?php echo anchor("/groups/$group->id", $group->name) ?> ,

or display user groups as a list :

<?php foreach ($student->groups as $group) : ?>
  <?php echo anchor("/groups/$group->id", $group->name) ?>
<?php endforeach; ?>

should become

<ul>
<?php foreach ($student->groups as $group) : ?>
  <li><?php echo anchor("/groups/$group->id", $group->name) ?></li>
<?php endforeach; ?>
</ul>


<?php
  $first = true;
  foreach ($student->groups as $group) :
    if (! $first) echo ', ';
    echo anchor("/groups/$group->id", $group->name);
    $first = false;
  endforeach;
?>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜