我的代码无法显示我选择的一个用户的详细信息,无论我点击谁,它总是会显示有关桌面上最后一个用户的详细信息。 我认为我的问题是我的代码无法实现我点击了谁只是选择了最后一个用户。你能看看并帮助我解决我的问题。 这是我的观点(我在bootstrap模态上使用模态来显示结果)
<div class="modal fade" id="userDetail" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
<h4 class="modal-title" id="exampleModalLabel"><span class = "glyphicon glyphicon-pencil"></span> Edit</h4>
</div>
<div class="modal-body">
<div class="form-group">
<?php echo form_open('admin/update_event'); ?>
<p><b>User Name</b></p><p><input class = "form-control" placeholder = "User name" type='text'
name = 'event_name' id = 'event_name' readonly value = "<?php echo $r->username;?>"></p>
<p><b>Email</b></p><p><input class = "form-control" readonly placeholder = "Email" type='text'
name = 'event_code' id = 'event_code' readonly value = "<?php echo $r->email;?>"></p>
<hr>
<p><b>First Name</b></p><p><input class = "form-control" readonly placeholder="First Name of member" type='text'
name = 'event_size' id = 'event_size' readonly value = "<?php echo $r->firstname;?>"></p>
<p><b>Last Name</b></p><p><input class = "form-control" readonly placeholder="Last Name" type='text'
name = 'lastname' readonly value = "<?php echo $r->lastname;?>"></p>
<p><b>Level</b></p><p>
<select class = "form-control">
<option selected disabled><?php if($r->level==1){
echo 'Admin';
}else{
echo 'Member';
} ?></option>
<option value="1" <?php echo ($r->level == 1)?:""; ?>>Administrator</option>
<option value="0" <?php echo ($r->level == 2)?:""; ?>>Member</option>
</select>
</p>
<p><b>Status</b></p><p><select class = "form-control">
<option selected disabled><?php if($r->sta==1){
echo 'Deactive';
}else{
echo 'Active';
} ?></option>
<option value="1" <?php echo ($r->sta == 1)?:""; ?>>Deactive</option>
<option value="0" <?php echo ($r->level == 0)?:""; ?>>Active</option>
</select></p>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
<input type = "submit" value = "Update" class = "btn btn-primary">
<?php echo form_close();?>
</div>
这是我的代码:
<?php
$count = 1;
foreach ($ro as $r)
{
?>
<tr>
<td scope="row"><?php echo $count++?></td>
<td>
<a href="#" data-toggle="modal" data-target="#userDetail">
<?php echo $r->username; ?>
</a>
</td>
<td><?php echo $r->email; ?></td>
<td><?php echo $r->firstname; ?></td>
<td><?php echo $r->lastname; ?></td>
<td><?php $r->level;
if($r->level==1){
echo '<strong><p style="color:#0047b3;">Admin</p></strong>';
}else{
echo '<strong><p style="color:#993300;">Member</p></strong>';
}
?></td>
</tr>
<?php
}
?>
这是我的控制器
function admins_area(){
$data['main_content'] = 'backend/home/admins_area';
$this->load->model('membership_model');
$data['rows']= $this->membership_model->getUserData();
$data['ro']= $this->membership_model->getAllUserData();
$this->load->view('includes/admin/template', $data);
}
这是我的模特
function getUserData(){
$this->db->where('username', $this->session->userdata('username'));
$query = $this->db->get('user');
if($query->num_rows()>0){
foreach ($query->result() as $row){
$data[]=$row;
}
return $data;
}
}
function getAllUserData(){
$query = $this->db->get('user');
if($query->num_rows()>0){
foreach ($query->result() as $row) {
$data[]=$row;
}
return $data;
}
}
答案 0 :(得分:0)
嘿在控制器中创建一个名为Modal.php的文件,并将此代码粘贴到此
中<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Modal extends CI_Controller {
function __construct()
{
parent::__construct();
$this->load->database();
$this->load->helper(array('form', 'url'));
/*cache control*/
$this->output->set_header('Last-Modified: ' . gmdate("D, d M Y H:i:s") . ' GMT');
$this->output->set_header('Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0');
$this->output->set_header('Pragma: no-cache');
$this->output->set_header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
}
/***default functin, redirects to login page if no admin logged in yet***/
public function index()
{
}
/*
* $page_name = The name of page
*/
function popup($page_name = '' , $param2 = '' , $param3 = '')
{
//$account_type = $this->session->userdata('login_type');
$page_data['param2'] = $param2;
$page_data['param3'] = $param3;
$this->load->view( $page_name.'.php' ,$page_data);
}
}
和 这可以看到所有用户
<?php
$count = 1;
foreach ($ro as $r)
{
?>
<tr>
<td scope="row"><?php echo $count++?></td>
<td>
<?php echo $r->username; ?>
</td>
<td>
<button onclick="showAjaxModal('<?php echo site_url(); ?>/modal/popup/details/<?php echo $r->username; ?>')">Details</button>
</td>
</tr><?php } ?>
还有一些HTML
<div class="modal fade" id="modal_ajax" data-backdrop="static" data-keyboard="false">
<div class="modal-dialog" >
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
<h4 class="modal-title">User Details</h4>
</div>
<div class="modal-body">
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
和一些javascript
<script>
function showAjaxModal(url)
{
// SHOW AJAX RESPONSE ON REQUEST SUCCESS
jQuery.ajax({
url: url,
success: function(response)
{
jQuery('#modal_ajax .modal-body').html(response);
jQuery('#modal_ajax').modal('show', {
backdrop: 'static',
keyboard: false
});
}
});
}
</script>
最后在application / views文件夹中创建一个名为details.php的文件并写入
<?php
$this->db->where('username', $param2);
$query = $this->db->get('user');
$details = $query->row();
print_r($details);
?>