我正在开发一个从表中提取数据的小程序,然后构建一个html表来显示它并通过JSON返回该表。然而,正在发生的是表返回null。但是在循环结束时,我可以带来表变量,它将显示完整构建的表。我没有收到任何错误,也找不到表重置的区域。所以,如果有人可以帮助我,那就太棒了。我的代码在下面,我道歉此刻有点乱,我打算尽快清理它:
<?php
error_reporting(E_ALL);
ini_set('display_errors', '1');
require_once '../../inc/config.php';
$response = array();
$response['table'] = "";
$table = "<thead>
<tr>
<th>Card Name</th>
<th class='hidden-xs'>Card Number</th>
<th class='hidden-xs'>Set</th>
<th class='hidden-xs'>Rarity</th>
<th class='hidden-xs text-center'>Market Avg.</th>
<th class='hidden-xs text-center'>Change</th>
<th class='hidden-xs text-center'>Sell Price</th>
<th class='hidden-xs text-center'>Buy Price</th>
</tr>
</thead>
<tbody>";
$cardname = $_REQUEST['card_name'];
$set_id = $_REQUEST['set_name'];
$rarity = $_REQUEST['rarity'];
$where = "1=1";
$where .= !empty($cardname) ? " AND c.card_name LIKE :cardname" : "";
$where .= !empty($set_id) ? " AND c.setlist__id = :set_id" : "";
$where .= !empty($rarity) ? " AND c.ygo_rarity__id = :rarity" : "";
$q = "SELECT c.id, c.card_name, s.set_name, CONCAT_WS('-', s.set_abbr, c.card_number) as card_number, card_rarity, c.card_price, c.card_price_change, s.set_abbr, r.rarity_abbr FROM priceguide.cardlist c
INNER JOIN priceguide.setlist s
ON c.setlist__id = s.id
INNER JOIN priceguide.ygo_rarity r
ON c.card_rarity = r.rarity
WHERE $where
ORDER BY c.id ASC";
$stmt = $CONN->prepare($q);
$cardname = !empty($cardname) ? "%".$_REQUEST['card_name']."%" : "";
if(!empty($cardname)) $stmt->bindParam(":cardname", $cardname, PDO::PARAM_STR);
if(!empty($set_id)) $stmt->bindParam(":set_id", $set_id, PDO::PARAM_INT);
if(!empty($rarity)) $stmt->bindParam(":rarity", $rarity, PDO::PARAM_INT);
if($stmt->execute()){
while($row = $stmt->fetch(PDO::FETCH_ASSOC)){
$table.="<tr>";
$card = htmlentities($row['card_name']);
$table.="<td><a href='' data-imageSrc='".$row['set_abbr']."/".$row['card_number']."-".$row['rarity_abbr'].".png' class='card-view' data-cardid='".$row['id']."'>".htmlentities($row['card_name'])."</a></td>";
$table.="<td class='hidden-xs'>".$row['card_number']."</td>";
$table.="<td class='hidden-xs'>".$row['set_name']."</td>";
$table.="<td class='hidden-xs'>".$row['card_rarity']."</td>";
$table.="<td class='hidden-xs text-center'>$".number_format(floatval($row['card_price']), 2, '.', '')."</td>";
$change = number_format(floatval($row['card_price_change']), 2, '.', '');
if($change == 0.00){
$change = "<font color='#FFCC00'><span class='glyphicon glyphicon-minus'></span></font>";
}elseif($change < 0.00){
$change = "<font color='#CC0000'><span class='glyphicon glyphicon-triangle-bottom'></span></font>\$$change";
}else{
$change = "<font color='#33CC00'><span class='glyphicon glyphicon-triangle-top'></span></font>\$$change";
}
$table.="<td class='hidden-xs text-center'>$change</td>";
$sell = number_format(floorToFraction(floatval($row['card_price']), 4), 2, '.', '');
$maxprice = (($sell - 1.93)-($sell*.10))/1.35;
$maxprice = floorToFraction($maxprice, 4);
$maxprice = $maxprice < 0 ? number_format(0, 2, '.', '') : number_format($maxprice, 2, '.', '');
$table.="<td class='hidden-xs text-center'>\$$sell</td>";
$table.="<td class='hidden-xs text-center'>\$$maxprice</td>";
$table.="</tr>";
}
$table .= "</tbody>";
$response['errors'] = false;
$response['table'] = $table;
}else{
$response['errors'] = true;
$response['message'] = "There was an error when searching the database, plesae contact the system administrator";
}
echo json_encode($response);
function floorToFraction($number, $denominator = 1){
if($number > 1.00){
$x = $number * $denominator;
$x = ceil($x);
$x = $x / $denominator;
return $x;
}else{
return $number;
}
}
?>
答案 0 :(得分:1)
这很可能是因为PHP中的json_encode()要求所有内容都是UTF8编码的。你可以试试
$response['table'] = utf8_encode($table);
我在其中看不到任何可能需要编码的内容。