SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Enthusiast
    Join Date
    Jun 2007
    Posts
    54
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Help Displaying Resultset in Chart/Table Format

    Hello All --

    I need some help with this.

    I am trying to write a PHP page that generates a table with numerous rows and columns. I have stored all column and row entries in MYSQL and can retrieve and display them.

    My problem is that every cell can potentially have multiple links to PDF files. I have resultsets set up now that is retrieving results for entire rows. Within this resultset, I have a field referencing a column name. There can be multiple instances of this in this resultset. I need to be able to group the records that match colmumn name and print those out in that cell.

    Example: Say I have a table with rows a, b and c and columns d, e, and f.

    I have three resultsets that retrieve all records for each given row:

    Code:
    select * from table where row='a'
    select * from table where row='b'
    select * from table where row='c'
    This retrieves all my records for each of those rows. I have multiple records in each that refer to the columns. Say for example, 2 references for d, 3 for e and 4 for f.

    So my question is, how can I loop through each of my resultsets so 2 entries print out in column d, 3 in e and 4 in column f?

    I appreciate any guidance you guys could provide.

    Thanks!

    -- Chris

  2. #2
    SitePoint Zealot
    Join Date
    Sep 2007
    Posts
    136
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code SQL:
    SELECT * FROM TABLE WHERE ROW='a' OR ROW='b' OR ROW='c' ORDER BY COLUMN

    make sense?

  3. #3
    SitePoint Enthusiast
    Join Date
    Jun 2007
    Posts
    54
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It does, yes - thats what I already have done, in fact.

    What I'm trying to do is loop through my resultset and print out the results with the same column identifier together.

    So, in your example, I might have 2 column d, 3 column e and 4 column f. How can I loop through the resultset and print out each column in its own <td> cell?

  4. #4
    SitePoint Zealot
    Join Date
    Sep 2007
    Posts
    136
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by ccorbett View Post
    It does, yes - thats what I already have done, in fact.

    What I'm trying to do is loop through my resultset and print out the results with the same column identifier together.

    So, in your example, I might have 2 column d, 3 column e and 4 column f. How can I loop through the resultset and print out each column in its own <td> cell?
    Ohhhh, I see, here's some pseudocode for what I think you're looking for

    Code PHP:
    /*
    Assuming all your rows are loaded into an associative array() called $rows
    */
     
    $indexedRows = array();
     
    foreach($rows as $row) {
     
        $indexedRows[$row['column']][] = $row;
     
    }
     
    $currentColumn = null;
     
    foreach($indexedRows as $key => $data) {
     
        echo "<td>Data for Column: ".$key;
     
        foreach($data as $row) {
            print_r($row); //or whatever you want to display         
        }
     
        echo "</td>";
    }

  5. #5
    SitePoint Enthusiast
    Join Date
    Jun 2007
    Posts
    54
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks gimbles... I'll give that a try on Monday...

    I've never worked with loading a resultset into an array before - is that hard to do?

    I'm assuming it would happen before the start of your pseudocode?

    Thanks again!

  6. #6
    SitePoint Enthusiast
    Join Date
    Nov 2007
    Posts
    41
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by gimbles View Post
    Code SQL:
    SELECT * FROM TABLE WHERE ROW='a' OR ROW='b' OR ROW='c' ORDER BY COLUMN

    make sense?
    better

    select * from table where row in ('a','b','c') ORDER BY column

  7. #7
    SitePoint Enthusiast
    Join Date
    Jun 2007
    Posts
    54
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by teng View Post
    better

    select * from table where row in ('a','b','c') ORDER BY column
    If I do it this way, can I still group them together as gimbles suggested, or is there a different way?


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
  •