Codeigniter:将下拉值存储到会话变量中

时间:2016-04-26 10:24:12

标签: php codeigniter

我在登录页面中有一个下拉列表,它提取公司代码,它的相应ID作为值。我需要将此特定值(ID)存储在会话变量中,以便我可以在整个站点中使用它。因此,当选择该特定公司时,我可能能够查看该特定公司的详细信息。如果有替代方法,请告诉我。

MODEL:

public function login ()
{
    $user = $this->get_by(array(
    'company_id' => $this->input->post('company_id'),
    'username' => $this->input->post('username'),
    'password' => $this->hash($this->input->post('password')),
), TRUE);

$this->session->set_userdata($user);
    if (count($user)) {
        // Log in user
        $data = array(
            'username' => $user->username,
            'id' => $user->id,
            'loggedin' => TRUE,
            'is_admin' => $user->is_admin,
            'company_id' => $user->company_id
        );
        $this->session->set_userdata($data);
        return TRUE;
    }

}

查看:

<tr>
    <td>Location</td>
    <td><select class="form-control" id="company_code" name="company_code" required>
          <option value="0" selected>Select Company</option>                      
          <?php foreach($locations as $location){
            $selected = $user->company_id==$location->company_id?"selected='selected'":'';
            echo "<option $selected value='".$location->company_id."'>".$location->company_code."</option>";
        }?>
        </select>
</td>
</tr>
<tr>
    <td>Username</td>
    <td><?php echo form_input('username','','class="form-control" placeholder="Enter User Name"'); ?></td>
</tr>
<tr>
    <td>Password</td>
    <td><?php echo form_password('password','','class="form-control" placeholder="Enter Password"'); ?></td>
</tr>
<tr>
    <td></td>
    <td><?php echo form_submit('submit', 'Log in', 'class="btn btn-primary"'); ?></td>
</tr>

我希望将上面的company_id发布到会话变量中,以便在整个程序中使用它。任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:0)

试试这个

 $selected=$this->session->userdata("company_id")==$location->company_id?"selected='selected'":'';