473,397 Members | 1,949 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

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?

Expand|Select|Wrap|Line Numbers
  1. function comments($number) {
  2.  
  3.         $query = "SELECT id,title,comment FROM comment ORDER BY id DESC";
  4.         $result = @mysql_query($query) or die('No ID found');
  5.  
  6.         $count = 0;
  7.  
  8.         while (($row = mysql_fetch_assoc($result)) && ($count < $number)) {
  9.  
  10.             echo $row['title'] . " ";
  11.             echo $row['comment'] . "<br/>";
  12.  
  13.             $count++;        
  14.         }
  15.         mysql_free_result($result);
  16.     }
Main class:
Expand|Select|Wrap|Line Numbers
  1. <?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
Aug 2 '06 #1
3 1361
Oh that function is a mess, its now:

Expand|Select|Wrap|Line Numbers
  1. function comments($number) {
  2.  
  3.         $query = "SELECT id,title,comment FROM comment ORDER BY id DESC LIMIT $number";
  4.         $result = @mysql_query($query) or die('No ID found');
  5.  
  6.         while ($row = mysql_fetch_assoc($result)) {
  7.  
  8.             echo $row['title'] . " ";
  9.             echo $row['comment'] . "<br/>";                    
  10.         }
  11.         mysql_free_result($result);
  12.     }
Aug 2 '06 #2
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_assoc($result)) {

echo $row['title'];
echo $row['comment'] . "<br/>";
}[/PHP]
Aug 3 '06 #3
Banfa
9,065 Expert Mod 8TB
Not suree quite what you mean by that last post, and the code seems quite compact to me already.
Aug 3 '06 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

7
by: Pablo J Royo | last post by:
Hello: i have a function that reads a file as an argument and returns a reference to an object that contains some information obtained from the file: FData &ReadFile(string FilePath); But ,...
12
by: Olumide | last post by:
I'm studying Nigel Chapman's Late Night Guide to C++ which I think is an absolutely fantastic book; however on page 175 (topic: operator overlaoding), there the following code snippet: inline...
10
by: Fraser Ross | last post by:
I need to know the syntax for writing a reference of an array. I haven't seen it done often. I have a class with a member array and I want a member function to return an reference to it. ...
6
by: Generic Usenet Account | last post by:
Is it okay to return a local datastructure (something of type struct) from a function, as long as it does not have any pointer fields? I think it is a bad idea, but one of my colleagues does not...
9
by: CptDondo | last post by:
I am working on an embedded platform which has a block of battery-backed RAM. I need to store various types of data in this block of memory - for example, bitmapped data for control registers,...
7
by: Aaron | last post by:
Complete code follows. I am new to .NET programming (and programming in general) and I am having a difficult time understanding how to fill a variable in one sub, and then access it from...
5
by: Robert Fitzpatrick | last post by:
Can someone point me to some more information or perhaps show an example of returning a recordset from a plpgsql function. I'd like to send an argument or arguments to the function, do some queries...
13
by: Logan Lee | last post by:
Hi. I've written a small program to learn to write in C. But unfortunately the output is all jumbled up and not nice. /* read_file.c The whole point of this code is to read the entire content...
23
by: pauldepstein | last post by:
Below is posted from a link for Stanford students in computer science. QUOTE BEGINS HERE Because of the risk of misuse, some experts recommend never returning a reference from a function or...
5
by: Cromulent | last post by:
Okay I'm having a few issues with this and I can't seem to get it sorted out (most likely due to my inexperience with Python). Here is my Python code: def fileInput(): data = s =...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.