2017-08-10 20 views
0

我一直在试图建立一个松弛的应用程序这需要的产品,并出现在农闲一个选择框之内的地方它的名称和SKU的基本信息。不幸的是我的代码来填充有效的JSON是走错了地方,当我尝试使用循环itterate。松弛,PHP和JSON - 有效的标记为迭代应用选择

有效的JSON是在这里:

{ 
    "text": "Great! You want to find something!", 
    "attachments": [ 
     { 
      "text": "Please type what you want to find", 
      "fallback": "Sorry! Cant do that at the moment!", 
      "callback_id": "cg_selectproduct", 
      "color": "#3AA3E3", 
      "attachment_type": "default", 

      "actions": [ 

       { 
        "name": "cg_choice", 
        "text": "Find Product", 
        "type": "select", 
        "options": [ 
         { 
           "text": "option1", 
           "value": "option1" 
         }, 

         { 
           "text": "option2", 
           "value": "option2" 
         }, 
         { 
           "text": "option3", 
           "value": "option3" 
         }] 
       } 
      ] 
     } 
    ] 
} 

这工作完全正常不迭代。如果我告诉应用程序去这里,我没有任何问题。它正确显示所有选项。

无效PHP

$check = ($dbh->prepare("SELECT * FROM product_list WHERE FAMILY='PARENT'")); 
$check->execute(); 
$row = $check->fetchAll(); 
// execute a pdo search to find all product parents 

$jsonInput = ""; 
foreach($row as $rows){ 
$jsonInput .= '"text"=>"' . $rows['PRODUCT_NAME'] . '", "value" => "' . $rows['SKU'] . '",'; 
} 
$jsonInput = rtrim($jsonInput, ','); 
//Create an iterative string which will contain the product names and skus, removing the comma at the end. 

header('Content-Type: application/json'); 
//Set the content type to json 


$optionSelect = array(
    "text" => "Great! You want to find something!", 
    "attachments" =>array(
      "text" => "Please type what you want to find", 
      "fallback" => "Sorry! Cant do that at the moment!", 
      "callback_id" => "cg_selectproduct", 
      "color"=> "#3AA3E3", 
      "attachment_type" => "default", 

      "actions" => array(
        "name" => "cg_choice", 
        "text" => "Find Product", 
        "type" => "select", 
        "options" => array($jsonInput) 
         ) 

      ) 

    ); 
//Create and itterate the options for the selection so it's populated 

print_r(json_encode($optionSelect)); 
//print to show json 

我不是100%肯定在那里我有这个打算是错误的。也许我想太多的小部分。任何人都可以帮助我,我哪里出错了?

回答

0
$jsonInput = []; 
foreach($row as $rows) { 
    $jsonInput[] = array(
     'text' => $rows['PRODUCT_NAME'], 
     'value' => $rows['SKU'] 
    ); 
} 

// ........... 

"options" => $jsonInput 
+0

你的先生,是一个英雄。 –

+0

虽然这工作,因为我正在寻找的,我还是打了一下碰钉子的。看起来生成的输出不匹配json输出。在这里看到:[链接](http://puu.sh/x6x9v.png)左边的一个是生成一个和一个在右边是有效的JSON标记。 –

+0

然后在需要的地方添加从零开始的索引。例如:“附件” =>阵列(阵列(... - 双人间数组()键 – Sergej