ankh2054 Posted May 23, 2014 Share Posted May 23, 2014 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. Link to comment Share on other sites More sharing options...
Soma Posted May 23, 2014 Share Posted May 23, 2014 $discuss_image_logo .= your're concatenating on and on through the foreach. Add a cleaner somewhere before those $discuss_image_logo = ''; Link to comment Share on other sites More sharing options...
ankh2054 Posted May 23, 2014 Author Share Posted May 23, 2014 thanks Soma, I used the below. unset($discuss_image_logo); Link to comment Share on other sites More sharing options...
Soma Posted May 23, 2014 Share Posted May 23, 2014 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. 2 Link to comment Share on other sites More sharing options...
Soma Posted May 23, 2014 Share Posted May 23, 2014 Edit: maybe even simpler since you don't need to concatenate the string is just write $discuss_image_logo = "..."; and not $discuss_image_logo .= "..."; Link to comment Share on other sites More sharing options...
ankh2054 Posted May 23, 2014 Author Share Posted May 23, 2014 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 More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now