SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Enthusiast
    Join Date
    Jun 2004
    Location
    Gold Coast, Australia
    Posts
    35
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    noobie foreach loop question

    Hey folks,

    I'm not experienced with php and trying to modify some templates within a project I'm working on. I currently have the following to work with. I want it to present the results 3 per row instead of 1 line per row as it currently is.

    Code:
    <?php $perRow = 3; $first = true; $odd = true; foreach ($zm_resultList->getResults() as $product) { ?>
    <tr>
        <td class="cpt"><p class="product-image"><a href="<?php zm_product_href($product->getId()) ?>" id="product_<?php echo $product->getId() ?>" class="product"><?php zm_image($product->getImageInfo(), PRODUCT_IMAGE_MEDIUM) ?></a></p>
            <a href="<?php zm_product_href($product->getId()) ?>"><?php echo $product->getName() ?></a><br/>
            <?php zm_more(zm_strip_html($product->getDescription(), false), 120) ?>
    		<?php zm_format_currency($product->getPrice()) ?>
        </td>
    </tr>
                    <?php $first = false; $odd = !$odd; } ?>
    If anyone could give me some guidance here I'd really appreciate it.

  2. #2
    Theoretical Physics Student bronze trophy Jake Arkinstall's Avatar
    Join Date
    May 2006
    Location
    Lancaster University, UK
    Posts
    7,062
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Try this:

    <?php
    $currRes = 0;
    $perRow = 3;
    $first = true;
    $odd = true;
    foreach ($zm_resultList->getResults() as $product) {
    if($currRes &#37; 3 == 0){
    echo "<tr>";
    }?>
    <td class="cpt"><p class="product-image"><a href="<?php zm_product_href($product->getId()) ?>" id="product_<?php echo $product->getId() ?>" class="product"><?php zm_image($product->getImageInfo(), PRODUCT_IMAGE_MEDIUM) ?></a></p>
    <a href="<?php zm_product_href($product->getId()) ?>"><?php echo $product->getName() ?></a><br/>
    <?php zm_more(zm_strip_html($product->getDescription(), false), 120) ?>
    <?php zm_format_currency($product->getPrice()) ?>
    </td>
    <?php
    if($currRes % 3 == 2){
    echo "</tr>";
    }
    $first = false; $odd = !$odd; }
    ?>
    Jake Arkinstall
    "Sometimes you don't need to reinvent the wheel;
    Sometimes its enough to make that wheel more rounded"-Molona

  3. #3
    rajug.replace('Raju Gautam'); bronze trophy Raju Gautam's Avatar
    Join Date
    Oct 2006
    Location
    Kathmandu, Nepal
    Posts
    4,013
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Little bit changed the way that i always do like:
    Code php:
    $perRow = 3;
    $counter = 1;
    echo '<tr>';
    foreach($zm_resultList->getResults() as $product){
    	echo '<td class="cpt">';
    	echo '<p class="product-image">';
    	echo '<a href="' . zm_product_href($product->getId()) . '" id="product_' . $product->getId() . '" class="product">' . zm_image($product->getImageInfo(), PRODUCT_IMAGE_MEDIUM) .'</a>';
    	echo '</p>';
    	echo '<a href="' . zm_product_href($product->getId()) .'">' . $product->getName() .'</a>';
    	echo '<br/>';
    	echo zm_more(zm_strip_html($product->getDescription(), false), 120);
    	echo zm_format_currency($product->getPrice());
    	echo '</td>';
    	if(($counter &#37; $perRow) == 0){echo '</tr><tr>';}
    }
    echo '</tr>';
    Mistakes are proof that you are trying.....
    ------------------------------------------------------------------------
    PSD to HTML - SlicingArt.com | Personal Blog | ZCE - PHP 5


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •