Jump to content

Mixing two different arrays


onjegolders
 Share

Recommended Posts

Hi guys, come a little unstuck.

For a particular project on my new site, I'm creating a grid of images but inside that grid, I'd also like to include some extra text boxes (for which I'm using repeater fields).

I think I can use $var->add($new_array) to add one array to another but as the two types of boxes will have different fields I'm a bit unsure of the output.

I could also just loop through all the images, then all the boxes after within the same grid but then I wouldn't be able to mix the boxes up.

Does anyone have any idea how this could be accomplished?

Thanks guys.

Link to comment
Share on other sites

It sounds like these are all PageArrays? If the items in it are going to be of varying types, you could make the output loop check the type to determine how it's going to render it:

foreach($items as $item) {
  if($item->template == 'product') {
    // render a product
  } else if($item->template == 'repeater-photos') {
    // render a photo from a repeater
  } else {
    // ...and so on
  }
}

Another approach would be to let the items render themselves via their template files. Your product.php and repeater-photos.php templates (for example) could just be coded to render nothing but the markup for their individual item. Then your loop would be essentially this:

foreach($items as $item) {
  echo $item->render();
}
Link to comment
Share on other sites

Hi Ryan, thanks for the reply.

Actually, they'd both belong to the project.php template.

The grid would be made of $page->images and $page->project_boxes

So I'd need each iteration to output a <li class="grid_item"></li>

but within that would either be an <img> or another <div> depending on whether it was from the images or the repeater boxes.

Link to comment
Share on other sites

post-502-0-86697700-1363623880_thumb.pngIdeally I'd like a random order. With the text boxes mixed up in a grid with the images.

I was thinking it would be a matter of making one array and then sorting random. But as the contents of the two initial arrays are different...

Attached is a screenshot of the grid but as you can see I just have the boxes coming after the images

Link to comment
Share on other sites

How about this?

$array = array();

foreach($page->repeater as $r){
    $array[] =
<<<EOT
    <div>
        <h2>{$r->headline}</h2>
        <p>{$r->body}</p>
    </div>
EOT;
}

foreach($page->images as $i){
    $array[] = "<img src='{$i->url}'/>";
}

shuffle($array);

foreach($array as $a){
    echo $a;
}

edit: the forum editor seems not to approve the heredoc sintax, and because I don't want any conflicts with it, here is the alternative way :)

$array = array();

foreach($page->repeater as $r){
    $array[] = "<div><h2>{$r->headline}</h2><p>{$r->body}</p></div>";
}

foreach($page->images as $i){
    $array[] = "<img src='{$i->url}'/>";
}

shuffle($array);

foreach($array as $a){
    echo $a;
} 
  • Like 4
Link to comment
Share on other sites

  • 2 months later...

Thought I'd reuse this old thread as have an interesting quandry.

I have a template where there is more than one date field assigned to it, what I'd like to do is to be able to combine these and order a list by date.

At present, I can't think of a way of doing this natively with PW. Any bright ideas? Cheers.

Link to comment
Share on other sites

Hi Diogo,

Each project has a due date but also other date fields. What I'm trying to achieve is a sort of "log" for that project where I can see a chronological list of all dates linked to this project.

The problem is it would need to combine more than one date field before ultimately sorting them as one array (I guess).

I've thought about creating a new array of all the dates but then they'd lose association with their respective fields.

Ultimately it should look something like

1st January - New Years Day

5th January - Started project

11th January - Started design phase

18th January - Project due

25th January - Sent final draft to client

Link to comment
Share on other sites

If I understood well, I don't know how to do that in a PW way, but this should take you there:

$datefields = $page->fields->find("type=FieldtypeDatetime");
$dates = array();

foreach ($datefields as $f) {
    $dates[$f->name] = $page->$f;
}

asort($dates);

foreach ($dates as $k => $v) {
    echo $k . "=>" . $v;
    echo "<br>";
}
 

edit: changed from filter() to find() in the first line

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