Всем привет! Помогите пожалуйста передать переменную из одной функции в другую, уже целую неделю бьюсь и никак... Надо передать переменную Код (Text): $pod_zakaz из ф. Код (Text): cartGetCartContent() в ф. Код (Text): InsertItemIntoCart($itemID) вот коды функций: Код (Text): function cartGetCartContent() { $cart_content = array(); $stock_help = 0; $total_price = 0; $freight_cost = 0; $variants = ''; if (isset($_SESSION["log"])) { $q = db_query("SELECT itemID, Quantity FROM ". SHOPPING_CARTS_TABLE. " WHERE customerID='".regGetIdByLogin($_SESSION["log"])."'" ); while ($cart_item = db_fetch_row($q)) { $variants=GetConfigurationByItemId( $cart_item["itemID"] ); $q_shopping_cart_item = db_query("select productID from ". SHOPPING_CART_ITEMS_TABLE." where ". " itemID='".$cart_item["itemID"]."'" ); $shopping_cart_item = db_fetch_row( $q_shopping_cart_item ); $q_products = db_query("SELECT name, Price, productID, in_stock, min_order_amount, shipping_freight, free_shipping FROM ". PRODUCTS_TABLE. " WHERE productID='".$shopping_cart_item["productID"]."'"); if ( $product = db_fetch_row($q_products) ) { $costUC = GetPriceProductWithOption( $variants, $shopping_cart_item["productID"] ); $tmp = array( "productID" => $product["productID"], "id" => $cart_item["itemID"], "name" => $product["name"], "in_stock" => $product["in_stock"], "quantity" => $cart_item["Quantity"], "free_shipping" => $product["free_shipping"], "costUC" => $costUC, "cost" => show_price($cart_item["Quantity"]* GetPriceProductWithOption($variants, $shopping_cart_item["productID"])) ); $freight_cost += $cart_item["Quantity"]*$product["shipping_freight"]; $strOptions=GetStrOptions( GetConfigurationByItemId( $tmp["id"] )); if ( trim($strOptions) != "" ) $tmp["name"].=" (".$strOptions.")"; if ( $product["min_order_amount"] > $cart_item["Quantity"] ) $tmp["min_order_amount"] = $product["min_order_amount"]; if ( $product["in_stock"] <= 0 ) { $stock_help = "1"; } if ( $cart_item["Quantity"] > $product["in_stock"] && $product["in_stock"] <= 0 ) { $product["in_stock"] = 0; $tmp["stock_raznica"] = $cart_item["Quantity"] - $product["in_stock"]; } else if ( $cart_item["Quantity"] > $product["in_stock"] && $product["in_stock"] > 0 ) { $tmp["stock_raznica"] = $cart_item["Quantity"] - $product["in_stock"]; } else { $tmp["stock_raznica"] = 0; } $pod_zakaz = $tmp["stock_raznica"]; $cart_content[] = $tmp; $total_price += $cart_item["Quantity"]* GetPriceProductWithOption($variants, $shopping_cart_item["productID"]); } } } else { $total_price = 0; $stock_help = 0; $cart_content = array(); if ( isset($_SESSION["gids"]) ) for ($j=0; $j<count($_SESSION["gids"]); $j++) { if ($_SESSION["gids"][$j]) { $session_items[]= CodeItemInClient($_SESSION["configurations"][$j], $_SESSION["gids"][$j]); $q = db_query("SELECT name, Price, in_stock, shipping_freight, free_shipping FROM ". PRODUCTS_TABLE. " WHERE productID='".$_SESSION["gids"][$j]."'"); if ($r = db_fetch_row($q)) { $costUC = GetPriceProductWithOption( $_SESSION["configurations"][$j], $_SESSION["gids"][$j]) $id = $_SESSION["gids"][$j]; if (count($_SESSION["configurations"][$j]) > 0) { for ($tmp1=0;$tmp1<count($_SESSION["configurations"][$j]);$tmp1++) $id .= "_".$_SESSION["configurations"][$j][$tmp1]; } $tmp = array( "productID" => $_SESSION["gids"][$j], "id" => $id, "name" => $r[0], "quantity" => $_SESSION["counts"][$j], "in_stock" => $r["in_stock"], "free_shipping" => $r["free_shipping"], "costUC" => $costUC, "cost" => show_price($costUC * $_SESSION["counts"][$j]) ); $strOptions=GetStrOptions( $_SESSION["configurations"][$j] ); if ( trim($strOptions) != "" ) $tmp["name"].=" (".$strOptions.")"; if ( $_SESSION["counts"][$j] > $r["in_stock"] ) { $stock_help = "1"; } if ( $_SESSION["counts"][$j] > $r["in_stock"] && $r["in_stock"] <= 0 ) { $r["in_stock"] = 0; $tmp["stock_raznica"] = $_SESSION["counts"][$j] - $r["in_stock"]; } else if ( $_SESSION["counts"][$j] > $r["in_stock"] && $r["in_stock"] > 0 ) { $tmp["stock_raznica"] = $_SESSION["counts"][$j] - $r["in_stock"]; } else { $tmp["stock_raznica"] = 0; } $pod_zakaz = $tmp["stock_raznica"]; $q_product = db_query( "select min_order_amount, shipping_freight from ".PRODUCTS_TABLE. " where productID=". $_SESSION["gids"][$j] ); $product = db_fetch_row( $q_product ); if ( $product["min_order_amount"] > $_SESSION["counts"][$j] ) $tmp["min_order_amount"] = $product["min_order_amount"]; $freight_cost += $_SESSION["counts"][$j]*$product["shipping_freight"]; $cart_content[] = $tmp; $total_price += GetPriceProductWithOption( $_SESSION["configurations"][$j], $_SESSION["gids"][$j] )*$_SESSION["counts"][$j]; } } } } return array( "cart_content" => $cart_content, "total_price" => $total_price, "stock_help" => $stock_help, "freight_cost" => $freight_cost ); } код ф. InsertItemIntoCart($itemID): Код (Text): db_query("insert ".SHOPPING_CARTS_TABLE. "(customerID, itemID, pod_zakaz)". "values( '".regGetIdByLogin($_SESSION["log"])."', '".$itemID."', '".$pod_zakaz."' )" ); } ОЧЕНЬ НАДЕЮСЬ НА ВАШУ ПОМОЩЬ!
сходу, не смотря код, есть способ - можешь объявить переменную в глобальной области видимости. почитай про global $var или суперглобальный массив $GLOBALS['var']
Gromo спасибо за информацию для изучения. На простеньком коде у меня все получается с global, но у меня не получается это применить в коде выше. Я заметил такую вещь, что если я объявлю переменную глобальной в коде выше после Код (Text): return array( "cart_content" => $cart_content, "total_price" => $total_price, "stock_help" => $stock_help, "freight_cost" => $freight_cost ); , то эта переменная не передается, а если перед , то все гуд. Что это значит?
ну почитай тогда ещё про return псы: А вобще что тут передовать? Зделай одну глобальную переменную и работай с ней хоть во всех функциях., типа: Код (Text): $test=0; function foo1() { global $test; return $test++; } function foo2() { global $test; return $test++; } foo1(); foo2(); echo $test;
Т.е., если я вызываю return, то функция прерывается и все что находится ниже return в функции просто бессмысленно. Правильно я понимаю? А переменную я могу объявлять в любом месте функции или же нет? Может я что-то не так делаю... Вот как я передаю переменную из функции в функцию, но это не работает: Код (Text): function cartGetCartContent() { $cart_content = array(); $stock_help = 0; $total_price = 0; $freight_cost = 0; $variants = ''; if (isset($_SESSION["log"])) { $q = db_query("SELECT itemID, Quantity FROM ". SHOPPING_CARTS_TABLE. " WHERE customerID='".regGetIdByLogin($_SESSION["log"])."'" ); while ($cart_item = db_fetch_row($q)) { $variants=GetConfigurationByItemId( $cart_item["itemID"] ); $q_shopping_cart_item = db_query("select productID from ". SHOPPING_CART_ITEMS_TABLE." where ". " itemID='".$cart_item["itemID"]."'" ); $shopping_cart_item = db_fetch_row( $q_shopping_cart_item ); $q_products = db_query("SELECT name, Price, productID, in_stock, min_order_amount, shipping_freight, free_shipping FROM ". PRODUCTS_TABLE. " WHERE productID='".$shopping_cart_item["productID"]."'"); if ( $product = db_fetch_row($q_products) ) { $costUC = GetPriceProductWithOption( $variants, $shopping_cart_item["productID"] ); $tmp = array( "productID" => $product["productID"], "id" => $cart_item["itemID"], "name" => $product["name"], "in_stock" => $product["in_stock"], "quantity" => $cart_item["Quantity"], "free_shipping" => $product["free_shipping"], "costUC" => $costUC, "cost" => show_price($cart_item["Quantity"]* GetPriceProductWithOption($variants, $shopping_cart_item["productID"])) ); $freight_cost += $cart_item["Quantity"]*$product["shipping_freight"]; $strOptions=GetStrOptions( GetConfigurationByItemId( $tmp["id"] )); if ( trim($strOptions) != "" ) $tmp["name"].=" (".$strOptions.")"; if ( $product["min_order_amount"] > $cart_item["Quantity"] ) $tmp["min_order_amount"] = $product["min_order_amount"]; if ( $product["in_stock"] <= 0 ) { $stock_help = "1"; } if ( $cart_item["Quantity"] > $product["in_stock"] && $product["in_stock"] <= 0 ) { $product["in_stock"] = 0; $tmp["stock_raznica"] = $cart_item["Quantity"] - $product["in_stock"]; } else if ( $cart_item["Quantity"] > $product["in_stock"] && $product["in_stock"] > 0 ) { $tmp["stock_raznica"] = $cart_item["Quantity"] - $product["in_stock"]; } else { $tmp["stock_raznica"] = 0; } global $x1; $x1 = $tmp["stock_raznica"]; ... Код (Text): function InsertItemIntoCart($itemID) { [b]cartGetCartContent();[/b] db_query("insert ".SHOPPING_CARTS_TABLE. "(customerID, itemID, pod_zakaz)". "values( '".regGetIdByLogin($_SESSION["log"])."', '".$itemID."', '".$x1."' )" ); } Может я что-то не так делаю? Помогите плиз...
Конечно не работает - ты вобще смотришь что тебе пишут? Во-первых объяви переменную x1 как глобальную. Во-вторых я не вижу что ты инициализировал её во второй функции: Код (Text): function InsertItemIntoCart($itemID) { global x1; }
сори, не заметил... Вроде все сделал как надо, но не работает... вот код: Код (Text): <?php function InsertItemIntoCart($itemID) { global $x1; cartGetCartContent(); db_query("insert ".SHOPPING_CARTS_TABLE. "(customerID, itemID, pod_zakaz)". "values( '".regGetIdByLogin($_SESSION["log"])."', '".$itemID."', '".$x1."' )" ); } $x1 = 0; function cartGetCartContent() { $cart_content = array(); $stock_help = 0; $total_price = 0; $freight_cost = 0; $variants = ''; ... ... global $x1; $x1 = 11; ... ... } ?> Показывает 0
PHP: <?php function InsertItemIntoCart($itemID) { $x1=100; echo $x1; $x1 = cartGetCartContent($x1); echo $x1; db_query("insert ".SHOPPING_CARTS_TABLE. "(customerID, itemID, pod_zakaz)". "values( '".regGetIdByLogin($_SESSION["log"])."', '".$itemID."', '".$x1."' )" ); } $x1 = 0; function cartGetCartContent($x1) { $cart_content = array(); $stock_help = 0; $total_price = 0; $freight_cost = 0; $variants = ''; ... ... echo $x1; $x1 = 11; ... ... return $x1; } ?>