1

I want to make it so that every 3 columns it forms a new row. I am lost and can't figure out how to do this even after reading other posts.So I'd want it to be like

icon icon icon
icon icon icon
icon icon icon
etc....

foreach($sMenu as $row) {
    $sClass = ($row['id'] == $aPage['id']) ? ' class="ui-btn-active ui-btn-custom"' : ' class="ui-btn-custom"';
    $sIcon = ($row['icon'] != '') ? ' data-icon="' . $row['icon'] . '"' : '';

    $sSiteRoot = get('site-root');

    $row['url'] .= ($row['url'] == '') ? '' : '/';

    $url = $sSiteRoot . $row['url'];
    $url = str_replace('(', '%28', $url);
    $url = str_replace(')', '%29', $url);
    $url = str_replace("'", '%27', $url);
    $sNavigation2 .= '<td><img src=". $sIcon .' . $url . '"></td>';
}
3
  • Sometimes the easiest way to do this is to have a variable keep track of how many times you run this loop ($i = 0). Then, just check to see if the number is equal to 2 (if ($i == 2)) then... just add your </tr><tr> break (or whatever you need...) and reset the $i variable. Then, keep going through the foreach loop you have. Commented Jan 29, 2012 at 4:22
  • So where would I implement that into the code. would it be Commented Jan 29, 2012 at 4:24
  • I just put that comment into an answer below... and hopefully the code on that is easier to read. :) Commented Jan 29, 2012 at 4:25

4 Answers 4

5

Sometimes the easiest way to do this is to have a variable keep track of how many times you run this loop. For example:

$i = 0;

Then, inside of the loop, just check to see if the number $i is equal to 2 (in this case.)

foreach($sMenu as $row) {

    // this is all your old code right here...

    if ($i == 2) { 
        // then add your </tr><tr> break or whatever...
        $i = 0;
    } else {
        $i++;
    }
}

Then, just keep going through the foreach loop.

This can also be done using the modulus operator as a cleaner way to write this "row break" check (as shown by the other answers here.)

Sign up to request clarification or add additional context in comments.

2 Comments

+ incrementing $i at the end of each loop
This is just one way to do it... :)
2

Use modulus (%) and output a new row if a counter is evenly divisible by 3:

$i = 0;

foreach($sMenu as $row) {
    $i += 1;

    $sClass = ($row['id'] == $aPage['id']) ? ' class="ui-btn-active ui-btn-custom"' : ' class="ui-btn-custom"';
    $sIcon = ($row['icon'] != '') ? ' data-icon="' . $row['icon'] . '"' : '';

    $sSiteRoot = get('site-root');

    $row['url'] .= ($row['url'] == '') ? '' : '/';

    $url = $sSiteRoot . $row['url'];
    $url = str_replace('(', '%28', $url);
    $url = str_replace(')', '%29', $url);
    $url = str_replace("'", '%27', $url);
    $sNavigation2 .= '<td><img src=". $sIcon .' . $url . '"></td>';

    if( $i % 3 == 0 ) {
        $sNavigation2 .= '</tr><tr>';
    }
}

Comments

0

you can make use of the "key" as index in array:

foreach($sMenu as $index=>$row)
{
    if(($index)%3==0){$sNavigation2 .= "<tr>";}
    //$sClass = ($row['id'] == $aPage['id']) ? ' class="ui-btn-active ui-btn-custom"' : ' class="ui-btn-custom"';
    //$sIcon = ($row['icon'] != '') ? ' data-icon="' . $row['icon'] . '"' : '';

    //$sSiteRoot = get('site-root');

    //$row['url'] .= ($row['url'] == '') ? '' : '/';

    //$url = $sSiteRoot . $row['url'];
    //$url = str_replace('(', '%28', $url);
    //$url = str_replace(')', '%29', $url);
    //$url = str_replace("'", '%27', $url);
    //$sNavigation2 .= '<td><img src=". $sIcon .' . $url . '"></td>';
    if(($index+1)%3==0){$sNavigation2 .= "</tr>";}
}
if(count($sMenu)%3 != 2){$sNavigation2 .= "</tr>";}

Comments

0

One way to check for every three items is to use the modulo operator, which checks for a remainder upon division.

Here's some psuedocode to get you started:

$counter = 0;
// start the first row
$html = '<tr>';
foreach( $sMenu as $row) {
    //add an item 
    $html .= '<td>' . $row[ 'id' ] . '</td>';
    //increment the counter, which is used to keep track of the number of items
    $counter++;
    //if $counter/3 has zero as a remainder, it's divisible by three
    if( $counter % 3 === 0 ) {
       //end the row after 3 items and begin a new one
       $html .= '</tr><tr>';
    }
}
//make sure there's an ending <tr> in case it ended on an odd number of items
$html = preg_replace( '/<tr>$/gi', '', $html );
$html .= '</tr>'; 

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.