Foreach row display as column

40,856

Solution 1

I figured it out, turns out it's a pretty simple solution with few cycles and pre-populating an array before writing rows and cells.

I tested it, works like a charm. ;) It might be useful for someone, so here it is:

foreach($records as $key => $row) {
       foreach($row as $field => $value) { 
           $recNew[$field][] = $value;
       }
}
//This creates a new array composed/transposed with the field names as keys and
//the "rowed" values as sub-arrays.

echo "<table>\n";

foreach ($recNew as $key => $values) // For every field name (id, name, last_name, gender)
{
    echo "<tr>\n"; // start the row
    echo "\t<td>" . $key . "</td>\n" ; // create a table cell with the field name
        foreach ($values as $cell) // for every sub-array iterate through all values
        {
           echo "\t<td>" . $cell . "</td>\n"; // write cells next to each other
        }
    echo "</tr>\n"; // end row

}

echo "</table>";

Solution 2

Answer has been updated for adjusted original post requirements as follows:

this is a wierd (or elegant) way to get what you want done and it will likely take tweaking to get it to look pretty, but it seems to work as you can see at the following url

http://www.bluevineconsulting.com/table_test.php

it has the php div method on top and does indeed show that the div's are different sizes as you had hoped would not happen. I have however come up with elegant/ugly solution that works with a table.

// i populated a fake object so i could test with it
$data = new stdClass();
$data->student1->id = "1";
$data->student1->name = "Test1";
$data->student1->last_name = "User1";
$data->student1->gender = "Male";
$data->student2->id = "2";
$data->student2->name = "Test";
$data->student2->last_name = "User<br>\nMaiden<br>\nOther\n";
$data->student2->gender = "Female";
$data->student3->id = "3";
$data->student3->name = "Test3";
$data->student3->last_name = "User3";
$data->student3->gender = "Male";
$data->student4->id = "4";
$data->student4->name = "Test4";
$data->student4->last_name = "User4";
$data->student4->gender = "Female";
$data->student5->id = "5";
$data->student5->name = "Test5";
$data->student5->last_name = "User5";
$data->student5->gender = "Female";
$data->student6->id = "6";
$data->student6->name = "Test6";
$data->student6->last_name = "User6";
$data->student6->gender = "Female";
$datacount = count($data);


foreach($data as $row) {
    $datacount++;
    if (empty($colcount)) {
        foreach ($row as $key => $val) {
            $colcount++;
            $keyname[$colcount] = $key;
        }
        echo "Columns = ".$colcount."<br>\n";
     }
}
echo "records = ".$datacount."<br>\n<br>\n";
echo ("<b>PHP Table method:</b><br>\n<br>\n");
echo ("<table border='1px' cellspacing='0'>\n");
foreach ($keyname as $key) {
    echo "    <tr style='vertical-align:top;'>\n";
    for ($d = 1; $d < $datacount; $d++) {
        $id = "student".$d;
        echo "        <td>".$data->$id->$key."</td>\n";
     }
    echo "    </tr>\n";
}
echo ("</table>\n");

i have to admit it is strange, but it keeps your tables with the correct heights .. the next more complicated part would be to set how many columns to show as this method makes it much more complicated to break after 5 columns like the div method I had earlier.

Share:
40,856
Viktor
Author by

Viktor

/

Updated on August 24, 2020

Comments

  • Viktor
    Viktor over 3 years

    I have a pretty standard database like:

    id | name | last_name                | gender 
    -----------------------------------------------
    1  | John | Doe                      | Male
    2  | Jane | Smith Dolores Clayborne  | Female
    3  | Paul | Paulson                  | Male
    

    and I want to display it in a table, but every row in the DB needs to be a column in HTML table:

    id        | 1         | 2         | 3      
    ---------------------------------------------
    name      | John      | Jane      | Paul
    ---------------------------------------------
    last_name | Doe       | Smith     | Paulson
              |           | Dolores   |
              |           | Clayborne |
    ---------------------------------------------
    gender    | Male      | Female    | Male 
    

    If I go with:

    foreach($data as $row) {
    echo "<tr><td>" . $row->id . "</td></tr>";
    echo "<tr><td>" . $row->name . "</td></tr>";
    echo "<tr><td>" . $row->last_name . "</td></tr>";
    echo "<tr><td>" . $row->gender . "</td></tr>";
    }
    

    I get all the data in one long column. How do I break the column after every SQL row?

    Note: $data is an array of objects that contain properties with the field values (you can probably figure that out from the example).

    EDIT:

    I found the solution, see my answer, it's simple and elegant.