gpt4 book ai didi

php - 无法在 html 中显示用户的详细数据(但可以使用 print_r() 显示数组) "codeigniter"

转载 作者:行者123 更新时间:2023-11-29 17:47:13 27 4
gpt4 key购买 nike

当我单击每个用户名旁边的详细信息按钮时,我想显示用户详细信息模式,但问题是我无法显示从 Controller 获得的“user_detail”数据,这让我很困惑< em>因为当我使用 print_r() 从 Controller 显示时,它完美地显示了我想要的数据数组。现在我只想将其显示为 html,就像我首先显示所有用户一样。

这是我的完整代码,我认为我们应该只关注 Controller 中的 get_detail() 方法。

模型/test_model.php:

function get_user()
{
$query = $this->db->query("SELECT * FROM tb_users ");
$result = $query->result_array();
return $result;
}

function get_detail($user_id)
{
$query = $this->db->query("SELECT * FROM tb_users where user_id = '".$user_id."'");
$result = $query->result_array();
return $result;
}

Controller /Welcome.php:

public function index() 
{
$data["datakcp"] = $this->test_model->get_user();
$this->load->view('welcome_message',$data); // do this by loading a "view" that formats the data we gather here
}

public function get_user($user_id = null)
{
// get the user_id from the ajax request
// this means if $user_id is set (not null) then make $user_id = $user_id. Otherwise, make $user_id = posted input
$user_id = ($user_id) ? $retailer_id : $this->input->post('user_id');

$datadetail= $this->test_model->get_detail($user_id);

echo json_encode($datadetail);
}

View /my_view.php:

<div class="container" style="width:700px;">  
<h3 align="center"> </h3>
<br>
<div class="table-responsive">
<table class="table table-bordered">
<tr>
<th width="70%">Employee Name</th>
<th width="30%">View</th>
</tr>
<?php foreach($datakcp as $user){ ?>
<tr>
<td><?php echo $user["user_name"] ?></td>
<td><input type="button" name="detail" value="detail" id="<?php echo $user["user_id"] ?>" class="btn btn-info btn-xs view_dawta"></td>
</tr>
<?php } ?>
</table>
</div>
</div>
<div id="dataModal" class="modal fade">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-body" id="employee_detail">
<table>
<thead>
<th>Username<th>
<th>Campaign</th>
</thead>
<tbody id="userDetail">
</tbody>
</table>
</div>
</div>
</div>

我的 jQuery ajax 脚本:

 <script>  
$(document).ready(function(){
$('.view_dawta').click(function(){
var user_id= $(this).attr("id");
$.ajax({
url:"index.php/Welcome/get_user",
method:"post",

data:{user_id:user_id},
success:function(data){
console.log(data)
$.each((JSON.parse(data)), function(key, value){
$('#userDetail').append(
'<tr>'+
'<td>'+value.user_name+'</td>'+
'<td>'+value.campaign_name+'</td>'+
'</tr>'
);
});
$('#dataModal').modal("show");
}
});
});
});
</script>

最佳答案

get_user函数没有返回任何内容。 (注意:当您尝试 print_rvar_dump 语句时,将返回 print_rvar_dump 语句的输出,否则不会返回任何内容)。

echo json_encode($datadetail);最后在 get_user功能并接收json响应数据。然后,您必须使用并格式化 json您的 ajax 中的数据成功函数。

或者,您可以返回格式化的 html来自get_user函数,在这种情况下你的 $('#employee_detail').html(data);应该按原样工作。

编辑:

1- 这是一个基本的 ajax您应该用来接收 json 的电话数据:

$.ajax({  
url:"index.php/Welcome/get_user",
method:"post",
contentType: "application/json;",
dataType: "json",
data:{user_id:user_id},
success:function(data){
$('#employee_detail').html(data);
$('#dataModal').modal("show");
}
});

2- 在你的 get_user 中函数,输入 echo json_encode($datadetail);在最后。此时,您应该可以看到 json模式中的数据。

3- 在 get_user功能,替换$datadetail["userdetail"]$datadetail 。在函数的末尾,echo json_encode($datadetail); .

4- 在 my_view.php ,在 <div class="modal-body" id="employee_detail"> 内构建一个表如下所示(这只是一种方法,如果您愿意,您可以决定不同的方法):

<table>
<thead>
<th>Username</th>
<th>Campaign</th>
<!-- include more <th> tags for other user data -->
</thead>
<tbody id="userDetail">
</tbody>
</table>

5- 在 ajax调用,您的success函数可以是:

success:function(data){  
$.each(data, function(key, value){
$('#userDetail').append(
'<tr>'+
'<td>'+value.user_name+'</td>'+
'<td>'+value.campaign_name+'</td>'+
/* include more <td> tags for other user data */
'</tr>'
);
});
$('#dataModal').modal("show");
}

6- 这应该足以让您完整了解如何接收和处理 json数据并将其格式化为 html。这应该能够帮助您入门。您现在可以处理其他格式细节等。

在相关说明中,您有重复的 user_id在您返回的数据中。请检查一下。

希望这有帮助。

关于php - 无法在 html 中显示用户的详细数据(但可以使用 print_r() 显示数组) "codeigniter",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49708047/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com