【发布时间】:2017-12-10 15:03:08
【问题描述】:
我的数据库中有一个表中的SELECT 数据。这些数据带有链接。我想要的是,当我单击链接时,它会在 div 中加载更多数据,而不是转到不同的页面。每个链接都带有一个id,从中可以知道要从数据库中加载div的数据。下面是代码
<div class="pricepop"></div>
<div class="pricetab">
<table class="table">
<thead>
<tr>
<th>COMMODITIES</th>
<th>UNITS</th>
<th>INTERNATIONAL PRICE($)</th>
<th>TERMS</th>
<th>LOCAL PRICE (₦)</th>
<th>AS AT</th>
<th>MARKET</th>
<th>Full Details</th>
</tr>
</thead>
<tbody>
<style>
.table > thead > tr > th, .table > tbody > tr > td {
font-size: 10px !important;
font-family: 'Lato', sans-serif;
font-weight: bold;
}
</style>
<?php
$sql = $db->prepare("SELECT * FROM commodityprices");
$result = $sql->execute();
while ($row = $result->fetchArray(SQLITE3_ASSOC))
{
$id = $row['id'];
$_SESSION['id'] = $id;
$name = $row['name'];
$unit = $row['unit'];
$iprice = $row['iprice'];
$lprice = $row['lprice'];
$irate = $row['irate'];
$lrate = $row['lrate'];
$terms = $row['cterms'];
$asat = date('d/M/Y');
$market = $row['market'];
echo '<tr>
<td>'.$name.'</td>
<td>'.$unit.'</td>';
if ($irate == "Down")
{
echo '
<td style="background: #ef5a66; color: #fff"><i class="fa fa-caret-down"> </i> '.$iprice.'</td>';
}
else
{
echo '
<td style="background: #28bc88; color: #fff"><i class="fa fa-caret-up"> </i> '.$iprice.'</td>';
}
echo '<td>'.$terms.'</td>';
if ($lrate == "Down")
{
echo '
<td style="background: #ef5a66; color: #fff"><i class="fa fa-caret-down"> </i> '.$lprice.'</td>';
}
else
{
echo '
<td style="background: #28bc88; color: #fff"><i class="fa fa-caret-up"> </i> '.$lprice.'</td>';
}
echo '<td>'.$asat.'</td>
<td>'.$market.'</td>
<td><a class="comprice" href="pricedetails.php?id='.$id.'">View more</a></td>
</tr>';
}
?>
</tbody>
</table>
</div>
下面我有一个javascript,它在点击链接时将数据库中的数据加载到.pricepop
$(document).ready(function() {
$(".comprice").on("click", function (e) {
e.preventDefault();
$(".pricepop").load("pricedetails.php");
});
});
下面是获取剩余价格详情的priceetails.php代码。
<thead>
<tr>
<th>COMMODITIES</th>
<th>TERMS</th>
<th>LOCAL PRICE (₦)</th>
<th>AS AT</th>
<th>MARKET</th>
<th>Price/bag</th>
</tr>
</thead>
<tbody>
<style>
.table > thead > tr > th, .table > tbody > tr > td {
font-size: 10px !important;
font-family: 'Lato', sans-serif;
font-weight: bold;
}
</style>
<?php
$priceId = $_SESSION['id'];
$sql = $db->prepare("SELECT * FROM commodityprices WHERE id = ?");
$sql->bindParam(1, $priceId, SQLITE3_INTEGER);
$result = $sql->execute();
while ($row = $result->fetchArray(SQLITE3_ASSOC))
{
$id = $row['id'];
$name = $row['name'];
$iprice = $row['iprice'];
$lprice = $row['lprice'];
$lrate = $row['lrate'];
$terms = $row['cterms'];
$asat = date('d/M/Y');
$market = $row['market'];
$priceperbags = $row['priceperbags'];
echo '<tr>
<td>'.$name.'</td>';
echo '<td>'.$terms.'</td>';
if ($lrate == "Down")
{
echo '
<td style="background: #ef5a66; color: #fff"><i class="fa fa-caret-down"> </i> '.$lprice.'</td>';
}
else
{
echo '
<td style="background: #28bc88; color: #fff"><i class="fa fa-caret-up"> </i> '.$lprice.'</td>';
}
echo '<td>'.$asat.'</td>
<td>'.$market.'</td>
<td class="comprice">'.$priceperbags.'</td>
</tr>';
}
?>
</tbody>
</table>
</div>
现在的问题是,无论我从回显的php 数据中单击哪个链接,.pricepop 中显示的数据都是最后一个echo 链接的详细信息。
我该如何解决这个问题,当我点击一个链接时,从该链接的id 获得的详细信息是echo 并显示在.pricepop 中。
【问题讨论】:
标签: javascript jquery html css sql