Jump to content

Column / Row structure in php table


mattix

Recommended Posts

Hello, 
I think this question is a bit of both php and mysql related. I hope I can get some help here. Especially with the mysql part. 

 

I have a table like this:

CREATE TABLE days (
    id INT AUTO_INCREMENT,
      daya INT,
      dayb INT,
    PRIMARY KEY (id)
);


INSERT INTO days VALUES
    (70, 19, 18),
    (69, 20, 17),
    (68, 24, 25),
    (67, 19, 18),   
    (66, 19, 17),    
    (65, 17, 16);


and mysql query and php snippet is this:

 $sql = "SELECT
    d1.id AS id1,
    d2.id AS id2,
    IF((d1.daya-d2.daya)=1,1,0) +
    IF((d1.daya-d2.dayb)=1,1,0) +
    IF((d1.dayb-d2.daya)=1,1,0) +
    IF((d1.dayb-d2.dayb)=1,1,0)   AS tcount
FROM days AS d1 , days AS d2
WHERE d1.id IS NOT NULL AND d2.id IS NOT NULL AND d1.id > d2.id";

  $result = $conn->query($sql);
  if ($result->num_rows > 0) {

   while($row = $result->fetch_assoc()) 
   {
        echo "<tr><td>" . $row["id1"] . "</td><td>". $row["id2"] . "</td><td>" . $row["tcount"]. 
                        "</td></tr>";   
             
    }
echo "</table>";


which gives me a table like this:

Id1  Id2   Count
50    49    1
50    48    0
49    48    0
50    47    1
49    47    1
48    47    0
50    46    1
49    46    1
48    46    0
47    46    1
50    45    1
49    45    1
48    45    0
47    45    1
46    45    1

I need to convert the table into this form: id1 goes down the first column, and id2 is in the first row.  How can i do this, please? can you give me some help? thank you!

id2    49    48    47    46    45
id1                    
50     1      0     1     1     1
49            0     1     1     1
48                  0     0     0
47                        1     1
46                              1
45                    

 

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.