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条)

报告相同问题?

悬赏问题

  • ¥60 要数控稳压电源测试数据
  • ¥15 能帮我写下这个编程吗
  • ¥15 ikuai客户端l2tp协议链接报终止15信号和无法将p.p.p6转换为我的l2tp线路
  • ¥15 经gamit解算的cors站数据再经globk网平差得到的坐标做形变分析
  • ¥15 GD32 SPI通信时我从机原样返回收到的数据怎么弄?
  • ¥15 phython读取excel表格报错 ^7个 SyntaxError: invalid syntax 语句报错
  • ¥20 @microsoft/fetch-event-source 流式响应问题
  • ¥15 ogg dd trandata 报错
  • ¥15 高缺失率数据如何选择填充方式
  • ¥50 potsgresql15备份问题