字段 'cats' 是 Angular 应用程序中的选择框,包含来自表格 cats 的数据,其中包括 id、name 和 module_id。
在 Slim 框架上添加项目的函数。
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() .'}}';
}
}
Angular代码,用于选择框
<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>
猫,返回mysql列上的数组
有什么想法?!
在线示例:http://45.55.73.98/angular-admin/api/v1/s/posts
已更新!
$cats = null;
foreach ($item->cats as $cat) {
$cats .= $cat . ",";
}
并且
$stmt->bindParam("cats", $cats);