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 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 Revit2020下载问题
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 单片机无法进入HAL_TIM_PWM_PulseFinishedCallback回调函数
  • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 如何在炒股软件中,爬到我想看的日k线