Solved

PHP For each - every two

Posted on 2013-06-26
10
449 Views
Last Modified: 2013-06-26
Hi Experts,

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; ?>     

Open in new window


I need it to output two <td> every one <tr> like this

<tr>
        <td></td>
        <td></td>
      </tr>

Open in new window


I cant seem to get a solution - please help!

Many thanks in advance
0
Comment
Question by:maccaj51
  • 3
  • 3
  • 2
  • +2
10 Comments
 
LVL 10

Expert Comment

by:honestman31
ID: 39277751
is it what u want or am i missing something

<?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>

  <td><a href="/product-category/<?php echo $catTerm->slug; ?>"><?php echo $catTerm->name; ?></a></td>


<?php endforeach; ?>  

Open in new window

0
 
LVL 15

Expert Comment

by:Jagadishwor Dulal
ID: 39277762
What will be your output?? Not just <tr><td></td><td></td></tr> I am talking about content?
0
 

Author Comment

by:maccaj51
ID: 39277767
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...
0
 
LVL 10

Expert Comment

by:honestman31
ID: 39277769
also u can replace  line #9  in my code above   with just   <td></td>
0
 

Author Comment

by:maccaj51
ID: 39277774
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>

Open in new window

0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 22

Expert Comment

by:Kim Walker
ID: 39277788
If you're looking to place the results in two columns, you'd be better off with a for loop.
<?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; ?>

Open in new window


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; ?>

Open in new window

0
 
LVL 34

Accepted Solution

by:
gr8gonzo earned 500 total points
ID: 39277889
This is the approach I typically use. It lets me easily change the # of columns per row later if I ever need to (just change $columnsPerRow to the desired number) and the code is very readable.

<?php
$catTerms = get_terms('product_cat', array('hide_empty' => 0, 'orderby' => 'ASC', 'parent'=> 28)); ?>
$currentColumn = 0;
$columnsPerRow = 2;

<tr>
<?php foreach($catTerms as $catTerm) : ?>
       $currentColumn++;
       <td><a href="/product-category/<?php echo $catTerm->slug; ?>"><?php echo $catTerm->name; ?></a></td>
        <?php 
        // Start a new row every $columnsPerRow
        if(($currentColumn % $columnsPerRow) == 0)
        {
          echo "</tr><tr>";
        }
        ?>
<?php endforeach; ?>  
</tr>

Open in new window

0
 
LVL 10

Expert Comment

by:honestman31
ID: 39277909
Now I got what you want , please trythis  code and let me know
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; 
?>  

Open in new window

0
 
LVL 34

Expert Comment

by:gr8gonzo
ID: 39277947
@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))
0
 

Author Closing Comment

by:maccaj51
ID: 39277967
Many thanks for everyones help
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to dynamically set the form action using jQuery.

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now