-2

定義済みデータからいくつかのオブジェクトを保存しようとしています。最初のデータセットのみを保存するか、コードを変更するとデータを「配列」として保存します。コードは次のとおりです。

<?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();

配列からデータを取得し、そのデータを使用して複数のオブジェクトを作成するにはどうすればよいですか?

4

1 に答える 1

0

どうぞ....

 $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();
  }

あなたの再投稿に基づく新しい編集

 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"));
    }

}
于 2013-06-09T23:58:07.127 に答える