简体   繁体   中英

How to get an id of the user in Codeigniter?

This is actually very easy but yet I am confused how to do this: I have Reseller that can request X-number of users to admin. So that value gets saved to database. Now when admin logs in he can see the requests of resellers. Like this : 要求

Now Admin Can either approve or reject the request. If He approves the request the code creates users in users table mapped to that reseller. The mapping is done with a unique field called Key Now Creating users works perfectly but my problem is :

When I click on Approve it creates users but for only for the reseller wit id = 1. I want to create something like when I click on first record it creates users for 1st reseller and when on 2nd same as above[This is as per the image]. This is my view:

<?php if(count($users)): foreach($users as $user): ?>
<tr class="odd gradeX">
    <td><?php echo  $user->id; ?></td>
    <td><?php echo  $user->user_requested; ?></td>
    <td><?php echo  $user->key; ?></td>
    <td><?php echo  $user->date_requested; ?></td>
    <td><a href="reseller/create_user" class="btn btn-primary">Yes&nbsp<i class="glyphicon glyphicon-ok"></i></a></td>
    <td><a href="reseller/change_status" class="btn btn-danger">No&nbsp<?php echo'<i class="glyphicon glyphicon-trash"></i>';?></a></td>
    <td><a href="" class="btn btn-success"><?php echo  $user->status; ?></td>&nbsp</a></td>
</tr>

<?php endforeach; ?>
<?php else: ?>
<tr>
    <td colspan="3">There are no new Requests.</td>
</tr>
<?php endif; ?> 

The Controller

public function create_user()
{
    $this->load->model('more_m');
    $id= $this->session->userdata('id');
    $rajan =$this->more_m->get(array('user_requested',$id));
    $request=$rajan->user_requested;        

    $this->load->model('reseller_m');

    $query=$this->db->select('key');
    $query=$this->db->get('reseller');

    if($query->num_rows() > 0)
    {
        $row = $query->row_array();
        $key= $row['key'];
    }

    for($i=1; $i<=$request;$i++)
    {

        $userdata=array('key'=>$key);

        $this->db->insert('users',$userdata);

    }

    $this->load->model('more_m');
    $id = $this->session->userdata('id');
    $this->db->set('status', "'approved'",FALSE);
    $this->db->where('id',$id);
    $this->db->update('user_request');

    redirect('admin/new_user');
}

My mistake is in controller where I define $id. As when admin logs in his id would be fetched I don't know how to fetch the id of the reseller as per the record.

Its so simple with Codeigniter

What you have to do is pass the ID with the URL (You are wrong Use base_url() as well with the pointing controller)

ex <td><a href="<?php echo base_url()?>reseller/create_user/<?php echo $user->key; ?>"

so now your URL looks like (assume id is equal to 2)

http://localhost/<path to project>/reseller/create_user/2

so in Controller

function create_user($id)//So this know incoming Value to this
{
   //then in here
   echo $id; //this will show 2
   //Continue rest of your code with $id
}

If you wanna do it in php way,You can pass key in your url and get key in your controller using $this->uri->segment(n)

   <?php if(count($users)): foreach($users as $user): ?>
    <tr class="odd gradeX">
        <td><?php echo  $user->id; ?></td>
        <td><?php echo  $user->user_requested; ?></td>
        <td><?php echo  $user->key; ?></td>
        <td><?php echo  $user->date_requested; ?></td>
        <td><a href="reseller/create_user/<?php echo  $user->key; ?>" class="btn btn-primary">Yes&nbsp<i class="glyphicon glyphicon-ok"></i></a></td>
        <td><a href="reseller/change_status" class="btn btn-danger">No&nbsp<?php echo'<i class="glyphicon glyphicon-trash"></i>';?></a></td>
        <td><a href="" class="btn btn-success"><?php echo  $user->status; ?></td>&nbsp</a></td>
    </tr>

    <?php endforeach; ?>
    <?php else: ?>
    <tr>
        <td colspan="3">There are no new Requests.</td>
    </tr>
    <?php endif; ?> 

If you want jquery+php than use ajax.

add value attr. to button pass your key.

<button id="some_id" value="YOURKEY">Click Me</button

This is your script to update :

<script> 
$('body').on('click',"#some_id", function(){ 
var key =$(this).attr("value"); 
//Now you got ur key you can use ajax to update and change location of page on success 
}); 
</script>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM