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

Link to comment
Share on other sites

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>";
}

?>
Link to comment
Share on other sites

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>";
}
?>
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.