Page 1 of 1

Returning info from a function

Posted: Fri Dec 02, 2005 12:49 pm
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;
	}

Posted: Fri Dec 02, 2005 1:08 pm
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.

Posted: Fri Dec 02, 2005 3:14 pm
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']

Posted: Fri Dec 02, 2005 4:08 pm
by John Cartwright
why don't ya try :?

Posted: Fri Dec 02, 2005 5:51 pm
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:)