0

Hi guys Good day, this codes works perfectly for me.

<?php 
// This block grabs the whole list for viewing
$product_list = "";
$sql = mysql_query("SELECT * FROM product 

               ORDER BY date_added DESC");


$productCount = mysql_num_rows($sql); // count the output amount
if ($productCount > 0) {
  while($row = mysql_fetch_array($sql)){ 
             $id = $row["id"];
       $product_name = $row["name"];
       $details = $row["details"];
       $size = $row["size"];
       $price = $row["price"];
       $quantity = $row["quantity"];
       $date_added = strftime("%b %d, %Y", strtotime($row["date_added"]));
       $product_list .= "Product ID: $id 
                        - <strong>$product_name</strong> - 
                        Php$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";
      }

?>

My problem is i want to refactor this part and put it into table in order to give a pleasant look:

$product_list .= "Product ID: $id 
                    - <strong>$product_name</strong> - 
                    Php$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 />";

My Screenshots for my desired output.
http://img7.uploadhouse.com/fileuploads/17419/1741996659880843c6a2ae94919531384505f7a1.jpg

Here is what i did to refactor but it did not give me a good result:

 $product_list .= "?> 
                                <html>
                                <table >
                                    <tr>
                                        <td>
                                            Product ID: <?php echo $id ?>
                                        </td>
                                        <td>
                                             <?php echo $product_name; ?>
                                        </td>
                                        <td>
                                            Php <?php echo $price; ?>  
                                        </td>
                                        <td> 
                                            Added <?php echo $date_added; ?>
                                        </td>
                                        <td>
                                             <?php echo <a href='inventory_edit.php?pid=$id'>edit</a> • ?>
                                        </td>
                                        <td>
                                            <?php echo <a href='inventory_list.php?deleteid=$id'>delete</a><br />"; ?>
                                        </td>
                                    </tr>



                                 </table>

:help: pls help me to solve my desired output,, thanks

Edited by pritaeas: Updated title

4
Contributors
7
Replies
32
Views
4 Years
Discussion Span
Last Post by diether.silverious
0

It would be something like this

// would go before the loop
echo "<table><tr><td>Product ID</td><td>Product Name</td><td>Price</td><td>Added</td><td>Action</td></tr>";

// would go inside the loop
echo "<tr><td>$id</td><td>$product_name</td><td>$price</td><td>$date_added</td><td><a href='inventory_edit.php?pid=$id'>edit</a><a href='inventory_list.php?deleteid=$id'>delete</a></td></tr>";

// would go after the loop
echo "</table>";

Edited by pixelsoul

1

First start the table and add a header row:

<table >
<tr><th>Product ID</th><th>Items</th><th>Price</th><th>Date</th><th>Action</th></tr>

Then add the rows with data:

<?php
while($row = mysql_fetch_array($sql)){

    $id = $row["id"];
    $product_name = $row["name"];
    $details = $row["details"];
    $size = $row["size"];
    $price = $row["price"];
    $quantity = $row["quantity"];
    $date_added = strftime("%b %d, %Y", strtotime($row["date_added"]));

    echo "<tr>";
    echo "<td>$id</td>";
    echo "<td>$product_name</td>";
    echo "<td>$price</td>";
    echo "<td>$date_added</td>";
    echo "<td><a href='inventory_edit.php?pid=$id'>edit</a> • ";
    echo "<a href='inventory_list.php?deleteid=$id'>delete</a></td>";
    echo "</tr>";
}
?>

</table>

Edited by broj1

0

Since broj1 took the time to echo out all of the rows like I usually would have, I recommend his as well.

0

Try:

   <table border="1" width="400" align="center" class="table">
      <tr>
     <th>Product ID</th><th>Items</th><th>Price</th><th>Added</th><th>Action</th>                            
     </tr>                            
     <tr>                            
     <td><?php echo $id ?></td><td><?php echo $product_name; ?></td><td><?php echo $price;?></td>
     <td><?php echo $date_added; ?></td>
        <td><?php echo "<a href='inventory_edit.php?pid=$id'>edit</a> •" ?><?php echo "<a href='inventory_list.php?deleteid=$id'>delete</a>"; ?></td>
     </tr>
     </table>

should be near the mark also use

.table{
   border-collapse:collapse;
   border:#0920D6;
}

in your css to change the 3d table border to asingle line if you want.

2

The reason I put the table headers etc into an echo is so I can run them inside of the conditional statement that checks if any rows are returned. Then if rows returned equals zero I just don't display the table and include the "no results" message instead.

Votes + Comments
Good point
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.