My function to output html is not working...

Hi, can anyone tell me why my function isn’t working?

Simply put it;
counts how many modules are published (from a joomla template)
sets a module width
outputs the html

The code works fine when i use it via an include, but when i try to put it into a function, the page stops loading when it gets to the function call… Can anyone tell me why this is happening?

<?php
$var = modWidth($row, $siteWidth);
echo $var;
<?php 
function modWidth($row, $siteWidth)
{	
	$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($this->countModules('topRow' . $row . '_' . $position)) 
		{
			$published = $published+1;
		}
	}
		
	// Sets module width according to number published
	$pixelsUsed = $published * 15;
	$pixelsLeft = $siteWidth - $pixelsUsed;
	$modWidth = $pixelsLeft / $published;
		
	// Outputs published modules
	foreach ($array as $position) 
	{
		if($this->countModules('topRow' . $row . '_' . $position)) 
		{?>
			<div id="<?php echo'topRow' . $row . '_' . $position?>" class="modRow" style="width:<?php echo $modWidth;?>px;">
			<jdoc:include type="modules" name="<?php echo'topRow' . $row . '_' . $position?>" style="xhtml" />
			</div>
		<?php
		}
	}
	
}
<?php error_reporting(E_ALL); ?>

Put this at the top of your page. What does it output?

First of all sorry about my English.

Your problem are that you are writting at html (echo) inside function and try to restuned this values…

You must save the content in a variable
[php]$temp =’’;
// Outputs published modules
foreach ($array as $position)
{
if($this->countModules(‘topRow’ . $row . ‘’ . $position))
{
$temp.="


<jdoc:include type=‘modules’ name='topRow".$row."".$position."’ style=‘xhtml’ />
";

  }

}
return $temp;[/php]

I suppossed that this is not entirely code, because this code works, but it’s not correct to use in other case.

@kikesv - Sorry but this hasn’t solved it, the page is still failing to output anything after the function is called

@lothop - where should this <?php error_reporting(E_ALL); ?> be placed? i put it in the header, inside the body tag and right at the top and got nothing each time… would it make a difference that im developing on my localhost (mamp)?

The problem are $this->countModules() the variable $this are declarated out of function. If you want to use it inside, you must pass as parameter

[php]

<?php function modWidth($row, $siteWidth,$obj) { $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($obj->countModules('topRow' . $row . '_' . $position)) { $published = $published+1; } } // Sets module width according to number published $pixelsUsed = $published * 15; $pixelsLeft = $siteWidth - $pixelsUsed; $modWidth = $pixelsLeft / $published; $temp =''; // Outputs published modules foreach ($array as $position) { if($obj->countModules('topRow' . $row . '_' . $position)) { $temp'
'; } } return $temp; } [/php] [php] <?php $var = modWidth($row, $siteWidth,$this); echo $var; ?>[/php]

@kikesv - thats great, the page is now loading properly. However - the function is only outputting the html from the last pass of the foreach loop, ie - only loading the module in position ‘H’, if i unpublish module H, only position ‘G’ is outputted.

I need it to output the html for every position of the loop, A through H, how would i make this happen please?

Sorry,for you to put in the $temp .=.

[php]

<?php function modWidth($row, $siteWidth,$obj) { $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($obj->countModules('topRow' . $row . '_' . $position)) { $published = $published+1; } } // Sets module width according to number published $pixelsUsed = $published * 15; $pixelsLeft = $siteWidth - $pixelsUsed; $modWidth = $pixelsLeft / $published; $temp =''; // Outputs published modules foreach ($array as $position) { if($obj->countModules('topRow' . $row . '_' . $position)) { $temp.='
'; } } return $temp; } [/php]

@kikesv - thanks very much, you’ve been a great help!

Sponsor our Newsletter | Privacy Policy | Terms of Service