Share Prices - Property for sale in Spain - Advertising - Loans - Loans

PDA

View Full Version : Can someone explain why this function isn't working?


Greenmethod
Feb 7th 2008, 7:19 am
I have a function that is inside of another function... At first I thought that was why it wasn't working, but there is another function inside of it that works fine. Anyway here it is...

Sub Function:

<!-- Function to check how many are in stock-->
<?php
function in_stock($cat_num){
global $num_in_stock;
$in_stock_sql = mysql_query("SELECT `cat_no` FROM `inventory` WHERE `cat_no` = '$cat_num' AND `cust_out`=' '");
$num_in_stock = mysql_num_rows($in_stock_sql);
};?>

Main Function:

foreach($cat_nums as $cat_num){
in_stock($cat_num);
echo $num_in_stock;
};

Greenmethod
Feb 7th 2008, 7:20 am
I know that the cat_nums array is there because I put the sub function inside (without the function) and it works fine.

Brewster
Feb 7th 2008, 8:20 am
The first thing that I would check is if the mysql_query function is returning an error.

Brew

Greenmethod
Feb 7th 2008, 9:17 am
This might be a better example... It doesn't carry the global array to the function that calls the other function if that makes any sense...

<?php
function setup_invoice($cust_no,$quote_num,$quant){
$cat_nums_sql = mysql_query("SELECT DISTINCT `cat_num` FROM `quotes` WHERE `cust_no`='$cust_no' AND `quote_num`='$quote_num'");
while($cat_num = mysql_fetch_assoc($cat_nums_sql)){
$cat_nums[] = $cat_num['cat_num'];
};
chk_num_ordered($cat_nums,$quantity,$quote_num);
echo 'quant in inv: ';
print_r($quant); //DOES NOT DISPLAY
echo '<br>';
};?>

<!-- Function to check the number ordered against being anything but a number. -->
<?php
function chk_num_ordered($cat_nums,$quantity,$quote_num){
global $quant;
echo 'cat_nums: ';
print_r($cat_nums);
echo '<br>';
foreach($cat_nums as $cat_num){
$quantity = mysql_query("SELECT `cat_num` FROM `quotes` WHERE `quote_num`='$quote_num' AND `cat_num`=$cat_num");
if ($_POST['ord' . $cat_num] && preg_match("/^[0-9]*$/",$_POST['ord' . $cat_num])){
$quant[] = $_POST['ord' . $cat_num];
}else{
$quant[] = mysql_num_rows($quantity);
};
};
echo 'quant inside check: ';
print_r($quant); //DISPLAYS
echo '<br>';
};




include_once('db.php');
$cust_no = 3862;
$quote_num = 6129;
$cat_nums[] = 1017;
$cat_nums[] = 1032;
$cat_nums[] = 1033;
$cat_nums[] = 1009;
$cat_nums[] = 1005;
setup_invoice($cust_no,$quote_num,$quant);
echo 'After setup: ';
print_r($quant); //DISPLAYS
?>

Brewster
Feb 7th 2008, 9:30 am
Greenmethod - can you answer my question? You are not checking the return value from mysql_query which may be causing the error.

Brew

Greenmethod
Feb 7th 2008, 9:37 am
It is not a problem with the query.. if it was, none of them would display. It's a problem with getting the $quant variable into the setup_invoice function. Either way, here's the new code...

<?php
function setup_invoice($cust_no,$quote_num,$quant){
$cat_nums_sql = mysql_query("SELECT DISTINCT `cat_num` FROM `quotes` WHERE `cust_no`='$cust_no' AND `quote_num`='$quote_num'") or die(mysql_error());
while($cat_num = mysql_fetch_assoc($cat_nums_sql)){
$cat_nums[] = $cat_num['cat_num'];
};
chk_num_ordered($cat_nums,$quantity,$quote_num);
echo 'quant in inv: ';
print_r($quant);//DOES NOT DISPLAY
echo '<br>';
};?>

<!-- Function to check the number ordered against being anything but a number. -->
<?php
function chk_num_ordered($cat_nums,$quantity,$quote_num){
global $quant;
echo 'cat_nums: ';
print_r($cat_nums);
echo '<br>';
foreach($cat_nums as $cat_num){
$quantity = mysql_query("SELECT `cat_num` FROM `quotes` WHERE `quote_num`='$quote_num' AND `cat_num`=$cat_num") or die(mysql_error());
if ($_POST['ord' . $cat_num] && preg_match("/^[0-9]*$/",$_POST['ord' . $cat_num])){
$quant[] = $_POST['ord' . $cat_num];
}else{
$quant[] = mysql_num_rows($quantity);
};
};
echo 'quant inside check: ';
print_r($quant); //DISPLAYS
echo '<br>';
};




include_once('knal.php');
$cust_no = 3862;
$quote_num = 6129;
$cat_nums[] = 1017;
$cat_nums[] = 1032;
$cat_nums[] = 1033;
$cat_nums[] = 1009;
$cat_nums[] = 1005;
setup_invoice($cust_no,$quote_num,$quant);
echo 'After setup: ';
print_r($quant); //DISPLAYS
?>

It runs the same.

mrmaypole
Feb 7th 2008, 9:29 pm
setup_invoice needs global $quant.

Revolution333
Feb 7th 2008, 10:34 pm
<!-- Function to check how many are in stock-->
<?php
function in_stock($cat_num){
$in_stock_sql = mysql_query("SELECT `cat_no` FROM `inventory` WHERE `cat_no` = '$cat_num' AND `cust_out`=' '");
$num_in_stock = mysql_num_rows($in_stock_sql);
return $num_in_stock;

};?>

Main Function:

foreach($cat_nums as $cat_num){
echo in_stock($cat_num);
};

Try that.

Also you wouldn't need the global $num_in_stock; Global is for when you have a different function somewhere else.