2

This is my first post, so I will be trying to be as thorough as possible. I am also very new to PHP.

This is a wordpress site using PODS CMS plugin so keep in mind the wordpress image uploader allows access to multiple sizes of one singular image upload. The concept is that I have a group of data called "team" and this group has three fields - images, title, bio. I will be generating a list of thumbnails for each team member in one containing unordered list and then in another containing div I will have a larger version of he image, the title, and the bio. I am basically making a tabbed content area where the thumbnails are the tabs

The ultimate HTML output would be:

<ul>
<li> <img src="thumbnailurl.jpg"/></li>
<li> <img src="thumbnailurl2.jpg"/></li>
<li> <img src="thumbnailurl3.jpg"/></li>
</ul>
<div class="panes">
<div><h2>Title 1</h2> <p> BIO CONTENT </p></div>
<div><h2>Title 1</h2> <p> BIO CONTENT </p></div>
<div><h2>Title 1</h2> <p> BIO CONTENT </p></div>
</div>

The current issue I am having is that I can get all of the image urls for the first record, but when it comes to the second record in the second foreach i need to re-run the array for the new record but I can not figure out how.

<?php
  $Record = new Pod('the_team');
  $Record->findRecords($orderby = 't.id DESC'); 
  $mylist=array();

  while ($Record->fetchRecord())
  {
    $image_array = $Record->get_field('photo');
    $title = $Record->get_field('name');
    $desc = $Record->get_field('bio');
    $id = $Record->get_field('id');
    $mylist[] = array('name' => $title, 'desc' => $desc, 'id'=> $id ); 
?>

<ul>
<?php
  foreach($image_array as $i => $image)
  {
    $image_thumb_url = wp_get_attachment_image_src( $image['ID'], 'thumbnail', false );
    $image_thumb_url = $image_thumb_url[0];

    $image_med_url = wp_get_attachment_image_src( $image['ID'], 'medium', false );
    $image_med_url = $image_med_url[0];

    $image_large_url = wp_get_attachment_image_src( $image['ID'], 'large', false );
    $image_large_url = $image_large_url[0];

    $image_full_url = wp_get_attachment_image_src( $image['ID'], 'full', false );
    $image_full_url = $image_full_url[0];
?>
  <li>
    <a href="<?php echo $image_large_url; ?>">
      <img src="<?php echo $image_thumb_url; ?>" />
    </a>
  </li>
<?php } ?>
<?php } ?>
</ul>

<div class="panes">
<?php  
  foreach ($mylist as $person)
  { ?>
    <div class="team-member" id="member<?php echo $person['id']; ?>">
      <h2><?php echo $person['name']; ?></h2>
      <?php echo $person['desc']; ?>
      <a href="<?php echo $person['photo'];  ?>">
        <img src="<?php echo $person['photo'];   ?>" />
      </a>
<?php } ?>
</div>
</div>

Okkkay.. So i have the first problem solved!!! But it brings up a second one. I am thinking I will either need a second image field OR call just the first image in the array in the <li> and just the second image in the array for the <div>:

<?php

$Record = new Pod('the_team');
$Record->findRecords($orderby = 't.id DESC');
$mylist=array();
$cnt = 0;
?>
   <ul class="tabs">
<?php 
while ($Record->fetchRecord()) :
  $mylist[$cnt] = array(
    'name' => $Record->get_field('name'),
    'desc' => $Record->get_field('bio'),
    'id'=> $Record->get_field('id')
  );
  ?>
  <?php
  $image_array = $Record->get_field('photo');
  foreach($image_array as $i => $image) :
    $image_thumb_url = wp_get_attachment_image_src( $image['ID'], 'thumbnail', false );
    $mylist[$cnt]['img_thumb'] = $image_thumb_url[0];

    $image_med_url = wp_get_attachment_image_src( $image['ID'], 'medium', false );
    $mylist[$cnt]['img_med'] = $image_med_url[0];

    $image_large_url = wp_get_attachment_image_src( $image['ID'], 'large', false );
    $mylist[$cnt]['img_large'] = $image_large_url[0];

    $image_full_url = wp_get_attachment_image_src( $image['ID'], 'full', false );
    $mylist[$cnt]['img_full'] = $image_full_url[0];
  ?>
    <li>
      <a href="#">
        <img src="<?php echo $image_thumb_url[0]; ?>" />
      </a>
    </li>
  <?php endforeach; ?>
<?php
  $cnt++;
endwhile;
?>  </ul>

  <div class="panes">
  <?php foreach ($mylist as $person) : ?>
    <div class="team-member" id="member<?php echo $person['id']; ?>"><div id="member-info"><h2>Meet <?php echo $person['name']; ?></h2>
      <?php echo $person['desc']; ?></div>
      <a href="<?php echo $person['img_large'];  ?>" rel="prettyPhoto">
        <img src="<?php echo $person['img_med'];   ?>" style="float:left" />
      </a>
    </div>
  <?php endforeach; ?>
  </div>
Adrian Heine
  • 4,051
  • 2
  • 30
  • 43
  • It's really difficult to tell what is going on here. Try to remove extraneous code (ex: you are setting many variables such as $image_med_url but never using them for anything) – Rob Apodaca Nov 16 '11 at 02:29
  • I'm still a bit confused by the question, when you say second record do you mean the second iteration of the while loop that would fetch the next record from `$Record->fetchRecord()`? – David Barker Nov 16 '11 at 03:08

1 Answers1

0

I think I can see what your problem is. Your second foreach loop doesn't benefit from the while loop, as such your only option if following this path would be to reloop through the fetched data. I wouldn't recommend doing this as there is nearly always a way you can write the code needed for the second loop into the original loop.

As an example of this I've re-written your code to incorporate the second foreach loop into the while loop (negating its need). This way, every record has a new entry into $html and $html2. Once it's looped through you have two variables that you can concat to produce the full html you are looking for.

<?php
$Record = new Pod('the_team');
$Record->findRecords($orderby = 't.id DESC');

// Custom variables 
$html = "<ul>\n";
$html2 = "";
$image_type=array('thumbnail','medium','large','full');

while ($Record->fetchRecord()) {

    $image_array = $Record->get_field('photo');
    $title = $Record->get_field('name');
    $desc = $Record->get_field('bio');
    $id = $Record->get_field('id');

    foreach($image_array as $i => $image) {

        foreach ($image_type as $type) {
            $image_temp =  wp_get_attachment_image_src( $image['ID'], $type , false );
            $image_url[$type] = $image_temp[0];
        } # End of Foreach loop

        // Create List HTML in primary output variable
        $html .= "<li>\n".
        "<a href=\"" . $image_url['large'] ."\">\n".
        "<img src=\"" . $image_url['thumbnail'] ."\" />\n".
        "</a>\n".
        "</li>\n";
    } #End of foreach loop

    // Create User HTML in secondary output variable
    $html2 .= "\t<div class=\"team-member\" id=\"member" . $id . ">\n".
              "<h2>" . $title . "</h2>\n".
              $desc . "\n".
              "<a href=\"" . $photo . ">\n". # $photo is not declared in this code (will error)
              "<img src=\"" . $photo . " />\n". # as above comment
              "</a>\n";
              "</div>\n";
} # End of while loop

// Wrap up the list elements, concat the secondary HTML.
$html .= "</ul>\n\n".
         "<div class=\"panes\">\n".
         $html2 . "\n".
         "</div>\n";

echo $html;
?>
David Barker
  • 14,484
  • 3
  • 48
  • 77
  • Ok, so I started by simple dropping your code in to my page just to see what would happen. It appears that it doesn't work (obviously as you said that the $photo wasnt defined) but it didn't event give an error at all. In looking at your code, the second loop also is not benefiting from the while statement as you have it closed before the $html .. am i correct? – Lindsay Branscombe Nov 16 '11 at 17:04
  • I've just added comments to show where the while loop closes. `$photo` would only produce an E_NOTICE (won't stop script running) as it is undeclared, meaning it has a default value in PHP of '' / 0 / false when used. I'll look back over the script for errors. – David Barker Nov 16 '11 at 17:14
  • I couldn't see anything that would stop the script from working however I haven't got an install of wordpress with PODS CMS installed to test it on. – David Barker Nov 17 '11 at 15:46
  • Well I had posted my solution but it was deleted by a moderator. I did, however come up with a solution using $cnt++; – Lindsay Branscombe Nov 17 '11 at 20:26