【发布时间】:2017-02-21 01:43:42
【问题描述】:
我目前正在使用 adwords SDK 进行开发。 我从数据库中查询产品是否有库存,并将广告设置为暂停或启用。
现在在我的 while 循环中,我有一个 IF 语句,由于某种原因,它当前总是触发 true。我想也许它只使用第一个值而不是循环通过 $stock,如果是这样的话,有什么想法吗?
我需要它来遍历每个库存状态。如果启用了“有货”帖子,则将其设置为暂停
这是代码
$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());
}
【问题讨论】:
-
你有 2 个 ifs :D 如果它是
if ($stock === "In Stock")则从中删除 1 ` = ` -
@KikiTheOne 哈哈 ;)
-
在您的内部函数中,您覆盖变量 $result - 也许这是一个问题。如果没有,则意味着您的所有记录都是“有货”:)
-
让我问 1 个问题。为什么你在 WhileLoop 中声明函数?而不是在循环之外,只需调用一次?
-
@nospor 我认为这将是错误,因为他在 $result fetch_array 时循环。因此,如果结果不再是 samy,则没有任何内容可以循环。要检查它是否在第一个循环后卡住,请在 while 循环中放置一个 echo $row['stock'] 。如果只有一个值,你就知道循环坏了。
标签: php arrays if-statement foreach while-loop