Compounded Variable

Below is an extract from a great cart script I have come accross which stores returned variables in a compounded variable $product_list .

<?php
$product_list = “”;
$sql = mysql_query(“SELECT * FROM products ORDER BY date_added DESC”);
$productCount = mysql_num_rows($sql); if ($productCount > 0) {
while($row = mysql_fetch_array($sql)){
$id = $row[“id”];
$product_name = $row[“product_name”];
$price = $row[“price”];
$date_added = strftime(“%b %d, %Y”, strtotime($row[“date_added”]));
$product_list .= “Product ID: $id - <strong>$product_name</strong> - $$price - <em>Added $date_added</em>       <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”;
}
?>

Then $product_list is echoed out later in the script.

<?php echo $product_list; ?> which echos out all the results one after another.

Depending upon the length of each variable in $product_list, the output is displayed with different lengths.

Just curious but is there a way of separating the variables in $product_list . to enable them to be put inside a table?

The code you showed is concatenating individual values to a string, and presumably this is what you want.

If you are asking “how can I also reuse that data somewhere else?” then one answer is to capture the original query as an array variable and then create your string as you did, but to then go on and reuse the array to output a table somewhere else in your script.



$product_list = "";

$sql = mysql_query("SELECT * FROM products ORDER BY date_added DESC");

$rows = mysql_fetch_array($sql);  // $rows is a new var holding the array

if (count($rows)) {  // check count of array here instead of $productCount

foreach($rows as $row)){ // use foreach() construct
    $id = $row["id"];
    $product_name = $row["product_name"];
    $price = $row["price"];
    $date_added = strftime("%b %d, %Y", strtotime($row["date_added"]));
    $product_list .= "Product ID: $id - <strong>$product_name</strong> - $$price - <em>Added $date_added</em> &nbsp; &nbsp; &nbsp; <a     href='inventory_edit.php?pid=$id'>edit</a> &bull; <a href='inventory_list.php?deleteid=$id'>delete</a><br />";
}
} else {
    $product_list = "You have no products listed in your store yet";
}

// then, somewhere else in your script:

foreach($rows as $row)){ // use foreach() construct
// output your table
}

However, if your Q was “How do I output the whole thing as a table instead of a string?”, then all you have to do is alter the concatenation in the loop - where you use the string to collect up all the bits of your table.

try



while($row = mysql_fetch_array($sql)){ 
$id = $row["id"];
$product_name = $row["product_name"];
$price = $row["price"];
$date_added = strftime("%b %d, %Y", strtotime($row["date_added"]));
$product_list .= "<tr><td>Product ID: $id </td><td> - <strong>$product_name</strong>  </td><td>- $$price </td><td> - <em>Added $date_added</em> &nbsp; &nbsp; &nbsp; </td><td> <a href='inventory_edit.php?pid=$id'>edit</a> </td><td> &bull; <a href='inventory_list.php?deleteid=$id'>delete</a> </td></tr>";
}


then


<table>
<?
echo $product_list
?>
</table>

hth