I’m not very good with php coding, so I’m asking for help please…
I would like to place the output of the following code into a neat table because at the moment it looks very untidy:
// This block grabs the whole list for viewing
$product_list = "";
$sql = mysql_query("SELECT * FROM products ORDER BY category DESC");
$productCount = mysql_num_rows($sql); // count the output amount
if ($productCount > 0) {
while($row = mysql_fetch_array($sql)){
$id = $row["id"];
$category = $row["category"];
$subcategory = $row["subcategory"];
$product_name = $row["product_name"];
$price = $row["price"];
$date_added = strftime("%b %d, %Y", strtotime($row["date_added"]));
$product_list .= "Category: $category | Subcategory: $subcategory | <strong>$product_name</strong> | $price | <a href='inventory_edit.php?pid=$id'>edit</a> • <a href='inventory_list.php?deleteid=$id'>delete</a><br />";
}
} else {
$product_list = "You have no products listed in your store yet";
}
I know I have to place a table after $product_list .= but I just get errors when I do. Could someone please point me in the right direction here? Any help would be much appreciated.
I’m not going to answer your question but since you say: “I’m not very good with php coding” I’m taking the liberty to give you some advice about getting organized. You have a statement:
$sql = mysql_query("SELECT * FROM products ORDER BY category DESC");
SQL is the acronym for “Structured Query Language.” The function ‘mysql_query()’ is not going to return a ‘Structured Query Language’ but a result. In fact, the parameters you feed to the function ‘mysql_query()’ is written in ‘Structured Query Language’ (“SELECT * FROM products ORDER BY category DESC”). To better reflect this reality it would be better to write:
$sql = "SELECT *
FROM products
ORDER BY category
DESC";
$result = mysql_query($sql) OR die(mysql_error());
Now $sql contains Structured Query Language while $result contains the result returned by the SQL.