Results 1 to 5 of 5
  1. #1
    Join Date
    Mar 2003
    Posts
    32

    Question Unanswered: Display row data as heading in coloumn

    hi,

    i need help to display record from mysql using php.
    i store my data on database like this

    table a
    row_id
    a
    b
    c
    d

    table b
    row_id | value
    a 10
    b 15
    b 16
    c 18


    and i need to display this record like this

    a | b | c
    10 15 0
    0 16 0

    is that anyone has code for problem like this?

    thanks

  2. #2
    Join Date
    Feb 2004
    Posts
    533
    I guess your talking about writing sql results to a html table. There are plenty of examples of this in the php manual.
    see ref.mysql.php and function.mysql-field-name.php

    Here's a function I pieced together to write out the html table.
    With the function use $headers = true or $headers = false if you don't want headers.
    PHP Code:
    // Performing SQL query
    $query 'SELECT * FROM dbtablename';
    $result mysql_query($query) or die('Query failed: ' mysql_error());
    sqltohtml($result$headers true); 
    PHP Code:
    function sqltohtml($result$headers=true)
    {
    if (
    $headers) {
    // ----------- Get the Field Names
    $numfields mysql_num_fields($result);
    echo 
    "<table border=\"1\">\n<tr>";
    for (
    $i=0$i $numfields$i++) // Header
    { echo '<th>'.mysql_field_name($result$i).'</th>'; }
    echo 
    "</tr>\n";
    } else {
    echo 
    "<table border=\"1\">\n";
    }

    while (
    $row mysql_fetch_row($result)) // Data
    { echo '<tr><td>'.implode($row,'</td><td>')."</td></tr>\n"; }

    // Printing results in HTML
    echo "<table border=\"1\">\n";
    while (
    $line mysql_fetch_array($resultMYSQL_ASSOC)) {
       echo 
    "\t<tr>\n";
       foreach (
    $line as $col_value) {
           echo 
    "\t\t<td>$col_value</td>\n";
       }
       echo 
    "\t</tr>\n";
    }
    echo 
    "</table>\n";
    }
    ?> 
    ~

    Bill

  3. #3
    Join Date
    Mar 2003
    Posts
    32
    thanks savbill, the function u given i already got. what is my problem is how about the data we apply as header not field name. any suggestion? tq

  4. #4
    Join Date
    Feb 2004
    Posts
    533
    Quote Originally Posted by zakyk
    what is my problem is how about the data we apply as header not field name. any suggestion? tq
    You could loop through your results set to reorder your data and create an array of data in your desired format. First task after returning the results of your select query would be to loop through rows and make an array of the first column to be used as headers and to index your values for the columns.

    1. query data
    2. create $colvals unique array of first column, use while loop
    3. create a muli-dimensional array with num of cols and rows to hold data
    4. loop through result rows and find index of col[0] value in the $colvals array
    this number will be uses as the col position of the values on each row to add the values in the new array
    5. loop through rows and cols of the re-ordered data in the new array to print the results in an html table.

    Heres the first part of getting the array of col1 values. If you get the rest of it or another solution post it.
    PHP Code:
    // Performing SQL query
    $query 'SELECT * FROM vallist';
    $result mysql_query($query) or die('Query failed: ' mysql_error());

    $colvals = array();

    while (
    $adata mysql_fetch_row($result)){
    array_push ($colvals$adata[0]) ;
    }
    $colvals array_unique($colvals);
    sort($colvals);

    // print results so far
    print_r ($colvals);
    exit; 
    ~

    Bill

  5. #5
    Join Date
    Apr 2005
    Location
    Baltimore, MD
    Posts
    297
    It's hard to tell just by what you've told us about your table structure, but there is most likely an easier way to do this using SQL queries rather than creating and sorting arrays in php. If not maybe you should consider restructuring your tables. Could you post the table structures? And also, the SQL query you're using to gather your data.

Posting Permissions

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