Table Trouble

Hello everyone,

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> &bull; <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.

Hi SusanV,

I would be tempted to add one column and get your table to do what you want it to do then gradually add more columns.

This may help:

// CSS Stuff
table tr td {text-align_center}



// 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)
  {
    echo '<table>';
      echo '<thead>'
           .'<tr>' 
           .  '<th>category</th>'
           .  '<th>Subcategory</th>' 
           .  '<th>Product Name</th>' 
           .  '<th>Price</th>'
           .  '<th>edit</th>'
           .  '<th>delete</th>'
           .'</tr>'
           .'</thead>';

      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"]));

         $edit     = "<a href='inventory_edit.php?pid=" .$id ."'>edit</a> &bull;"; 
         $delete  = "<a href='inventory_list.php?deleteid=" .$id ."'>delete</a>";
         echo '<tr>' 
                .   '<td>' .$category       .'</td>' 
                .   '<td>' .$subcategory    .'</td>' 
                .   '<td><strong>' .$product_name .'</strong></td>' 
                .   '<td>' .$price .'</td>' 
                .   '<td>' .$edit .'</td>'
                .   '<td>' .$delete .'</td>'
                . '</tr>';
      }
    echo '</table>';
    
  } else {
  $product_list = "You have no products listed in your store yet";
  }

Thank you so much, I am going to give it a try!

Hello Susan:

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.

Further, MySQL is going to be deprecated. It’s a good idea to upgrade to mysqli. See http://php.net/manual/en/function.mysql-error.php

Thank you so much for that Denny. I have managed to fix my tables. I learn something new every day. :slight_smile: