dtz30833 2014-03-08 00:20
浏览 28
已采纳

创建codeigniter表单选择

So, I am making a from select with the code below:

// Controller Code 
$data['message_to_options'] = array(
   '1' => 'username 1',
   '2' => 'username 2',
);

// View Code 
<?php echo form_dropdown('message_to', $message_to_options); ?>

This all works great. Except the users needs to be dynamically created not hard coded.

I have the following code from ion_auth for code igniter. This gets all the users in the system. Which Is what I want to do.

$users = $this->ion_auth->users()->result();

I just need a way to put them all together. I tried the following but it did not function correctly.

$users = $this->ion_auth->users()->result();

$data['message_to_options'] = array(
   foreach($users $user){
     '$user['id']' => '$user['username']',
   }
);

I didn't think it would work but I figured I would give it a shot. What is the PHP AND Code igniter "legal" way of doing this?

UPDATE

So when I do run this code, I receive this error:

Parse error: syntax error, unexpected 'foreach' (T_FOREACH), expecting ')'
  • 写回答

3条回答 默认 最新

  • dongxin5429 2014-03-08 01:39
    关注

    This is what I ended up doing:

    $users = $this->ion_auth->users()->result();
    
    $data['message_to_options'] = array();
    
    foreach ($users as $user) {
       $userID = $user->id;
       $username = $user->username;
       $data['message_to_options'][$userID] = $username;
    }
    

    I declared the user id and the username prior to adding the selection to the array. I also used the correct format as user->id and user->usrname rather than $user['username']

    Thanks to @Samutz for making the main components correct!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 腾讯云如何建立同一个项目中物模型之间的联系
  • ¥30 VMware 云桌面水印如何添加
  • ¥15 用ns3仿真出5G核心网网元
  • ¥15 matlab答疑 关于海上风电的爬坡事件检测
  • ¥88 python部署量化回测异常问题
  • ¥30 酬劳2w元求合作写文章
  • ¥15 在现有系统基础上增加功能
  • ¥15 远程桌面文档内容复制粘贴,格式会变化
  • ¥15 这种微信登录授权 谁可以做啊
  • ¥15 请问我该如何添加自己的数据去运行蚁群算法代码