SitePoint Sponsor

User Tag List

Results 1 to 5 of 5

Thread: Table Trouble

  1. #1
    SitePoint Member
    Join Date
    Apr 2011
    Location
    South Africa
    Posts
    24
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    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:

    HTML Code:
    // 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.

  2. #2
    SitePoint Mentor bronze trophy
    John_Betong's Avatar
    Join Date
    Aug 2005
    Location
    City of Angels
    Posts
    1,834
    Mentioned
    73 Post(s)
    Tagged
    6 Thread(s)
    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}

    PHP Code:

    // 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";
      } 
    Learn how to be ready for The New Move to Discourse

    How to make Make Money Now with a *NEW* look

    Be sure to congratulate Patche on earning Member of the Month for July 2014

  3. #3
    SitePoint Member
    Join Date
    Apr 2011
    Location
    South Africa
    Posts
    24
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Thank you so much, I am going to give it a try!

  4. #4
    SitePoint Evangelist captainccs's Avatar
    Join Date
    Mar 2004
    Location
    Caracas, Venezuela
    Posts
    516
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    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:
    PHP Code:
    $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:
    PHP Code:
    $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
    Denny Schlesinger
    web services

  5. #5
    SitePoint Member
    Join Date
    Apr 2011
    Location
    South Africa
    Posts
    24
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Thank you so much for that Denny. I have managed to fix my tables. I learn something new every day. :-)


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
  •