Can someone explain why this function isn't working?

Discussion in 'PHP' started by Greenmethod, Feb 7, 2008.

  1. #1
    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);
    };?>
    PHP:
    Main Function:

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

     
    Greenmethod, Feb 7, 2008 IP
  2. Greenmethod

    Greenmethod Peon

    Messages:
    112
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    I know that the cat_nums array is there because I put the sub function inside (without the function) and it works fine.
     
    Greenmethod, Feb 7, 2008 IP
  3. Brewster

    Brewster Active Member

    Messages:
    489
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    60
    #3
    The first thing that I would check is if the mysql_query function is returning an error.

    Brew
     
    Brewster, Feb 7, 2008 IP
  4. Greenmethod

    Greenmethod Peon

    Messages:
    112
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    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
    ?>
    PHP:
     
    Greenmethod, Feb 7, 2008 IP
  5. Brewster

    Brewster Active Member

    Messages:
    489
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    60
    #5
    Greenmethod - can you answer my question? You are not checking the return value from mysql_query which may be causing the error.

    Brew
     
    Brewster, Feb 7, 2008 IP
  6. Greenmethod

    Greenmethod Peon

    Messages:
    112
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    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
    ?>
    PHP:
    It runs the same.
     
    Greenmethod, Feb 7, 2008 IP
  7. mrmaypole

    mrmaypole Peon

    Messages:
    19
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    setup_invoice needs global $quant.
     
    mrmaypole, Feb 7, 2008 IP
  8. Revolution333

    Revolution333 Peon

    Messages:
    227
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #8
    <!-- 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;
    
    };?>
    PHP:
    Main Function:

    	foreach($cat_nums as $cat_num){
    		echo in_stock($cat_num);
    };
    PHP:
    Try that.

    Also you wouldn't need the global $num_in_stock; Global is for when you have a different function somewhere else.
     
    Revolution333, Feb 7, 2008 IP