【问题标题】:Put Array json on mysql return Array将数组json放在mysql上返回数组
【发布时间】:2015-09-11 08:48:13
【问题描述】:

“cats”字段是 Angular 应用程序的一个选择框,包含来自表猫的数据,包括 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>

cats,返回 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);

【问题讨论】:

  • POST by JSON =cats ["1", "2"] content "dfgdfgdfg" excerpt "dfgdfgdfg" originalDate "2015-06-24T03:00:00.000Z" 发布真实部分 "posts" slug "gdfg" 副标题 "gdf" 标题
  • 你能发布你的 Angular 代码吗
  • 感谢所有回复的人,今天早上我得到了答案。我会在这里发布结果。
  • $cats=implode(',',$item-&gt;cats); 比循环容易。
  • @MarcoRiesco 如果您得到了上述答案,请在此处发布并接受您自己的答案。不要让问题保持开放。谢谢。

标签: php mysql arrays angularjs slim


【解决方案1】:

正如 cmets 中所述,并且在问题的编辑中提供了解决方案,我只是将答案放在这里(按照评论中的建议进行了修改)。

this-&gt;cats 是一个数组 - 首先将他转换为字符串:

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

然后绑定他:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-21
    • 2016-04-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多