Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    New to the CF scene
    Join Date
    Apr 2007
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Formatting PHP output in HTML table

    Hallo!

    I have two tables in database "Category" and "SubCategory".
    I need some formatting output in HTML table.

    Here I show design on MySQL tables:

    PHP Code:
    CREATE TABLE `category` (
      `
    idint(4NOT NULL auto_increment,
      `
    nametext NOT NULL,
      
    PRIMARY KEY  (`id`)
    );

    INSERT INTO `category` (`id`, `name`) VALUES 
    (1'Web Programming'),
    (
    2'Database');

    CREATE TABLE `subcategory` (
      `
    idint(4NOT NULL auto_increment,
      `
    cat_idint(4NOT NULL default '0',
      `
    nametext NOT NULL,
      `
    descriptiontext NOT NULL,
      
    PRIMARY KEY  (`id`)
    ) ;

    INSERT INTO `subcategory` (`id`, `cat_id`, `name`, `description`) VALUES 
    (11'PHP''Personal Home Page'),
    (
    21'JavaScript''Client-Side Java Script Programming'),
    (
    32'MySQL''Structured Query Language'),
    (
    42'PostgreSQL''Beauty SQL'),
    (
    52'Microsoft SQL''ghjghjgh ghghjghj'); 
    I have use this PHP code to display output:

    PHP Code:
    $select "SELECT id, name FROM category";
    $query mysql_query ("$select") or die (mysql_error());

    echo 
    "<table border=1>"

    while (
    $row mysql_fetch_array ($query)) {

    $select_sub "SELECT name FROM subcategory WHERE cat_id=$row[0]";
    $query_sub mysql_query ("$select_sub") or die (mysql_error());

        echo 
    "<td>" "<b>" $row['name']. "</b>" "</td>";
                
            echo 
    "<tr><td bgcolor=\"#CCFF99\">";
                
                while (
    $row_sub mysql_fetch_array ($query_sub))
                {
                echo  
    "<u>" $row_sub['name'] . "</u>" "&nbsp;&nbsp;";
                }
            
            echo 
    "</tr></td>";
    }
    echo 
    "</table>"
    But this output in 1 column

    Web Programming
    PHP JavaScript CSS VB
    Database
    MySQL PostgreSQL Microsoft SQL

    But I want get output in row like:

    Web Programming-----Database
    PHP JavaScript CSS VB----MySQL PostgreSQL Microsoft SQL

    Any suggestion?
    Thanks!

  • #2
    Senior Coder rafiki's Avatar
    Join Date
    Aug 2006
    Location
    Floating around somewhere...
    Posts
    2,046
    Thanks
    19
    Thanked 42 Times in 42 Posts
    dont insert the id into the database table mysql will do that for you
    also i seen
    (1, 1, 'PHP', 'Personal Home Page'),
    PHP stands for
    Hypertext Pre Processor

  • #3
    Regular Coder
    Join Date
    Oct 2005
    Location
    Right Here
    Posts
    654
    Thanks
    1
    Thanked 0 Times in 0 Posts
    PHP Code:

    while ($row mysql_fetch_array ($query)) {
        
    //store id in array
        
    $id_array[] = $row[0];

        echo 
    "<td>" "<b>" $row['name']. "</b>" "</td>";
    }
    echo 
    "</tr><tr>";
    foreach(
    $id_array as $id) {
        
    $select_sub "SELECT name FROM subcategory WHERE cat_id=$id";
        
    $query_sub mysql_query ("$select_sub") or die (mysql_error());
                
            echo 
    "<td bgcolor=\"#CCFF99\">";
                
                while (
    $row_sub mysql_fetch_array ($query_sub))
                {
                echo  
    "<u>" $row_sub['name'] . "</u>" "&nbsp;&nbsp;";
                }
            
            echo 
    "</td>";
    }
    echo 
    "</tr></table>"
    This is one way although might not be proficient if you have a ton of values.

  • #4
    Senior Coder
    Join Date
    Jan 2007
    Posts
    1,648
    Thanks
    1
    Thanked 58 Times in 54 Posts
    PHP stands for
    Hypertext Pre Processor
    Actually, that would make HPP.

    http://www.php.net/manual/en/faq.gen...eneral.acronym

  • #5
    Senior Coder
    Join Date
    Jan 2007
    Posts
    1,648
    Thanks
    1
    Thanked 58 Times in 54 Posts
    The code posted is already horribly inefficient. A query for each category?

    Code:
    SELECT * FROM category c, subcategory sc WHERE sc.cat_id = c.id ORDER BY c.id ASC
    Now you'll get rows like this:

    Code:
    1    Web Programming     1    1     PHP     Personal Message
    ...
    Loop through these rows. Everytime the cat_id changes, echo out the header.

    This will get you what you had in a more efficient manner.

    Your example is a little confusing, so I'm not sure what you are trying to achieve.

    PHP Code:
    <table>
        <tr>
            <?php
            $row_count 
    0;
            
    $columns 3;
            while (
    $row mysql_fetch_assoc($result)) {
                if (
    $row_count == $columns) {
                    echo 
    "</tr><tr>";
                    
    $row_count 0;
                }
                echo 
    "<td> ....... </td>";
                
    $row_count++;
            }
            
    ?>
        </tr>
    </table>

  • #6
    Senior Coder rafiki's Avatar
    Join Date
    Aug 2006
    Location
    Floating around somewhere...
    Posts
    2,046
    Thanks
    19
    Thanked 42 Times in 42 Posts
    Quote Originally Posted by aedrin View Post
    Quote Originally Posted by PHP.NET
    Hypertext Preprocessor
    see..

  • #7
    Senior Coder
    Join Date
    Jan 2007
    Posts
    1,648
    Thanks
    1
    Thanked 58 Times in 54 Posts
    You didn't include the PHP part. And your capitalization suggested you meant Pre to be a seperate part.


  •  

    Posting Permissions

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