2016-10-12 10 views
1

Im現在adwords SDKで開発中です。 商品がデータベースから在庫されているかどうかを照会し、広告を一時停止または有効に設定します。PHP - IF文は常にTrue In Whileループ

私のwhileループでは、私は現在何らかの理由で常に真を発しているIF文を持っています。私はたぶんその最初の値を使用し、$株をループしていないと思うかもしれません、場合は、任意のアイデアthats?

各在庫状況を確認する必要があります。より一般的に、あなたがそれを宣言するだろう - ポスト「在庫」他に有効にした場合、私はそれがあったように関数がループ内で宣言されるだろう、なぜわからない

HERESにコード

$sql = "SELECT * FROM MYDB.MYTBL" ; 
$result = mysqli_query($conn, $sql); 

if ($result) { 

while($row = mysqli_fetch_array($result)) 
    { 
    $adGroup = $row['adgroup']; 
    $adGroupId = $row['adgroup_id']; 
    $product = $row['product']; 
    $stock = $row['stock']; 
    $client = $row['client']; 


    if ($stock === "In Stock") { 

     if(!function_exists('UpdateAdGroupExample')){ 

     function UpdateAdGroupExample(AdWordsUser $user, $adGroupId) { 
      // Get the service, which loads the required classes. 
      $adGroupService = $user->GetService('AdGroupService', ADWORDS_VERSION); 

      // Create ad group using an existing ID. 
      $adGroup = new AdGroup(); 
      $adGroup->id = $adGroupId; 

      // Update the status. 
      $adGroup->status = 'ENABLED'; 

      // Create operation. 
      $operation = new AdGroupOperation(); 
      $operation->operand = $adGroup; 
      $operation->operator = 'SET'; 

      $operations = array($operation); 

      // Make the mutate request. 
      $result = $adGroupService->mutate($operations); 

      // Display result. 
      $adGroup = $result->value[0]; 
      printf("Ad group with ID '%s' has updated default bid '$%s'.\n", $adGroup->id, 
       $adGroup->status); 

     } 

     try { 
      // Get AdWordsUser from credentials in "../auth.ini" 
      // relative to the AdWordsUser.php file's directory. 
      $user = new AdWordsUser(); 
      $user->SetClientCustomerId('XXXXXXXXX'); 
      // Log every SOAP XML request and response. 
      $user->LogAll(); 

      // Run the example. 
      UpdateAdGroupExample($user, $adGroupId); 
     } catch (Exception $e) { 
      printf("An error has occurred: %s\n", $e->getMessage()); 
     } 
     } 
     try { 
     // Get AdWordsUser from credentials in "../auth.ini" 
     // relative to the AdWordsUser.php file's directory. 
     $user = new AdWordsUser(); 
     $user->SetClientCustomerId('XXXXXXXXX'); 
     // Log every SOAP XML request and response. 
     $user->LogAll(); 

     // Run the example. 
     UpdateAdGroupExample($user, $adGroupId); 
     } catch (Exception $e) { 
     printf("An error has occurred: %s\n", $e->getMessage()); 
     } 
+0

uは2つのIFSを持っている:その 'IF($ストックは=== "在庫")' 1 'は='それ – KikiTheOne

+0

@KikiTheOne笑から削除する場合はD;) – nospor

+0

あなたの内の関数で変数を上書き$ result - 多分これは問題です。そうでなければ、あなたのすべてのレコードが「在庫あり」であることを意味します:) – nospor

答えて

0

を一時停止するように設定しそのようなループの前に。私はその点を見逃している可能性がありますが、私はあなたがこれらの行に沿って何かをすることができると思っていたでしょう。

$sql = "SELECT * FROM MYDB.MYTBL" ; 
$result = mysqli_query($conn, $sql); 

if($result) { 

    if(!function_exists('UpdateAdGroupExample')){ 

     function UpdateAdGroupExample(AdWordsUser $user, $adGroupId) { 
      // Get the service, which loads the required classes. 
      $adGroupService = $user->GetService('AdGroupService', ADWORDS_VERSION); 

      // Create ad group using an existing ID. 
      $adGroup = new AdGroup(); 
      $adGroup->id = $adGroupId; 

      // Update the status. 
      $adGroup->status = 'ENABLED'; 

      // Create operation. 
      $operation = new AdGroupOperation(); 
      $operation->operand = $adGroup; 
      $operation->operator = 'SET'; 

      $operations = array($operation); 

      // Make the mutate request. 
      $result = $adGroupService->mutate($operations); 

      // Display result. 
      $adGroup = $result->value[0]; 

      printf("Ad-Group with ID '%s' has updated default bid '$%s'.\n", $adGroup->id, $adGroup->status); 
     } 
    } 



    while($row = mysqli_fetch_array($result)) { 
     /* appear unused ~ is there further code in the loop not shown? */ 
     #$adGroup = $row['adgroup']; 
     #$product = $row['product']; 
     #$client = $row['client']; 

     $adGroupId = $row['adgroup_id']; 
     $stock = trim($row['stock']); 


     if ($stock == "In Stock") { 

      try { 
       $user = new AdWordsUser(); 
       if($user){ 

        $user->SetClientCustomerId('XXXXXXXXX'); 
        $user->LogAll(); 

        UpdateAdGroupExample($user, $adGroupId); 
       } else { 
        throw new Exception('User could not be created'); 
       } 
      } catch(Exception $e) { 
       printf("An exception has occurred: %s\n", $e->getMessage()); 
      } 
     } else { 
      /* debug what the value of $stock is */ 
      printf("stock: %s\n",$stock); 

     }/* end stock check */ 

    }/* end while */ 

}/* end if($result) */ 
+0

この行が$ result = $ adGroupService-> mutate($ operations);あなたが上書きしているので問題は起こりません...あなたは私より多くを知ってもらえますx: – Twinfriends

+0

@RamRaider - あなたの答えは間違いなく効くと思います。私はこの行を使うことができます$ adGroup-> status = 'ENABLED'; if($ stock == "在庫あり")ステートメントでは、 –

関連する問題