Jump to content

[SOLVED] Calling functions


ninedoors

Recommended Posts

I have been working with php for about a year now and have recently started working with functions but I am having a little bit of trouble.  I have created my function lets say it is:

<?php

function fcsteaches()	{

include 'config.php';

//Connect to apache server(MySQL) and select database.
mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
mysql_select_db("$db_name")or die("cannot select DB");

//get the avg eaches per bay
$query = "SELECT quantity FROM frcstqunt";
$result = mysql_query($query);
while ($row = mysql_fetch_assoc($result))
{
$arrayqunt[] = $row['quantity']; 
}

//sort the array values
rsort($arrayqunt);

//get number of locations from currentlocations
$query = "SELECT * FROM currentlocations";
$result = mysql_query($query);
$numrows = mysql_num_rows($result);

//slice array so only top movers are left
$arrayslice = array_slice($arrayqunt, 0, $numrows);

//sum array
$arrayfinal = array_sum($arrayslice);

//get number of bays from locsperbay
$query = "SELECT * FROM locsperbay";
$result = mysql_query($query);
$numrows = mysql_num_rows($result);

$avgfcsteaches = $arrayfinal/$numrows;

return $avgfcsteaches;
}//end function
?>

 

And I return the value I want to get out of the function.  But now when I call that function in a new page and try to echo $avgfcsteaches, I get nothing.  This is the call I make to the function:

 

<?php

include 'getlocs.php';
include 'readexcel.php';
include 'curreaches.php';
include 'fcsteaches.php';

//VARIBALES FROM FORM
//---------------------------------------------
//excel sheet used for forecasting data
$sheet ='Excel/MC_BRAND_FORECAST.xls'; 
//line to be balanced
$brand = 'M5';
//---------------------------------------------

//functions calls
//getlocs($brand);
//readexcel($brand, $sheet);
//curreaches();
fcsteaches();

echo $avgfcsteaches;
?>

 

I am just testing these functions that I have made but ran into this snag.  Do I have something wrong with the syntax in my code or is there an easier way to do what I am doing.  Thanks

Link to comment
https://forums.phpfreaks.com/topic/100299-solved-calling-functions/
Share on other sites

Hey Paul,

 

I tried that and it still is giving me nothing.

 

<?php

include 'getlocs.php';
include 'readexcel.php';
include 'curreaches.php';
include 'fcsteaches.php';

//VARIBALES FROM FORM
//---------------------------------------------
//excel sheet used for forecasting data
$sheet ='Excel/MC_BRAND_FORECAST.xls'; 
//line to be balanced
$brand = 'M5';
//---------------------------------------------

//functions calls
//getlocs($brand);
//readexcel($brand, $sheet);
//curreaches();
$answer = fcsteaches();

echo $answer;
?>

try this then, you will notice I have added an "or die" statement to the queries to make sure they are doing what they should and are not failing inside the function and also changed the name of each query and corrsponding calls to that query to save any possible mix up of data

function fcsteaches()	{

include 'config.php';

//Connect to apache server(MySQL) and select database.
mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
mysql_select_db("$db_name")or die("cannot select DB");

//get the avg eaches per bay
$query1 = "SELECT quantity FROM frcstqunt";
$result1 = mysql_query($query1) or die ("Error in query 1" . mysql_error());
while ($row1 = mysql_fetch_assoc($result1))
{
$arrayqunt[] = $row1['quantity']; 
}

//sort the array values
rsort($arrayqunt);

//get number of locations from currentlocations
$query2 = "SELECT * FROM currentlocations";
$result2 = mysql_query($query2) or die ("Error in query 2 " . mysql_error());
$numrows2 = mysql_num_rows($result2);

//slice array so only top movers are left
$arrayslice = array_slice($arrayqunt, 0, $numrows2);

//sum array
$arrayfinal = array_sum($arrayslice);

//get number of bays from locsperbay
$query3 = "SELECT * FROM locsperbay";
$result3 = mysql_query($query3) or die ("error in query 3 . mysql_error());
$numrows3 = mysql_num_rows($result3);

$avgfcsteaches = $arrayfinal/$numrows3;

return $avgfcsteaches;
}//end function
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.