Quantcast
Channel: Active questions tagged html - Stack Overflow
Viewing all articles
Browse latest Browse all 67469

How to limit the number of columns of the table created from PHP while loop

$
0
0

I have this table created from php while loop from MySQL. The problem is the column goes outside the page. Using CSS word-wrap did not work.

enter image description here

Here's what I am looking for

enter image description here

The original code. Sorry for the ugly code.

<?php
// 1
$sqlCount = "SELECT MAX(rowascol_id) AS totalcount FROM rowascol";
$resultCount = mysqli_query($con, $sqlCount);
$row = mysqli_fetch_assoc($resultCount);
    $totalCount = $row['totalcount'];

// 2
$sqlRowAsCol = "SELECT * FROM rowascol";
$resultRowAsCol = mysqli_query($con, $sqlRowAsCol);
?>

<table id="myTable" border="1" cellpadding="6" style="border-collapse: collapse; text-align: center;">
    <thead style='background-color: #cee8ff;'>
        <?php 
        for($i = 1; $i <= $totalCount; $i++){
            echo "<th>$i</th>";
        }?>
    </thead>
    <tbody>
        <?php 
        while($row = mysqli_fetch_array($resultRowAsCol)) {
            $data['col1'][] = $row['n1'];
            $data['col2'][] = $row['n2'];
            $data['col3'][] = $row['n3'];
            $data['col4'][] = $row['n4'];
            $data['col5'][] = $row['n5'];
            $data['col6'][] = $row['n6'];
            $data['ans'][] = $row['ans'];
            $data['edit'][] = $row['rowascol_id'];
        }?>

        <!-- looping the table -->
        <?php
        // 1
        echo "<tr>";
            foreach ($data['col1'] as $col1) {
                echo "<td>$col1</td>";
            }
        echo "</tr>";
        echo "<tr>";
            foreach ($data['col2'] as $col2) {
                echo "<td>$col2</td>";
            }
        echo "</tr>";
        echo "<tr>";
            foreach ($data['col3'] as $col3) {
                echo "<td>$col3</td>";
            }
        echo "</tr>";
        echo "<tr>";
            foreach ($data['col4'] as $col4) {
                echo "<td>$col4</td>";
            }
        echo "</tr>";
        echo "<tr>";
            foreach ($data['col5'] as $col5) {
                echo "<td>$col5</td>";
            }
        echo "</tr>";
        echo "<tr>";
            foreach ($data['col6'] as $col6) {
                echo "<td>$col6</td>";
            }
        echo "</tr>";
        echo "<tr  id='ansCell' style='font-weight: bold;'>";
            foreach ($data['ans'] as $ans) {
                echo "<td>$ans</td>";
            }
        echo "</tr>";
        echo "<tr>";
            foreach ($data['edit'] as $edit) {
                echo "<td>";
                    echo "<a href='edit.php?id=".$edit."'>Edit</a>";
                echo "</td>";
            }
        echo "</tr>";
         ?>
    </tbody>
</table>

Viewing all articles
Browse latest Browse all 67469

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>