Jump to content

Getting a specific repeater row and its fields


landitus
 Share

Recommended Posts

Hi! I'm currently working on a project that lists chairs. Each chair comes in multiple colors. Each chair has a repeater field with 2 fields: an upload a photo field and a select (which is a Page field) so you can associate the photo with a color from the select.

From the Color perspective. Each color has been aplied to a number of chairs, and I want to have a page for the color "Red" and list all red chairs but show the red color image of that chair.

So I need to find all chairs which are in red (this gets selected with another field but not inside a repeater). Then inside the loop I look for a repeater instance that has the color red. Everything good till the point I don't know how to find the photo adjacent to the color red. That is, the field in the same line of the repeater. I only need the full row which is the red thumbnail and leave the rest behind,

I think I might be over complicating somehow... I'm really lost. Is this the right way?

I guess this might be difficult to explain. Let me paste some code and an image for the Chair element with the repeater:

$chairs = $pages->find('colores='.$page->name); 
    $i = 0;
    if($chairs) {
        foreach($chairs as $chair) {
        
        $i++;
        $additionalClass = ($i % 3) == 0 ? " last" : "";
    ?>
        <div class="item <?php echo $additionalClass ;?>">
            <a href="<?php echo $chair->url ;?>">    
            <?php 
                $chair_color_photos = $chair->producto_color_thumbs; // The repeater field. Contains rows with color names -> thumbnail
                $color_thumb = ''; // Find the red color name and photo
                if($chair_color_photos) {
                    foreach ($chair_color_photos as $color_photo) {
                        if($color_photo->producto_color_thumbs_opcion == $page) { // $page is Red
                            $color_thumb = $color_photo->producto_color_thumbs_opcion;
                        }
                    }
                }
                if($color_thumb) {
                    // Find the Red photo ??
                }
                elseif($chair->producto_thumb) {
                    echo "<img src='{$chair->producto_thumb->url}' alt='' />";
                }
                echo "<hgroup class='descripcion'>";
                    echo "<h3>{$chair->title}</h3>";
                    echo "<h4>{$chair->summary}</h4>";
                echo "</hgroup>";
            ?>
            </a>
        </div><!-- .item -->
    <?php    
        }        
    }
    

post-72-0-22671600-1380568843_thumb.png

Link to comment
Share on other sites

$color = $pages->get("template=color-option, name=red"); // Or where you keep your colors actually

// Just a placeholder
$p = new NullPage();

// Loop all your repeater rows
foreach($page->repeaters as $rep) {
  // Check for the wanted color
  if ($rep->color == $color) {
    $p = $rep; // And when found, put it to our variable
    continue; // No need to go further
  }
}

if ($p->id) {
  echo $p->img->url; 
} else {
  echo "No $color chairs found";
}

I read and wrote pretty quickly, but hopefully above could be in some help?

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...