2011-01-10 83 views
0

嗯,我從事一些PHP代碼,從我的供應商處提取庫存水平,並根據產品的SKU將庫存水平插入數據庫。我已將它插入到class.product.php文件中,其中包含用於單個產品頁面的所有代碼。我遇到的問題是,當產品頁面加載時,它不會顯示更新的庫存水平,除非您點擊刷新。我已將代碼移到所有位置,無法更新數據庫並在顯示頁面之前加載更新後的數字。爲什麼我無法看到數據庫更新,除非我刷新頁面?

即使放置在所有其他代碼之前,我仍然需要刷新頁面才能看到更新。我不知道該怎麼辦。我也許感覺,我並沒有真正理解PHP如何加載代碼。我每天都在爲此工作數週。我嘗試將它作爲包含文件運行,在單獨的頁面上,頂部,中間和各處運行。

在類文件中,它看起來像我調用代碼顯示庫存水平之前的代碼,這就是爲什麼我很困惑,爲什麼它不會加載更新。

有關爲什麼我無法看到更改,除非我刷新頁面的任何想法?

謝謝!

+0

我唯一能想到的問題就是緩存。具體是IE。 – benhowdle89 2011-01-10 14:42:07

+1

我們需要看到一些代碼才能真正幫助。 – Rudu 2011-01-10 14:49:17

回答

1

PHP加載內容,當你提出要求, 所以打開頁面ONCE獲取內容,

你想要做的就是更新數據的一件事就是AJAX調用一個PHP函數,在JSON返回數據或XML格式 在這裏你可以看到一些examples,但考慮搜索更詳細的例子。

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); 


    } 
相關問題