0

I want to create a row if columns greater than 3 using PHP loop because i am using wordpress

My code is here

<div class="row">
    <div class="column">column1</div>
    <div class="column">column2</div>
    <div class="column">column3</div>
</div>

If columns are greater than 3, then it should create a new row like this

<div class="row">
    <div class="column">column1</div>
    <div class="column">column2</div>
    <div class="column">column3</div>
</div>

<div class="row">
    <div class="column">column1</div>
  </div>

Any help will be highly appreciated.

Thanks in advance

1
  • You mean Columns right...? or am I completely wrong? Commented Oct 17, 2013 at 8:25

3 Answers 3

4

Sure - just use modulus:

<?php
    $elements = array('foo', 'bar', 'rab', 'oof');

    echo '<div class="row">';
    foreach ($elements as $i => $element) {
        if ($i > 0 && $i % 3 == 0) {
            echo '</div><div class="row">';
        }
        echo '<div class="column">' . $element . '</div>';
    }
    echo '</div>';
?>

DEMO

Output:

<div class="row">
    <div class="column">foo</div>
    <div class="column">bar</div>
    <div class="column">rab</div>
</div>
<div class="row">
    <div class="column">oof</div>
</div>
Sign up to request clarification or add additional context in comments.

4 Comments

if i have unlimited post?
@Sajal Then this works as well? There's no count anywhere that would indicate otherwise?
$elements = array('foo', 'bar', 'rab', 'oof'); what to write on the place of foo, bar ect, sorry but i am new in php
@Sajal Your own array? Where would you get coloum1, coloum2, coloum3.
1

You need something like this:

<?
echo '<div class="row">';
for ($i=0; $i<15;$i++){
    if ($i%3 == 0 && $i != 0){
        echo '</div><div class="row">';
    }
    echo '<div class="column">column '.($i+1).'</div>';

}
echo '</div>';
?>

WORKING CODE

7 Comments

I don't think, that is what the OP meant.
Your double quote is invalid. Change the "row" to 'row' Example, echo "</div>\n<div class='row'>";
i i dont want to fix lenth of coloum $i<15 if i dont want to give 15?
@Sajal this is only example. I don't know how many items do You have. If You have array for that - just use foreach loop with some counter.
Sorry to bother you, but i am new in php, if you can give me an example how to set counter
|
0

There is alternative solution with function.

<?php
// Managing the Code -- Functions: Handling a Variable Number of Parameters
// building a row of 10 <td> columns with a variable number of items
function preferencesRow()
{
    // initialize $output
    $output = '';
    // use "func_get_args()" to collect all parameters into an array
    $params = func_get_args();
    // used to make sure 10 columns are filled
    $maxCols = 10;

    // loop through the parameters
    foreach ($params as $item) {
        $output .= "<td width='80px' align='center'>$item</td>\n";
        $maxCols--;
    }

    // fill in the rest of the row with empty columns
    for ($x = 0; $x < $maxCols; $x++) {
        $output .= "<td width='80px'>&nbsp;</td>\n";
    }
    return $output;
}

// NOTE: you can use "." or "," with echo
echo '<h1>Preferences</h1><hr />' . PHP_EOL;    
echo '<table border=1>' . PHP_EOL;
echo '<tr><th>Tarzan</th>';
echo preferencesRow('Africa', 'jungles', 'tantor', 'mangani', 'cabin in the woods', 
                    'hunting', 'swinging in trees', 'Jane');
echo '</tr>';
echo '<tr><th>Jane</th>';
echo preferencesRow('London', 'parties', 'dancing', 'social events', 'lavish estates');
echo '</tr>';
echo '</table>' . PHP_EOL;

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.