2017-08-10 5 views
0

私は、製品の名前とスキューの基本情報を取得し、余裕に表示される選択ボックス内に配置するスラックアプリケーションを作成しようとしています。残念ながら、ループを使って繰り返ししようとすると、有効なjsonを読み込むコードが間違っています。slack、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出力と一致しないようです。参照:[link](http://puu.sh/x6x9v.png)左側のものは生成されたもので、右側のものは有効なjsonマークアップです。 –

+0

次に、必要に応じてゼロベースのインデックスを追加します。例: "attachments" => array(array(... - array()キーをdoble – Sergej

関連する問題