2015-06-24 21 views
5

Field 'cats' è una selectbox dall'app Angular, contiene i dati di table cats, con id, name e module_id.Put Array json su mysql return Array

funzione sul quadro Slim per aggiungere elementi ..

function addItem($section) { 

    $request = \Slim\Slim::getInstance()->request(); 
    $item = json_decode($request->getBody()); 

    $sql = "INSERT INTO " .$section. " (title, subtitle, slug, excerpt, originalDate, content, cats, published) VALUES (:title, :subtitle, :slug, :excerpt, :originalDate, :content, :cats, :published)"; 

    try { 
     $db = getConnection(); 
     $stmt = $db->prepare($sql); 

     $stmt->bindParam("title", $item->title); 
     $stmt->bindParam("subtitle", $item->subtitle); 
     $stmt->bindParam("slug", $item->slug); 
     $stmt->bindParam("excerpt", $item->excerpt); 
     $stmt->bindParam("originalDate", $item->originalDate); 
     $stmt->bindParam("content", $item->content); 
     $stmt->bindParam("cats", $item->cats); 
     $stmt->bindParam("published", $item->published); 

     $stmt->execute(); 
     $item->id = $db->lastInsertId(); 
     $db = null; 

     print_r(json_encode($item)); 
    } 
    catch(PDOException $e) { 
     echo '{"error":{"text":'. $e->getMessage() .'}}'; 
    } 
} 

codice angolare, per la casella di selezione

<div class="col-sm-12 m-b-30"> 
    <p class="f-500 c-black m-b-15">Categorias</p> 
    <div class="btn-group bootstrap-select show-tick"> 
    <button type="button" class="btn selectpicker btn-default" ng-model="item.cats" data-toggle="dropdown" title="Selecione a Categoria" aria-expanded="false" data-html="1" data-multiple="1" bs-options="cats.id as cats.name for cats in categorias" bs-select> 
     Selecione a categoria <span class="caret"></span> 
    </button> 

    </div> 
</div> 

gatti, restituire Array sulla colonna di mysql

Tutte le idee ?!

Esempio on-line: http://45.55.73.98/angular-admin/api/v1/s/posts

Aggiornato!

$cats = null; 
foreach ($item->cats as $cat) { 
    $cats .= $cat . ","; 
} 

e

$stmt->bindParam("cats", $cats); 
+0

POST JSON = gatti \t [ "1", "2"] \t contenuti \t "dfgdfgdfg" \t estratto \t "dfgdfgdfg" \t originalDate .210 "2015-06-24T03: 00: 00.000Z" \t pubblicati \t veri \t sezione \t "post" \t slug \t "gdfg" \t sottotitolo \t "gdf" \t titolo –

+0

Puoi pubblicare il tuo codice angolare –

+0

Ho aggiunto il codice ora, la risposta JSON questo ultimo link –

risposta

0

Come indicato nei commenti e la soluzione è fornita nella modifica della domanda, sto solo mettendo la risposta qui (modificato come suggerito nel commento).

$cats=implode(',',$item->cats); 

e

$stmt->bindParam("cats", $cats);