maccaj51
asked on
PHP For each - every two
Hi Experts,
I have this php code:
I need it to output two <td> every one <tr> like this
I cant seem to get a solution - please help!
Many thanks in advance
I have this php code:
<?php
$catTerms = get_terms('product_cat', array('hide_empty' => 0, 'orderby' => 'ASC', 'parent'=> 28)); ?>
<?php foreach($catTerms as $catTerm) : ?>
<td><a href="/product-category/<?php echo $catTerm->slug; ?>"><?php echo $catTerm->name; ?></a></td>
<?php endforeach; ?>
I need it to output two <td> every one <tr> like this
<tr>
<td></td>
<td></td>
</tr>
I cant seem to get a solution - please help!
Many thanks in advance
What will be your output?? Not just <tr><td></td><td></td></tr > I am talking about content?
ASKER
I havent explained myself very well... This just outputs the same array value side by side.
I want to loop through the whole array putting every two values into a row...
I want to loop through the whole array putting every two values into a row...
also u can replace line #9 in my code above with just <td></td>
ASKER
the output should be...
<tr>
<td><a href="/product-category/value1">Value1</a></td>
<td><a href="/product-category/value2">Value2</a></td>
</tr>
<tr>
<td><a href="/product-category/value3">Value3</a></td>
<td><a href="/product-category/value4">Value4</a></td>
</tr>
If you're looking to place the results in two columns, you'd be better off with a for loop.
However, this will only work if you have an even number of items. If you have or may have an odd number, you'd need to use an if statement for the second <td>.
<?php
$catTerms = get_terms('product_cat', array('hide_empty' => 0, 'orderby' => 'ASC', 'parent'=> 28)); ?>
<?php for($i = 0; $i < count($catTerms); $i += 2) : ?>
<tr><td><a href="/product-category/<?php echo $catTerms[i]->slug; ?>"><?php echo $catTerm->name; ?></a></td>
<td><a href="/product-category/<?php echo $catTerms[i+1]->slug; ?>"><?php echo $catTerm->name; ?></a></td></tr>
<?php endfor; ?>
However, this will only work if you have an even number of items. If you have or may have an odd number, you'd need to use an if statement for the second <td>.
<?php
$catTerms = get_terms('product_cat', array('hide_empty' => 0, 'orderby' => 'ASC', 'parent'=> 28)); ?>
<?php for($i = 0; $i < count($catTerms); $i += 2) : ?>
<tr><td><a href="/product-category/<?php echo $catTerms[i]->slug; ?>"><?php echo $catTerm->name; ?></a></td>
<?php if ($i + 1 < count($catTerms) ) { ?>
<td><a href="/product-category/<?php echo $catTerms[i+1]->slug; ?>"><?php echo $catTerm->name; ?></a></td>
<?php } ?>
</tr>
<?php endfor; ?>
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Now I got what you want , please trythis code and let me know
try this
try this
<?php
$catTerms = get_terms('product_cat', array('hide_empty' => 0, 'orderby' => 'ASC', 'parent'=> 28)); ?>
$counter = 0;
<?php
foreach($catTerms as $catTerm) :
if ( $counter/2 == intval ( $counter/2 ) ) echo "<tr>" ;
?>
<td><a href="/product-category/<?php echo $catTerm->slug; ?>"><?php echo $catTerm->name; ?></a></td>
<?php
if ( $counter/2 == intval ( $counter/2 ) ) echo "</tr>";
$counter++;
endforeach;
?>
@honestman31: Use the % operator. It's a much cleaner and more efficient way to do exactly what you're doing:
Multiple math operations and intval() casting:
if( $counter/2 == intval($counter/2) )
One math operation:
if( ($counter % 2) == 0)
Or for even shorter syntax:
if(!($counter % 2))
Multiple math operations and intval() casting:
if( $counter/2 == intval($counter/2) )
One math operation:
if( ($counter % 2) == 0)
Or for even shorter syntax:
if(!($counter % 2))
ASKER
Many thanks for everyones help
Open in new window