SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Member
    Join Date
    Jan 2012
    Posts
    20
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    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> &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 $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?

  2. #2
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    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.

    PHP Code:

    $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.

  3. #3
    SitePoint Wizard lorenw's Avatar
    Join Date
    Feb 2005
    Location
    was rainy Oregon now sunny Florida
    Posts
    1,104
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    try
    PHP Code:

    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
    PHP Code:
    <table>
    <?
    echo $product_list
    ?>
    </table>

    hth
    What I lack in acuracy I make up for in misteaks


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •