passing variable to function
hi,
Been struggling with a problem for hours..... dunno what to try anymore!
The problem is: How do I pass a variable into a function which uses the variable to process a MySQL query??
I need to setup a function that I can call up (from about 200 different locations on the page - so it is essential I get it working ;)
I call up the function like this: <?php MakeAverage($ri1); ?>
where ri1 is a column on my MySQL database.
This is the function:
function MakeAverage($warble)
{
$connect = mysql_connect("localhost", "username", "password") or die ("screeewup");
mysql_select_db("dbase");
$query= "SELECT $warble FROM table1";
$results = mysql_query($query) or die (mysql_error());
$counter = 0;
while($rows = mysql_fetch_array($results))
{
extract($rows);
if ($warble < 1)
{
$warble = 1;
$t_ri1 = $t_ri1 + $warble;
$counter+=1;
}
else
{
if (($warble > 5)&&($warble != 9))
{
$warble = 5;
$t_ri1 = $t_ri1 + $warble;
$counter+=1;
}
else
{
$t_ri1 = $t_ri1 + $warble;
$counter+=1;
}
}
}
$raverage1 = round(($t_ri1 / $counter),2);
echo $raverage1."<br>";
}
?>
I started of with no arguments (i.e. $ri1 instead of $warble) and it worked fine, but as soon as I tried feeding ri1 into the function as an argument it stopped working. I.ve tried using &$warble in the function, declaring $warble inside and outside the function, quotemarks in every possible place and way, and loads of other things.... BUT IT WONT WORK!
Help?
Thanks!
|