doumengbai2031 2013-06-09 23:45
浏览 53
已采纳

从数组中将对象保存到数据库[关闭]

I'm trying to save several objects from predefined data. It saves only first set of data or if I change code it saves data as "array". Here is the code:

<?php

$category = array(
    'Account/Profile',
    'Blogs',
);

$question = array(
    'How do I change my email notifications',
    'How do I edit my profile details',
    'What is a blog?',
);

$answer = array(
    '<p>To change your email notifications</p>',
    '<p>To change your profile details</p>',
    '<p>A weblog, or blog, is.</p>',
);


$questionCount = count($question);
for ($i = 0; $i <= $questionCount; $i++) {
    $faq           = new Object();
    $faq->category = $category[$i];
    $faq->question = $question[$i];
    $faq->answer   = $answer[$i];
}

$didSave = $faq->save();

How can I pull data from arrays and create multiple objects using that data?

  • 写回答

1条回答 默认 最新

  • doushuangai9733 2013-06-09 23:58
    关注

    Here you go....

     $cats = array('Account/Profile', 'Blogs');
    
     $question = array(
      'How do I change my email notifications', 
      'What is a blog?',);
    
     $answer = array(
      '<p>To change your email notifications you can do so from notifications page</p>', 
      '<p>A weblog, or blog, is arguably one of the fundamental DNA pieces of most types of social networking site.</p>',);
    
    //Then count the questions and iterate
     for($i=0;$i<=count($question);$i++)
      {
       $object = New Object; //Your object class
       //NEW OBJECT TO SAVE, I call it object, but you have to fill this in,
       //with whatever class/object it is youre creating, and saving
       $object->category = $cats[$i];
       $object->question = $question[$i];
       $object->answer = $answer[$i];
       $object->save();
      }
    

    NEW EDIT BASED ON YOUR REPOST

     for($i=0;$i<=count($question);$i++)
     {
     $faq = new ElggObject();
     $faq->subtype = 'faq';
        $faq->container_guid = $CONFIG->site_guid;
        $faq->owner_guid = $CONFIG->site_guid;
     $faq->category = $cats[$i];
     $faq->question = $question[$i];
     $faq->answer = $answer[$i];
     $faq->access_id = $access;
     if($faq->save()) {
            system_message(elgg_echo("faq:add:success"));
        } else {
            register_error(elgg_echo("faq:add:error:save"));
        }
    
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 图示五个参数的模型校正是用什么方法做出来的。如何建立其他模型
  • ¥100 描述一下元器件的基本功能,pcba板的基本原理
  • ¥15 STM32无法向设备写入固件
  • ¥15 使用ESP8266连接阿里云出现问题
  • ¥15 BP神经网络控制倒立摆
  • ¥20 要这个数学建模编程的代码 并且能完整允许出来结果 完整的过程和数据的结果
  • ¥15 html5+css和javascript有人可以帮吗?图片要怎么插入代码里面啊
  • ¥30 Unity接入微信SDK 无法开启摄像头
  • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
  • ¥20 cad图纸,chx-3六轴码垛机器人