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>';
}
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.)
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>';
}
}
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>";}
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>';