-2

How can I make this foreach line only go through the first result?

<?php foreach ($collection as $product) : ?>

I tried the following, but that does not work. It than does not display any results:

<?php foreach (array_slice($collection, 0, 1) as $product) : ?>

EDIT:

The following works perfect:

<?php 
$i = 0; 
foreach ($collection as $product) : 
    if($i < 1) {
        Inner content
    } 
    $i++; 
endforeach;
?>

Total current code:

<tbody>
    <tr>
        <td class="image" rowspan="3">      
        <?php $i = 0; foreach ($collection as $product) : if($i < 1) {?>
            <img src="<?php echo $this->helper('catalog/image')->init($product, 'thumbnail')->resize(75) ?>" alt="<?php echo $this->htmlEscape($product->getName()) ?>" width="75" height="75" />
        <?php } $i++; endforeach ?>
        </td>
        <td class="order" colspan="2">order</td>
        <td class="exclTax">excl. vat</td>
        <td class="inclTax">incl. vat</td>
    </tr>                     

    <?php foreach ($collection as $product) : ?>
    <tr>
        <td class="select"><input type="radio" name="featured_1807" id="featured_1807_1349567899" value="3071895, IM" data-product-id="3071895" data-product-sup="IM"></td>
        <td class="title"><a href="<?php echo $abstractBlock->getProductUrl($product) ?>" class="" tooltip="" title=""><?php echo $this->htmlEscape($product->getName()) ?></a></td>
        <td class="price"><?php echo $abstractBlock->getPriceHtml($product, true, '-related') ?></td>
        <td class="priceIncl"><?php echo $abstractBlock->getPriceHtml($product, true, '-related') ?></td>
    </tr>
    <?php endforeach ?>
</tbody>

How can I achieve that?

JGeer
  • 1,768
  • 1
  • 31
  • 75
  • 1
    Should work fine. Please provide us a [mcve] – Rizier123 Dec 02 '16 at 14:34
  • The problem will be in the code you run INSIDE the foreach, not in the foreach line itself **Show more code** – RiggsFolly Dec 02 '16 at 14:35
  • 1
    Come to think of it, if you only want the first occurance processed why foreach at all. Why not use `$collection[0]` – RiggsFolly Dec 02 '16 at 14:36
  • @RiggsFolly Thanks, I updated my question, that works perfect. What am I missing here? You mean ``? – JGeer Dec 02 '16 at 14:38
  • Why do you start and stop PHP on each line. Makes for **totally unreadable and unmaintainable code** – RiggsFolly Dec 02 '16 at 14:40
  • And No I ment if you are only interested in whats in `$collection[0]` forget the loop completely just use `$collection[0]['whatever']` in the code you currently have inside the loop – RiggsFolly Dec 02 '16 at 14:41
  • @RiggsFolly Thanks, sorry. Already edit it. But is my current way a good solution? – JGeer Dec 02 '16 at 14:42
  • Possible duplicate of [Get first key in a (possibly) associative array?](http://stackoverflow.com/questions/1028668/get-first-key-in-a-possibly-associative-array) – Dan Abrey Dec 02 '16 at 15:39

2 Answers2

1

Why bother with a loop if you are only interested in the first occurance of $collection

You could do this instead and probably not have to change any code you currently have inside your loop

<?php 

$product = $collection[0];

Code you had in the loop
?>

If its not a numeric key then you can use

<?php
reset($collection);  // make sure you are on first occ
$product = $collection[key($collection)];

... html stuff

// and then do again for your second loop 
// if you only want the first occ of that as well

reset($collection);  // make sure you are on first occ
$product = $collection[key($collection)];
RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
  • Thanks. I tried that following, but that does not work: ` ` – JGeer Dec 02 '16 at 14:51
  • 1
    In what way _Does not work_ There is lots going on in that line of code – RiggsFolly Dec 02 '16 at 14:54
  • Get the following php error on frontend: `Fatal error: Uncaught Error: Cannot use object of type AW_Autorelated_Model_Product_Collection` – JGeer Dec 02 '16 at 14:55
  • And that was working before when you wrote the for loop? – RiggsFolly Dec 02 '16 at 14:55
  • Then this code should work as its doing exactly what you foreach loop is doing – RiggsFolly Dec 02 '16 at 14:58
  • Show this line of code as you had it written inside you foreach loop – RiggsFolly Dec 02 '16 at 14:59
  • Well inside your code, I placed exactly the same as I did in my edit code. To display the image. But is my currenty working way a bad solution to fix this? Otherwise I will use that. – JGeer Dec 02 '16 at 15:02
  • 1
    It does seem a little unnecessary to run a loop and stop it after 1 iteration. But its your app – RiggsFolly Dec 02 '16 at 15:04
  • Thanks, you are right about that. Maybe this is the problem. Inside the same template file, I still use the following `` to load the products as well. But before that code I want to load the code of only 1 result. – JGeer Dec 02 '16 at 15:08
  • Now you are asking me questions about code I cannot see. So I will have to say _"Thats a definite maybe!"_ – RiggsFolly Dec 02 '16 at 15:10
  • You are right. I updated my question with the total current code. – JGeer Dec 02 '16 at 15:11
  • Ah, thinking about it, If `$collection` is not numerically indexed that woudl be a problem! Do a `print_r($collection);` and tell me what the index is I bet its a string of some sort! – RiggsFolly Dec 02 '16 at 15:24
0
reset($collection); // Resets the array's internal pointer to the first element
$product = current($collection); // Returns the current element which is now first element

As far as I'm concerned, this is the best solution I would ever go for if I need to only extract the first element from an array.

Wolverine
  • 1,712
  • 1
  • 15
  • 18