rich_traff Posted July 27, 2011 Share Posted July 27, 2011 I have this function that does 3 things; 1) determines how many modules are published to a certain row in a joomla template layout. 2) sets the module width accordingly 3) outputs the html The modules are labeled A through H, what i want returned is the html for every module position to be published, at the moment it only returns the last pass of the foreach loop - so it outputs the html for module H, if i unpublished module H it will only show module G etc… I know it makes sense for that to be happening from looking at the code as it is now but i don't know how to change it so it does what i need… So, i want $result, to return all the html from each pass of the second foreach loop, can anyone help? <?php function modWidth3($row, $siteWidth, $mod) { $published = 0; $array = array('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H'); // Calculates how many modules are published to row foreach($array as $position) { if($mod->countModules('topRow' . $row . '_' . $position)) { $published = $published+1; } } // Sets module width according to number published $pixelsUsed = $published * 15; $pixelsLeft = $siteWidth - $pixelsUsed; $modWidth = $pixelsLeft / $published; $result =''; // Outputs published modules foreach ($array as $position) { if($mod->countModules('topRow' . $row . '_' . $position)) { $result =' <div id="topRow' . $row . '_' . $position.'" class="modRow" style="width:'.$modWidth.'px;"> <jdoc:include type="modules" name="topRow' . $row . '_' . $position.'" style="xhtml" /> </div>'; } } return $result; } Link to comment https://forums.phpfreaks.com/topic/242933-function-only-returning-last-pass-of-foreach/ Share on other sites More sharing options...
Nodral Posted July 27, 2011 Share Posted July 27, 2011 each time you loop through the foreach you are effectively writing over the $ result variable. instead of $result =' <div id="topRow' . $row . '_' . $position.'" class="modRow" style="width:'.$modWidth.'px;"> <jdoc:include type="modules" name="topRow' . $row . '_' . $position.'" style="xhtml" /> </div>'; try $result .=' <div id="topRow' . $row . '_' . $position.'" class="modRow" style="width:'.$modWidth.'px;"> <jdoc:include type="modules" name="topRow' . $row . '_' . $position.'" style="xhtml" /> </div>'; This will concatenate the next instance in the loop onto the previous one. Link to comment https://forums.phpfreaks.com/topic/242933-function-only-returning-last-pass-of-foreach/#findComment-1247781 Share on other sites More sharing options...
rich_traff Posted July 27, 2011 Author Share Posted July 27, 2011 thats great, thanks very much! Link to comment https://forums.phpfreaks.com/topic/242933-function-only-returning-last-pass-of-foreach/#findComment-1247784 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.