2013-06-24 74 views
0

我需要一個函數來從查詢中檢索數據並將該信息發送到請求加載表(html)的頁面,我正在嘗試所見,但沒有得到結果。如何從PHP5.3中的查詢中獲取對象數組?

CLASS PHP

class products { 

    public $Id; 
    public $sku; 
    public $name; 
    public $price; 

    var $function_result = array(); 

    function getProducts() { 
     $mysqli = new mysqli("localhost", "root", "usbw", "Datos"); 
     if (mysqli_connect_errno()) { 
      printf("Error in conection: %s\n", mysqli_connect_error()); 
      exit(); 
     } 
     $query = "SELECT Id,sku,name,price FROM products"; 
     if ($result = $mysqli->query($query)) { 
      $i = 0; 
      while ($obj = mysqli_fetch_object($result)) { 
       $object = new stdClass; 
       $object->Id = $obj["Id"]; 
       $object->sku = $obj["sku"]; 
       $object->name = $obj["name"]; 
       $object->price = $obj["price"]; 
      } 
     } 
     $mysqli->close(); 
     return $object; 
    } 

} 

HTML(PHP)

 <?php 
     require 'Clases/productos.class.php'; 
     $pro = new productos(); 
     $datos = $pro->getProducts(); 
     ?> 
     <!DOCTYPE html> 
      <html> 
       <head> 
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 
       <title></title> 
       </head> 
       <body> 
      <?php 
       for ($i = 0; $i < sizeof($datos); $i++) { 
       ?> 
      <p><?php echo $datos[$i]["name"]; ?></p> 
       <?php 
       } 
       ?> 
       </body> 
       </html> 

周到的建議和幫助!

謝謝! 問候

mauriciohz

+0

和問題是什麼? – 2013-06-24 21:00:40

回答

1

您需要將每個對象添加到一個數組中,並返回。改變你的getProducts功能是這樣的:

function getProducts() { 
    $mysqli = new mysqli("localhost", "root", "usbw", "Datos"); 
    if (mysqli_connect_errno()) { 
     printf("Error in conection: %s\n", mysqli_connect_error()); 
     exit(); 
    } 
    $query = "SELECT Id,sku,name,price FROM products"; 
    $objects = array(); 
    if ($result = $mysqli->query($query)) { 
     $i = 0; 
     while ($obj = mysqli_fetch_object($result)) { 
      $object = new stdClass; 
      $object->Id = $obj["Id"]; 
      $object->sku = $obj["sku"]; 
      $object->name = $obj["name"]; 
      $object->price = $obj["price"]; 
      $objects[] = $object; 
     } 
    } 
    $mysqli->close(); 
    return $objects; 
} 

此外,作爲標記B在評論中提到,有沒有真正的需要創建一個新的對象來存儲結果從mysqli_fetch_object通話,至少你的代碼是目前編寫的。所以while循環可以簡化爲如下所示:

while ($obj = mysqli_fetch_object($result)) { 
    $objects[] = $obj; 
} 
+0

無論如何,做所有這些$ object->任何賦值都沒有什麼意義。 fetch_object已經返回一個stdobject,所以'$ objects [] = $ obj'就是應該需要的。 –

+0

@MarcB這是一個公平點,但也可能這只是代碼的簡化版本,並且可能還有其他原因希望構建新對象。 –

+0

@MarcB我認爲這值得一提,所以我將你的建議加入了答案。謝謝。 –

1

您需要將每行保存到一個數組中。不要忘記你生成對象的數組,你不能用方括號訪問它的元素:

class Products 
{ 
    public $Id; 
    public $sku; 
    public $name; 
    public $price; 

    var $function_result = array(); 

    public function getProducts() { 
     $products = array(); 
     $mysqli = new mysqli("localhost", "root", "usbw", "Datos"); 

     if (mysqli_connect_errno()) { 
      printf("Error in conection: %s\n", mysqli_connect_error()); 
      exit(); 
     } 

     $query = "SELECT Id,sku,name,price FROM products"; 

     if ($result = $mysqli->query($query)) { 
      while ($obj = mysqli_fetch_object($result)) { 
       $object = new stdClass; 
       $object->Id = $obj["Id"]; 
       $object->sku = $obj["sku"]; 
       $object->name = $obj["name"]; 
       $object->price = $obj["price"]; 

       $products []= $object; 
      } 
     } 

     $mysqli->close(); 

     return $products; 
    } 
} 

使用箭頭操作符來訪問對象屬性:

require_once 'Clases/productos.class.php'; 
$almacen = new Productos(); 
$productos = $almacen->getProducts(); 

foreach ($productos as $producto) { ?> 
    <p><?php echo htmlspecialchars($producto->name)></p> 
<?php } 
+0

這兩個貢獻幫助我解決了我的問題,謝謝 – MauricioHz