Jump to content
sigmahokies

Multiple Columns in while or foreach loop.

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

Share this post


Link to post
Share on other sites

If you post your code we can see what you are doing and where you are going wrong

Share this post


Link to post
Share on other sites

Didn't you post this same question last month?

 

At least post some code for us to work with.

Share this post


Link to post
Share on other sites

Oh sorry, I forget about my first thread about while and foreach loop...I asked administrator to remove this thread, Thank you for remind me...

Share this post


Link to post
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>";
}

?>

Share this post


Link to post
Share on other sites

Hi Guru,

 

your code is working, but it is horizon data. I can take horizon data, but I prefer vertical data. If there is possible, Let me know.

 

Thanks,

Gary

Share this post


Link to post
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>";
}
?>

Share this post


Link to post
Share on other sites

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.