我不是100%肯定你正在嘗試做的,但我認爲這是你在找什麼:
/functions/getUserRequest.php
這是一個清理請求的函數。最好只在使用它之前將它包含在頁面中。這是可選的。
<?php
function getUserRequest($con,$user_id)
{
# Since mysql_ is deprecated/removed from new version, I will use PDO
# safely binding the value
$query = $con->prepare("select * from request, users where request.user_id = users.id and request.user_id = :0");
$query->execute(array(":0"=>$user_id));
while($result = $query->fetch(PDO::FETCH_ASSOC)) {
$row[] = $result;
}
return (isset($row))? $row : array();
}
/functions/connection.php
這裏是一個將使用define()
值的連接憑據數據庫連接。這是可選的,但應該以更完整的方式實施。在PHP7中刪除了mysql_*
。
function connection()
{
return new \PDO("mysql:host=".DB_HOST.";dbname=".DB_NAME, DB_USER, DB_PASS);
}
的index.php
# Here you would add a config with DB defines and such
# Add the functions
include_once(__DIR__.'/functions/connection.php');
include_once(__DIR__.'/functions/getUserRequest.php');
# Create connection
$con = connection();
?>
<table border="1" cellpadding="3">
<?php
# Since Ids are supposed to be unique, you should make an auto-incrementer
$i = 0;
# Pass id here
foreach(getUserRequest($con,123) as $info) {
?>
<tr class='my_loans_tr'>
<?php
/*
** Here is where the incrementing happens
** Also, pass the whole element to the js function
*/
?>
<td id='demo<?php echo $i ?>' onclick='myfunction(this)' class='weblex-show- detail' data-toggle='modal' data-target='#myModalw'><?php echo $info['loan_id'] ?></td>
<td class='admin_amount'><?php echo $info['amount'] ?></td>
<td class='admin_points'><?php echo $info['points'] ?></td>
<td class='admin_date'><?php echo $info['req_date'] ?></td>
<td class='admin_status'><?php echo $info['status'] ?></td>
<td class='admin_cancelled'><?php echo $info['cancelled_loan'] ?></td>
</tr>
<?php
$i++;
}
?>
</table>
那麼你的JS功能就會啓動:
<script type="text/javascript">
function myfunction(obj)
{
// This should get the value between the <td>Value</td>
var getValue = obj.innerHTML;
// You should then be able to use AJAX to retrieve data with
// inner value (or however you want to use this value)...
alert(getValue);
}
</script>
編輯:
由於您現在嘗試使用jQuery,因爲您的ID都是唯一的,所以您需要觸發點擊類。
<script type="text/javascript">
$(document).ready(function(){
// You need to be listening for the click on the class "detail"
$(".detail").click(function(){
// This captures the current selected DOM object
var obj = $(this);
// This will extract the value inside
var objValue = obj.text();
// This is where you send the data to a new page to get a response
$.ajax({
url: '/page/to/ajax/dispatcher.php',
type: 'post',
data: {
'id':objValue
},
success: function(response) {
// You can see the response in your console log
console.log(response);
// To update your html, you can just receive it from
// your ajax dispatch page and place it into the modal (or wherever)
$('#myModalw').html(response);
}
});
});
});
</script>
爲什麼在第一次查詢中不使用JOIN來獲取所有數據,因此還沒有做另一個查詢? – Hokusai
我想到了它,2個不同的表,請求哪裏loan_id創建每個貸款,收集(表2)貸款ID在這裏,但有一個字段名稱收集,所以我可以有loanid 147和1-10收集ID屬於loan_id 147,必須切換,無法顯示所有信息 –