我现在已经在网上搜索了几个小时,但我仍然无法弄明白。
所以我现在问你们:
好的,我有ha网站和MS SQL服务器(loalhost),我想让这个功能起作用:
首先:我运行一个查询来打印出我数据库中的所有客户(在表格中):`
$searchq = $_POST['search'];
$resultArray = array();
$sql = "SELECT * FROM Costumers WHERE firstname LIKE '%".$searchq."%' ";
$stmt = sqlsrv_query( $conn, $sql);
if( $stmt == false){
die( print_r(sqlsrv_errors(), tue) );
}
然后我将结果打印在表格中:
echo "<table border='1'>";
echo "<tr><th>CustomerID</th><th>Firstname</th><th>Lastname</th><th>Tlf</th><th>Email</th><th>Productnr</th><th>Address</th></tr>";
while($row = sqlsrv_fetch_array( $stmt, SQLSRV_FETCH_BOTH))
{
$resultArray[] = $row;
echo "<tr>";
echo "<td>" . $row[0] . "</td>";
echo "<td>" . $row[1] . "</td>";
echo "<td>" . $row[2] . "</td>";
echo "<td>" . $row[3] . "</td>";
echo "<td>" . $row[4] . "</td>";
echo "<td>" . $row[5] . "</td>";
echo "<td>" . $row[6] . "</td>";
echo "</tr>";
}
echo "</table>";
我已经制作了一个数组列表,以便我可以在代码中稍后使用变量“row [x]”。不知道这是否是必要的但是..
第二: 我现在想要这些行是可点击的,所以如果你从表中点击一个客户,你现在将运行一个新的查询:
$sql = "SELECT * FROM Products WHERE Costumers = '".$resultArray[0]['']."' ";
$stmt = sqlsrv_query( $conn, $sql);
if( $stmt == false){
die( print_r(sqlsrv_errors(), tue) );
}
echo "<table border='1'>";
echo "<tr><th>CustomerID</th><th>Firstname</th><th>Lastname</th><th>Tlf</th><th>Email</th><th>Productnr</th><th>Address</th></tr>";
while( $row = sqlsrv_fetch_array( $stmt, SQLSRV_FETCH_NUMERIC) )
{
echo "<tr>";
echo "<td>" . $row[0] . "</td>";
echo "<td>" . $row[1] . "</td>";
echo "<td>" . $row[2] . "</td>";
echo "<td>" . $row[3] . "</td>";
echo "</tr>";
}
echo "</table>";
现在应该打印出所有包含与我点击的CosuterID相同的CosuterID的产品。
我已经发现我需要使用som AJAX,但我无法让它工作..在这里有人可以帮助我吗?我真的需要这个工作!
谢谢:)
答案 0 :(得分:0)
在td上设置onClick功能 这里还包括最新的Jquery文件
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<?php
echo "<table border='1'>";
echo "<tr><th>CustomerID</th><th>Firstname</th><th>Lastname</th><th>Tlf</th><th>Email</th><th>Productnr</th><th>Address</th></tr>";
while($row = sqlsrv_fetch_array( $stmt, SQLSRV_FETCH_BOTH))
{
$resultArray[] = $row;
echo "<tr>";
echo "<td onClick='lodaproduct(" . $row[0] . ")'>" . $row[0] . "</td>";
echo "<td>" . $row[1] . "</td>";
echo "<td>" . $row[2] . "</td>";
echo "<td>" . $row[3] . "</td>";
echo "<td>" . $row[4] . "</td>";
echo "<td>" . $row[5] . "</td>";
echo "<td>" . $row[6] . "</td>";
echo "</tr>";
}
//Set another row column to show the out put from ajax.php page
echo "<tr><td id='costumersdata'></td></tr>";
echo "</table>";
?>
<script>
function lodaproduct(Costumers){
$.post("ajax.php?Costumers="+Costumers,
function(data){
$("#costumersdata").html(data);
});
}
</script>
在你的ajax.php页面上
<?php
$sql = "SELECT * FROM Products WHERE Costumers = '".$_POST['Costumers']."' ";
$stmt = sqlsrv_query( $conn, $sql);
if( $stmt == false){
die( print_r(sqlsrv_errors(), tue) );
}
echo "<table border='1'>";
echo "<tr><th>CustomerID</th><th>Firstname</th><th>Lastname</th><th>Tlf</th><th>Email</th><th>Productnr</th><th>Address</th></tr>";
while( $row = sqlsrv_fetch_array( $stmt, SQLSRV_FETCH_NUMERIC) )
{
echo "<tr>";
echo "<td>" . $row[0] . "</td>";
echo "<td>" . $row[1] . "</td>";
echo "<td>" . $row[2] . "</td>";
echo "<td>" . $row[3] . "</td>";
echo "</tr>";
}
echo "</table>";
?>
答案 1 :(得分:0)
我正在逐步说明,所以你可以按照这种方式: