Page 1 of 1

new to functions

Posted: Fri May 30, 2003 11:33 am
by penguinboy
I created a function to apply css border atributes to <td>'s

Code: Select all

<?php
	function border($top, $right, $bottom, $left, $style, $col)
	{
		if($style==""){$style="solid";}
		echo "style="
		border-style: ".$style.";
		border-color: #".$col.";
		border-top-width: ".$top."px;
		border-right-width: ".$right."px;
		border-bottom-width: ".$bottom."px;
		border-left-width: ".$left."px;
		"";
	}#################### EOF
?>
when I call the function from my class file, it looks something like this

Code: Select all

<?php
print"
<td ";$Lib -> border(0,0,2,0,'',$color); print">some text</td>";
?>
Notice how I have to print and then close the print and then call the function and then print again.
The reason I have to do that is because I'm echoing in the function.
I'd really like to be able to do something like:

Code: Select all

<?php
print"
<td ".$Lib -> border(0,0,2,0,'',$color).">some text</td>";
?>
Sorta like you can do with some of php's built in functions.

like:

Code: Select all

<?php
print"
<td>".mysql_num_rows($sql)."</td>";
?>
I don't know much about functions though.
Is there some way I can return that information without echoing/printing it?
And would that solve my problem or is there something else I need to do?


Thanks for any help.

--pb

Posted: Fri May 30, 2003 12:39 pm
by twigletmac
Instead of doing the echo within the function - return a value from it:

Code: Select all

function border($top, $right, $bottom, $left, $style, $col) 
   { 
      if($style==""){$style="solid";} 
      $style_info = 'style=" 
      border-style: '.$style.'; 
      border-color: #'.$col.'; 
      border-top-width: '.$top.'px; 
      border-right-width: '.$right.'px; 
      border-bottom-width: '.$bottom.'px; 
      border-left-width: '.$left.'px; 
      "';
      return $style_info; 
   }####################
Mac

Posted: Fri May 30, 2003 12:53 pm
by penguinboy
alright!
thanks twigletmac!
thats exactly what I needed.

-pb