vendredi 25 avril 2014

PHP mySql fetch all items from table


Vote count:

0




I'm really new to PHP and mySql, but I'm trying to figure out how to fetch all items from a table. In my item handler class I'm doing



static function getAllItems(){

$items = array();
$db = new Database();

$result = $db->query("SELECT * FROM items");

while($row = $result->fetch_assoc()){
$items[] = $row;
}

return $items;
}


and then on the client side I'm doing this to iterate through the items returned and display them, but keep getting an error



"Fatal error: Call to a member function getTitle() on a non-object"



What am I doing wrong?



<div class="table-responsive">
<?php
$allItems = ItemHandler::getAllItems();

if(empty($allItems)){
echo '<p class="lead text-center">No listings in this category yet.</p>';
} else {
echo '<table class="table">

<tr>
<th>Image</th>
<th>Item Name</th>
<th>Quantity</th>
<th>Description</th>
<th>Seller</th>
<th>Price</th>
<th>Purchase</th>
</tr>';


foreach($allItems as $item){
echo "<tr>
<td>blank</td>
<td>{$item->getTitle()}</td>
<td>1</td>
<td>{$item->getDescription()}</td>
<td>test seller</td>
<td>{$item->getPrice()}</td>
<td><a href='#'><button type='button' class='btn btn-primary'>Buy</button></a></td>
</tr>";
}

echo '</table>';
}

?>

</div>


asked 40 secs ago






Aucun commentaire:

Enregistrer un commentaire