Jump to content

Imagefield is multiplying when trying to display multiple pages using foreach


ankh2054
 Share

Recommended Posts

Hi,

I am using the following code to display multiple pages and adding the pages respective image OR default image if no image is available.

But what seems to happen is that for each loop an additional image is added.

Hope that make sense, I've added a screenshot for clarity.

// Pull all discussion pages

$discussions = $pages->find("template=discussions"); 
foreach($discussions as $item) { 

//Load and assign Image

if ($item->discuss_image->url){
$discuss_image = $item->get("discuss_image");
$discuss_logo =  $discuss_image->size(200,200, $image_options);
$discuss_image_logo .= "<img class='img-rounded img-responsive' src='{$discuss_logo->url}'' alt='{$discuss_image->description}'' />";

}
    else {
        $discuss_image_logo .= "<img class='img-rounded img-responsive' src='{$config->urls->templates}/styles/images/bom-mashien_logo.png' alt='BOMmachine' />";
    }

//Theme each page                    
echo "

        
          <a href='{$item->url}' class='list-group-item'>
                <div class='media col-md-3'>
                    <figure class='pull-left'>
                        {$discuss_image_logo}
                    </figure>
                </div>
                <div class='col-md-6'>
                    <h4 class='list-group-item-heading'>{$item->title}</h4>
                    <p class='list-group-item-text'>{$item->body}</p>
                </div>
                <div class='col-md-3 text-center'>
                    <h2> 14240 <small> votes </small></h2>
                    <button type='button' class='btn btn-default btn-lg btn-block'> Vote Now! </button>
                    <div class='stars'>
                        <span class='glyphicon glyphicon-star'></span>
                        <span class='glyphicon glyphicon-star'></span>
                        <span class='glyphicon glyphicon-star'></span>
                        <span class='glyphicon glyphicon-star'></span>
                        <span class='glyphicon glyphicon-star-empty'></span>
                    </div>
                    <p> Average 4.5 <small> / </small> 5 </p>
                </div>
          </a>
       

  
     ";  
}

thanks in advance for any help.

post-2239-0-35396600-1400842554_thumb.pn

Link to comment
Share on other sites

Not a very ideal way to deal with it.

Thing is the variable isn't defined before you start to concat it... enable debug mode and you'll see Notice the first loop like:

Notice: Undefined variable: discuss_image_logo

The thing with unset() is that the variable isn't there anymore. So, even worse you'll get a PHP Notice on every loop.

So simply do it like this.

foreach($discussions as $item) { 
     $discuss_image_logo = '';
     $discuss_image_logo .= "<image ...>";
     ....
}

And everything is fine.


We should start a new Forum with PHP Programming Support, cause this has nothing to do with ProcessWire not template nor API. :D

  • Like 2
Link to comment
Share on other sites

I see thanks for the info :) that make sense. 

Updated the code as following:

/ Pull all discussion pages

if ($item->discuss_image->url){
$discuss_image = $item->get("discuss_image");
$discuss_logo =  $discuss_image->size(200,200, $image_options);
$discuss_image_logo = "<img class='img-rounded img-responsive' src='{$discuss_logo->url}'' alt='{$discuss_image->description}'' />";

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

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...