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 6 of 6
  1. #1
    Regular Coder
    Join Date
    Oct 2012
    Location
    mother land --india
    Posts
    165
    Thanks
    38
    Thanked 2 Times in 2 Posts

    column to row using php

    Hi Friends,

    First of all, Happy new year !!!.

    I required a php script to transform the display from column to row.

    example:

    PHP Code:

        Header             Value
        Ticket Number     Number
        Project Type     Number
               Opened date     date
        SLA deadline        date 

    as

    PHP Code:
       [B]Header[/B]  Ticket Number     Project Type   Opened date   SLA deadline
        
    [B]Value[/B]       Number               Number           date              date 

    please help me with the way?

  • #2
    Senior Coder
    Join Date
    Jan 2011
    Location
    Missouri
    Posts
    4,092
    Thanks
    23
    Thanked 593 Times in 592 Posts
    I think we need to know how the first example is generated. Please post code.

  • #3
    Regular Coder
    Join Date
    Oct 2012
    Location
    mother land --india
    Posts
    165
    Thanks
    38
    Thanked 2 Times in 2 Posts
    Quote Originally Posted by sunfighter View Post
    I think we need to know how the first example is generated. Please post code.

    that is a DB view !!!

    And I want to display in the form of column to row format.
    Last edited by nani_nisha06; 01-03-2013 at 05:02 AM.

  • #4
    Regular Coder
    Join Date
    Jul 2012
    Location
    London
    Posts
    473
    Thanks
    4
    Thanked 86 Times in 86 Posts
    This selects a table from a database and displays all of it's contents for formating in to what you want.

    example here: http://dev.tempz.webatu.com/test2/ - If you fill out the form the table will automatically refresh.

    PHP Code:
    <?php
    $db_host 
    'localhost'//host
    $db_user 'username'//username
    $db_pwd 'password'//Password for the account on myPhpAdmin

    $database 'databasename'//database name
    $table 'db_table'//Database table.

    if (!mysql_connect($db_host$db_user$db_pwd))
        die(
    "Can't connect to database");

    if (!
    mysql_select_db($database))
        die(
    "Can't select database");

    // sending query
    $result mysql_query("SELECT * FROM {$table}");
    if (!
    $result) {
        die(
    "Query to show fields from table failed");
    }

    $fields_num mysql_num_fields($result);

    echo 
    "<p>";
    echo 
    "<table border='1' class='dataTable'><tr>";
    // printing table headers
    for($i=0$i<$fields_num$i++)
    {
        
    $field mysql_fetch_field($result);
        echo 
    "<th>{$field->name}</th>";
    }
    echo 
    "</tr>\n";
    // printing table rows
    while($row mysql_fetch_row($result))
    {
        echo 
    "<tr>";

        
    // $row is array... foreach( .. ) puts every element
        // of $row to $cell variable
        
    foreach($row as $cell)
            echo 
    "<td>$cell</td>";

        echo 
    "</tr>\n";
    }
    mysql_free_result($result);
    ?>
    Enjoy!

  • #5
    Regular Coder
    Join Date
    Oct 2012
    Location
    mother land --india
    Posts
    165
    Thanks
    38
    Thanked 2 Times in 2 Posts
    Quote Originally Posted by tempz View Post
    This selects a table from a database and displays all of it's contents for formating in to what you want.

    example here: http://dev.tempz.webatu.com/test2/ - If you fill out the form the table will automatically refresh.

    PHP Code:
    <?php
    $db_host 
    'localhost'//host
    $db_user 'username'//username
    $db_pwd 'password'//Password for the account on myPhpAdmin

    $database 'databasename'//database name
    $table 'db_table'//Database table.

    if (!mysql_connect($db_host$db_user$db_pwd))
        die(
    "Can't connect to database");

    if (!
    mysql_select_db($database))
        die(
    "Can't select database");

    // sending query
    $result mysql_query("SELECT * FROM {$table}");
    if (!
    $result) {
        die(
    "Query to show fields from table failed");
    }

    $fields_num mysql_num_fields($result);

    echo 
    "<p>";
    echo 
    "<table border='1' class='dataTable'><tr>";
    // printing table headers
    for($i=0$i<$fields_num$i++)
    {
        
    $field mysql_fetch_field($result);
        echo 
    "<th>{$field->name}</th>";
    }
    echo 
    "</tr>\n";
    // printing table rows
    while($row mysql_fetch_row($result))
    {
        echo 
    "<tr>";

        
    // $row is array... foreach( .. ) puts every element
        // of $row to $cell variable
        
    foreach($row as $cell)
            echo 
    "<td>$cell</td>";

        echo 
    "</tr>\n";
    }
    mysql_free_result($result);
    ?>
    Enjoy!
    tempz,

    I am not looking to expose the table as it is in from DB rather trying to make a column data as headers..

  • #6
    Senior Coder
    Join Date
    Jan 2011
    Location
    Missouri
    Posts
    4,092
    Thanks
    23
    Thanked 593 Times in 592 Posts
    If you had said that in either post "I am not looking to expose the table as it is in from DB rather trying to make a column data as headers.. " you would have had an answer earlier:

    Code:
    <?php
    require ('./inc/DB_connect.php'); // YOUR CONNECTION HERE
    //require('../header.inc.php');
    
    $TABLENAME = "clan1";             // YOUR DATABASE NAME HERE
    $query = "SELECT * FROM $TABLENAME";
    $result = mysql_query($query);
    $colCount = mysql_num_fields($result);
    
    echo "<br /><TABLE BORDER=1>$TABLENAME<br />";
    
    echo "<tr>";
    for($colNuber = 0; $colNuber < $colCount; $colNuber++)
    {
    	$fieldName = mysql_field_name($result, $colNuber);
    	echo "<th>$fieldName</th>";
    }
    echo "</tr>";
    
    /*  IF YOU WANT VALUE DO IT HERE  */
    /*while($row = mysql_fetch_row($result))   
    {
    	echo "<tr align=left VALIGN=TOP>";
    	for($colNumber = 0; $colNumber < $colCount; $colNumber++)
    	echo "<td>$row[$colNumber]</td>";
    	echo "</tr>";
    }*/
    echo "</table><br />";
    ?>


  •  

    Posting Permissions

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