...

View Full Version : functions within functions



ClubCosmic
10-31-2006, 12:51 AM
Hi all,

i was wondering if this is a valid function to use in a class. I would like the result of the query to be a variable in another function.


function sql_all_items($all_items){
$sql = "SELECT * FROM items ORDER BY item_id DESC";
$all_items = mysql_query($sql);
return $all_items;
}


So somewhere i would like to just be able to do:


function display ($all_items){
while ($row = mysql_fetch_array($all_items)) {
echo "blah blah blah";
}

}


Hope i was clear with what i am trying to do. I seem to lose the value of $all_items whenever i leave the 1st funtion.

thanks for any suggestions.
c.c.

marek_mar
10-31-2006, 12:55 AM
You have a typo in the first function. You don't need to set an argument for the first function. The data will be overwritten anyway (if you remove the typo). You could make the argument into a reference in which case you won't need to return anything as the variable given as the argument will be changed.

ClubCosmic
10-31-2006, 01:20 AM
function sql_all_items(){
$sql = "SELECT * FROM items ORDER BY item_id DESC";
$all_items = mysql_query($sql);
return $this->$all_items;
}

Is that what i should be doing?

marek_mar
10-31-2006, 07:44 PM
Without the "$this->" part it will return a MySQL result resource or false (that's what you should be expecting).
The second function will work with the result of the first function unless the first function returns false. You should check that.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum