使用php获取和回显结果

时间:2013-08-28 16:54:16

标签: php

我在显示查询结果时出现问题。奇怪的是,所有的结果都是获得的,只有一个不是。我完全失去了。

据我所知,我将描述这段代码应该如何工作。我是从教程中构建的。

在“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&amp;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" );
}

以下是产品类的部分内容,我认为它与显示列表有关... cus it say get list。

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'];
}

0 个答案:

没有答案