I'm working on a small program that extracts data from a table, and then builds an html table to display it in and returns that table back via JSON. What is happening though is that the table returns null. However at the end of the loop I can bring the table variable and it will display the table built perfectly in its entirety. I'm not receiving any errors and can't find an area where the table would reset itself. So if someone can help me out here that'd be amazing. My code is below, my apologies it's a little messy at the moment, I plan to clean it up soon:
<?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;
}
}
?>