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
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;
?>

Link to comment
Share on other sites

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
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.