Returning info from a function

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
therat
Forum Commoner
Posts: 62
Joined: Wed Oct 01, 2003 2:44 pm
Location: London

Returning info from a function

Post by therat »

I have written the following function to retrieve info from a database and now need to display it. Until now I have used echo within the function but am now trying to remove that from it.
Firstly is this the correct way of doing this and secondly how do I call the function on another page and display the results.

Code: Select all

function recentsubs() {
        global $database;
		$sql = "SELECT 
                         skinz_cats.cat_id , skinz_cats.cat_name,
                         skinz_images.subs_name, skinz_images.subs_id, skinz_images.subs_downloads
                  FROM 
                          skinz_cats
                  LEFT JOIN
                          skinz_images
                  ON 
                        skinz_images.cat_id = skinz_cats.cat_id
                  WHERE   
                        skinz_images.status = '1'
                  ORDER BY 
                        subs_date
                  DESC LIMIT 10";
		$result = $database->sql_query($sql);
        while($row = mysql_fetch_assoc($result)) {
            $recsub[] = $row;
        }
        return $recsub;
	}
User avatar
shiznatix
DevNet Master
Posts: 2745
Joined: Tue Dec 28, 2004 5:57 pm
Location: Tallinn, Estonia
Contact:

Post by shiznatix »

Code: Select all

$result = recentsubs();
print_r($result);
you assign a variable to when you call the function and that variable will be what the return value is.

yes that way works for a function. after you use them more you will find more uses for em.

if the function is in a file all by itself you include() it and then you can call it no problems.
therat
Forum Commoner
Posts: 62
Joined: Wed Oct 01, 2003 2:44 pm
Location: London

Post by therat »

Done that and I can see the contents of the array using your code. How do I get it to display all the cat_name parts of the array for instance. Would it be something like

Code: Select all

echo $result['cat_name']
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

why don't ya try :?
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post by Jenk »

In your particular example, the array is multidimensional.

So the structure of the array $result is $result[<rownumber>][<columnheading>]

So for the value of column cat_name from say the 5th row, you would need to call $result[5]['cat_name']

HTH:)
Post Reply