1

I have displayed my results in a table with 5 columns. The results read across the page then a <TR> starts a new line and the next row of results is displayed (see http://www.dsoba.co.uk/immemoriam/index2.php) Can I display them to first fill a column then the next column? Its easier to scan a column than a row.

            $result = mysqli_query($con,"SELECT * FROM memberlist where deceased='d' order by surname, initials");
            $tmp = 0; # initiate the tmp variable
    echo "<table class='table1'>
            <tr>";
        while($row = mysqli_fetch_array($result))
             {  # start of the while loop, once for each record

                # note removed the <BR> from the main output of the loop:
    echo "<td>" . $row['initials'] . " " . $row['surname'] . " " . $row['yearstart'] . " - " . $row['yearleft'] . "</td>";
        $tmp = ++$tmp; # increment 'tmp' by 1
        if ($tmp == 5)  {
        $tmp = 0;
    echo "</tr><tr>";  # new line if you've echoed 5 records
                        }

             }    # this is the end of the while loop


    echo "</tr></table>";

1 Answer 1

1

You can save your data into arrays per column.

$columns = [];
$i = 0;
while($row = mysqli_fetch_array($result)) {
    $columns[$i++ % INT_COLUMNS][] = $row;
}

foreach ($i = 0; $i < count($columns[0]); $i++) {
    echo '<tr>';
    foreach ($j = 0; $j < INT_COLUMNS; $j++) {
        if (isset($columns[$j][$i]) {
            echo '<td>...</td>';
        }
    }
    echo '</tr>';
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.