Jump to content

Multiple Columns in while or foreach loop.


sigmahokies

Recommended Posts

Hi everyone,

 

I am still learning PHP, I am getting better a little, but i am still struggle with while and foreach loop to create few columns. I create one column with while or foreach, but I couldn't figure to create few columns intsead of one long column. Can you help me or make any suggest?

 

Thank you so much

Since we are in the php forum could use array_chunk() to split the array of results into chunks.

 

You can also do this with html or css.

Table,style,css...get creative.

 

As simple as can explain it without getting crazy styling it.

<?php
$html = array();

$mysqli = new mysqli("localhost", "user", "password", "database");

$sql    = "SELECT column_name FROM table_name";
$result = $mysqli->query($sql);

while ($row = $result->fetch_assoc()) {
    $html[] = "<td>" . $row['column_name'] . "</td>";
}


$html = array_chunk($html, 3); // 3 chunks
?>


<!DOCTYPE html>

<html>

<head>

</head>
<table>

<?php
foreach ($html as $chunk) {
    echo "<tr>" . implode('', $chunk) . "</tr>";
}

?>
</table>

</html>

an array using array_chunk and css

<?php
//how many display columns
$number_columns = 8;

//dummy array data
$array          = range(1, 100);

//percentage for css division of page width
$percentage     = floor(100 / $number_columns);

//create chunks from the array calculating amount in array and number of columns
$columns        = array_chunk($array, ceil(count($array) / $number_columns));

//loop the chunked columns
foreach ($columns as $chunk) {

    //opted to make a divider and style
    echo "<div style='padding:10px;'>";

    //loop each array value within the chunk
    foreach ($chunk as $values) {

        //opted to make a paragraph and style
        echo "<p style='display:block;float:left;width: " . $percentage . "%;'>" . $values . "</p>";
    }
    echo "</div>";
}

?>

Put the left float on the <div>s and not the <p> tags. Also removed the padding.

<?php
//how many display columns
$number_columns = 8;

//dummy array data
$array          = range(1, 100);

//percentage for css division of page width
$percentage     = floor(100 / $number_columns);

//create chunks from the array calculating amount in array and number of columns
$columns        = array_chunk($array, ceil(count($array) / $number_columns));

//loop the chunked columns
foreach ($columns as $chunk) {

    //opted to make a divider and style
    echo "<div style='float:left;width: " . $percentage . "%;'>";

    //loop each array value within the chunk
    foreach ($chunk as $values) {

        //opted to make a paragraph and style
        echo "<p>" . $values . "</p>";
    }
    echo "</div>";
}
?>

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.