Returning data from functions?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • KevinUK
    New Member
    • Aug 2006
    • 3

    #1

    Returning data from functions?

    Hi, I have this function which currently outputs the data but I would like the class calling this function to handle the output instead. How do I go about doing that?

    Code:
    function comments($number) {
    	
    		$query = "SELECT id,title,comment FROM comment ORDER BY id DESC";
    		$result = @mysql_query($query) or die('No ID found');
    				
    		$count = 0;
    		
    		while (($row = mysql_fetch_assoc($result)) && ($count < $number)) {
    		
    			echo $row['title'] . " ";
    			echo $row['comment'] . "<br/>";
    		
    			$count++;		
    		}
    		mysql_free_result($result);
    	}
    Main class:
    Code:
    <?php	lastfive::comments(3);	?>
    I want to be doing the echo part of the code in the main class instead so my comments function can be more generic.

    Thanks, Kevin
    Last edited by KevinUK; Aug 2 '06, 01:53 PM.
  • KevinUK
    New Member
    • Aug 2006
    • 3

    #2
    Oh that function is a mess, its now:

    Code:
    function comments($number) {
    	
    		$query = "SELECT id,title,comment FROM comment ORDER BY id DESC LIMIT $number";
    		$result = @mysql_query($query) or die('No ID found');
    	
    		while ($row = mysql_fetch_assoc($result)) {
    		
    			echo $row['title'] . " ";
    			echo $row['comment'] . "<br/>";					
    		}
    		mysql_free_result($result);
    	}

    Comment

    • KevinUK
      New Member
      • Aug 2006
      • 3

      #3
      Well I've done it, is there any optimisation I can do on the class that calls the method though? Seems like I could narrow down the code needed.

      [PHP]
      $result = lists::comments (3);

      while ($row = mysql_fetch_ass oc($result)) {

      echo $row['title'];
      echo $row['comment'] . "<br/>";
      }[/PHP]

      Comment

      • Banfa
        Recognized Expert Expert
        • Feb 2006
        • 9067

        #4
        Not suree quite what you mean by that last post, and the code seems quite compact to me already.

        Comment

        Working...