除非我刷新页面,为什么我看不到数据库更新?

时间:2011-01-10 14:39:24

标签: php xml-rpc

嗯,我研究了一些从我的供应商那里提取库存水平的PHP代码,并根据产品的SKU将库存水平插入到数据库中。我已将其插入到class.product.php文件中,该文件包含用于单个产品页面的所有代码。我遇到的问题是,当产品页面加载时,除非您点击刷新,否则它不显示更新的库存水平。我已经将代码移动到整个地方,无法更新数据库并在显示页面之前加载更新的数字。

即使放在所有其他代码之前,我仍然需要刷新页面才能看到更新。我不知道该怎么办。我觉得也许,我并不真正理解PHP如何加载代码。几周以来,我每天都在努力。我尝试将其作为包含文件运行,在单独的页面上,顶部,中间,整个地方。

在类文件中,看起来我在调用代码之前有代码来显示库存水平,这就是为什么我对它为什么不加载更新感到困惑的原因。

除非我刷新页面,否则有什么想法我无法看到更改?

谢谢!

2 个答案:

答案 0 :(得分:1)

PHP在您请求时加载内容, 所以打开一个页面会得到内容ONCE,

要更新数据要做的事情是对php函数进行AJAX调用,以返回JSON或XML格式的数据 在这里你可以看到一些examples但是可以在谷歌上搜索更详细的例子。

答案 1 :(得分:0)

问题是我的代码直到获取并显示产品数据的代码之后才运行,因为我使用的信息来自仅被调用一次的产品数据。因此,首先调用产品数据以便我的代码运行。所以为了解决这个问题,我必须创建一个新函数来获取sku并将其传递给我的代码,然后调用要在页面上显示的产品数据的代码。我复制了现有函数来获取产品数据,将其重命名为GetRealTimeStockLevels并将我的代码添加到其底部。我将调用函数放在产品数据调用之上,它就像我想要的那样工作。我很高兴我得到了这个,现在我可以在结帐页面添加相同的功能。

下面是页面开头的函数调用,然后是我创建的用于运行更新代码的函数。

    public function __construct($productid=0)
    {
        // Get the stock level from supplier and update the database
        $this->_GetRealtimeStockLevels($productid);

        // Load the data for this product
        $this->_SetProductData($productid);

    public function _GetRealtimeStockLevels($productid=0)
    {

        if ($productid == 0) {
            // Retrieve the query string variables. Can't use the $_GET array
            // because of SEO friendly links in the URL
            SetPGQVariablesManually();
            if (isset($_REQUEST['product'])) {
                $product = $_REQUEST['product'];
            }
            else if(isset($GLOBALS['PathInfo'][1])) {
                $product = preg_replace('#\.html$#i', '', $GLOBALS['PathInfo'][1]);
            }
            else {
                $product = '';
            }


            $product = $GLOBALS['ISC_CLASS_DB']->Quote(MakeURLNormal($product));
            $productSQL = sprintf("p.prodname='%s'", $product);
        }
        else {
            $productSQL = sprintf("p.productid='%s'", (int)$productid);

        }

        $query = "
            SELECT p.*, FLOOR(prodratingtotal/prodnumratings) AS prodavgrating, pi.*, ".GetProdCustomerGroupPriceSQL().",
            (SELECT COUNT(fieldid) FROM [|PREFIX|]product_customfields WHERE fieldprodid=p.productid) AS numcustomfields,
            (SELECT COUNT(reviewid) FROM [|PREFIX|]reviews WHERE revstatus='1' AND revproductid=p.productid AND revstatus='1') AS numreviews,
            (SELECT brandname FROM [|PREFIX|]brands WHERE brandid=p.prodbrandid) AS prodbrandname,
            (SELECT COUNT(imageid) FROM [|PREFIX|]product_images WHERE imageprodid=p.productid) AS numimages,
            (SELECT COUNT(discountid) FROM [|PREFIX|]product_discounts WHERE discountprodid=p.productid) AS numbulkdiscounts
            FROM [|PREFIX|]products p
            LEFT JOIN [|PREFIX|]product_images pi ON (pi.imageisthumb=1 AND p.productid=pi.imageprodid)
            WHERE ".$productSQL;

        if(!isset($_COOKIE['STORESUITE_CP_TOKEN'])) {
            // ISC-1073: don't check visibility if we are on control panel
            $query .= " AND p.prodvisible='1'";
        }

        $result = $GLOBALS['ISC_CLASS_DB']->Query($query);
        $row = $GLOBALS['ISC_CLASS_DB']->Fetch($result);

        if (!$row) {
            return;
        }

        $this->_product = $row;
        $this->_prodid = $row['productid'];
        $this->_prodname = $row['prodname'];
        $this->_prodsku = $row['prodcode'];


        $GLOBALS['CurrentProductLink'] = ProdLink($this->_prodname);


                   $server_url = "http://ms.com/fgy/webservices/index.php";

                   $request = xmlrpc_encode_request("catalog.getStockQuantity", array($this->_prodsku));
                   $context = stream_context_create(array('http' => array(
                   'method' => "POST",
                   'header' => "Content-Type: text/xml",
                   'content' => $request
                    )));
                   $file = file_get_contents($server_url, false, $context);
                   $response = xmlrpc_decode($file);

                   $query = sprintf("UPDATE [|PREFIX|]products SET prodcurrentinv='$response' where prodcode='%s'",    $GLOBALS['ISC_CLASS_DB']->Quote($this->_prodsku)); 
                   $result = $GLOBALS['ISC_CLASS_DB']->Query($query);


    }
相关问题