我在显示查询结果时遇到问题。奇怪的是,所有的结果都得到了,只有一个没有。我完全不知所措。
据我所知,我将描述这段代码应该如何工作。我是根据教程构建的。
在“listProducts.php”中,数据库中的所有产品都列在一个表中。
<table id="productTable">
<tr>
<th>UPC</th>
<th>Title</th>
<th>Thumb</th>
<th>Price</th>
<th>Stock</th>
<th>Lead Time</th>
<th></th>
</tr>
<?php foreach ( $results['products'] as $product ) {
list($week, $day) = explode("|", $product->lead);
echo htmlspecialchars($product->lead);
?>
<tr onclick="location='products.php?action=editProduct&productId=<?php echo $product->id?>'">
<td><img src="../<?php echo $product->thumb ?>"/></td>
<td><?php echo htmlspecialchars($product->upc)?></td>
<td><?php echo htmlspecialchars($product->title)?></td>
<td style="text-align:right;">$<?php echo number_format($product->price, 2 )?></td>
<td style="text-align:right;"><?php echo number_format($product->stock, 0)?></td>
<td style="text-align:right;"><? echo $week.'|'.$day;?></td>
<td><button >Edit</button></td>
</tr>
<? } ?>
</table>
问题是铅,它作为铅存在于数据库中,没有被提取或没有被提取,因此没有被分解,因此没有被作为周和日回显。
即使出于测试目的,我只是回应,我什么也得不到。
<? echo htmlspecialchars($products->lead)?>
有一个 listProducts() 函数和一些我没有想到的类垃圾,但我已经彻底操纵到我自己的目的,所以我相当有信心它们应该工作。但是,无论如何我都会发布它们。
function listProducts() {
$results = array();
$data = Product::getList();
$results['products'] = $data['results'];
$results['totalRows'] = $data['totalRows'];
$results['pageTitle'] = "All Products";
if ( isset( $_GET['error'] ) ) {
if ( $_GET['error'] == "productNotFound" ) $results['errorMessage'] = "Error: Product not found.";
}
if ( isset( $_GET['status'] ) ) {
if ( $_GET['status'] == "changesSaved" ) $results['statusMessage'] = "Your changes have been saved.";
if ( $_GET['status'] == "productDeleted" ) $results['statusMessage'] = "Product deleted.";
}
require( TEMPLATE_PATH . "/admin/listProducts.php" );
}
这是我认为处理显示列表的产品类的部分......因为它说获取列表。
public static function getList( $numRows=100, $order="title DESC" ) {
$conn = new PDO( DB_DSN, DB_USERNAME, DB_PASSWORD );
$sql = "SELECT SQL_CALC_FOUND_ROWS *, id FROM products
ORDER BY " . $order . " LIMIT :numRows";
$st = $conn->prepare( $sql );
$st->bindValue( ":numRows", $numRows, PDO::PARAM_INT );
$st->execute();
$list = array();
while ( $row = $st->fetch() ) {
$product = new Product( $row );
$list[] = $product;
}
// Now get the total number of products that matched the criteria
$sql = "SELECT FOUND_ROWS() AS totalRows";
$totalRows = $conn->query( $sql )->fetch();
$conn = null;
return ( array ( "results" => $list, "totalRows" => $totalRows[0] ) );
}
以及一些构造函数和变量。不知道有没有必要
public $id = null;
public $upc = null;
public $title = null;
public $thumb = null;
public $description = null;
public $price = null;
public $stock = null;
public $lead = null;
public function __construct( $data=array() ) {
if ( isset( $data['id'] ) ) $this->id = (int) $data['id'];
if ( isset( $data['upc'] ) ) $this->upc = preg_replace ( "/[^\.\,\-\_\'\"\@\?\!\:\$ a-zA-Z0-9()]/", "", $data['upc'] );
if ( isset( $data['title'] ) ) $this->title = preg_replace ( "/[^\.\,\-\_\'\"\@\?\!\:\$ a-zA-Z0-9()]/", "", $data['title'] );
if ( isset( $data['thumb'] ) ) $this->thumb = $data['thumb'];
if ( isset( $data['description'] ) ) $this->description = $data['description'];
if ( isset( $data['price'] ) ) $this->price = $data['price'];
if ( isset( $data['stock'] ) ) $this->stock = (int) $data['stock'];
if ( isset( $data['week'] ) && isset( $data['day'] ) ) $this->lead = $data['week']."|".$data['day'];
}