php-general Digest 14 Jun 2012 11:55:30 -0000 Issue 7853

Topics (messages 318231 through 318231):

global array
        318231 by: Jeff Burcher

Administrivia:

To subscribe to the digest, e-mail:
        php-general-digest-subscr...@lists.php.net

To unsubscribe from the digest, e-mail:
        php-general-digest-unsubscr...@lists.php.net

To post to the list, e-mail:
        php-gene...@lists.php.net


----------------------------------------------------------------------
--- Begin Message ---
Hi,

 

I am running PHP 5.4 on IIs 6 on a Windows SBS 2003 server. Here is a
streamlined version of the code I am dealing with. I tried to trim as much
as possible to only show code that deals with my issue. The main issue I
think I am having is the global array statement within the function is not
working. From all of the articles I have read, I seem to be doing it
correctly, yet it does not recognize $InvReq as an array within the
function. The array works just fine outside of the function, though. Here
are the error messages:

 

Notice: Undefined variable: InvReq (line numbers point to inside the
function)

Warning: array_key_exists() expects parameter 2 to be array, null given
(line numbers point to inside the function)

 

If anyone could point out where I have incorrect syntax or something else
that would interfere with the global reference for the array within the
function, or tell me I can't do what I am doing using arrays, but assume I
don't have the ability to write to an external work table, which is why I am
trying to use an array in the first place. Thanks.

----------------------------------------------------------------------------
-----------------------------

<?php

$InvReq = array();

 

// there is some database action here and other programming, but the core
issue is writing to the array, so assume I have all of the necessary data

// data loop to gather inventory requests ---

                $linetotal = ???;

                $hldpartID = ?????;         //these variables are provided
by looping, so this check/write/update code snippet happens many times to
update/write to the array

 

                if (array_key_exists($hldpartID, $InvReq)) {

                                $InvReq[$hldpartID] += $linetotal;
//if this line will create the array entry if it does not exist, then I
don't need the key_exists check, anyone??

                } else {

                                $InvReq[$hldpartID] = $linetotal;

                }

 

                Part_BOM($hldpartID, $linetotal, 1);       //function is
called the first time to set up the BOM

// end loop ---

 

// after gathering all of the inventory requests, check for inventory on
hand

reset ($InvReq);

while (list($PartID,) = each($InvReq)) {

                // data loop to get inventory on hand ---

                                $OnHandQty = ???;         //these variables
are provided by looping, so this check/write/update code snippet happens
many times to update/add to the array

 

                                if (array_key_exists($PartID, $InvReq)) {

                                                $InvReq[$PartID] -=
$OnHandQty;

                                } else {

                                                $InvReq[$PartID] =
$OnHandQty;

                                }

                                Part_BOM($PartID, $OnHandQty, 2);
//function is called the second time to trim up BOM

                // end loop ---

}

// print list

$display_block = "<h2>Part Forecast</h2><table><tr><th>Part
Number</th><th>Amount Needed</th></tr>";

reset ($InvReq);

while (list($PartID,$need) = each($InvReq)) {

                $needout = number_format($need);

                $display_block .=
"<tr><td>$PartID</td><td>$needout</td></tr>";

}

$display_block .= "</table>";

 

//---this function adds or subtracts inventory to/from the BOM materials for
the part passed.

//---it also can add parts that make parts with their inventory total for a
multiple level BOM

 

function Part_BOM($PartID, $need, $phase) {

 

                global $Invreq;

 

                $BOMreq = $need * $BOMQty;                 //$BOMQty &
$BOMPartID are pulled from a database keyed by $PartID

 

                if ($phase == 1) {

                                if (array_key_exists($BOMPartID, $InvReq)) {

                                                $InvReq[$BOMPartID] +=
$BOMreq;      //first time through adds to array item totals

                                } else {

                                                $InvReq[$BOMPartID] =
$BOMreq;

                                }

                } else {

                                if (array_key_exists($BOMPartID, $InvReq)) {

                                                $InvReq[$BOMPartID] -=
$BOMreq;       //second time through subtracts from array item totals

                                }

                }

 

                if (--check for parts within other parts for multiple level
BOM--) {

                                Part_BOM($BOMPartID, $BOMreq, $phase);
//this calls itself and can refer/loop back to itself several times

                }

}

?>

<html>

<head>

</head>

<body>

<center>

                <?php echo $display_block ?>

</center>

</body>

</html>

----------------------------------------------------------------------------
--------------------------

Thanks for your input,

 

Jeff Burcher - IT Dept

Allred Metal Stamping

PO Box 2566

High Point, NC 27261

(336)886-5221 x229

j...@allredmetal.com

 

 


--- End Message ---

Reply via email to