duanhanglekr37902 2011-04-08 12:01
浏览 18
已采纳

如何在CodeIgniter中返回函数的完整输出?

I need to output all books from all shelfs. This code bellow only outputs last shelf of books. Any help would be helpful.

My Controller:

function index()
{
  $data['books'] = $this->_books();
  $this->load->view('books_view', $data);
}

function _books() {
  $xml = simplexml_load_file('books.xml');
  foreach ($xml->shelfs as $shelf)
  {
    $result = '<optgroup label="'.$shelf['id'].'">';
    foreach ($shelf->books as $book)
    {
      $result .= '<option value="'.$book->title.'">'.$book->title.'</option>';
    }
    $result .= '</optgroup>';
  }
  return $result;
}

My View:

echo form_open('books#');
echo '<select name="books[]" multiple="multiple" onClick="this.form.submit()">';
echo $options;
echo '</select></form>';

My Output:

only last shelf which is "Z".

My XML Data:

<?xml version="1.0" encoding="UTF-8" ?>
<library>

<shelfs id="A">
  <strip>
    <title>Book Title #1 for A</title>
    <author>Author Name #1 for A</author>
  </strip>
  <strip>
    <title>Book Title #2 for A</title>
    <author>Author Name #2 for A</author>
  </strip>
</comics>

...

<shelfs id="Z">
  <strip>
    <title>Book Title #1 for Z</title>
    <author>Author Name #1 for Z</author>
  </strip>
  <strip>
    <title>Book Title #2 for Z</title>
    <author>Author Name #2 for Z</author>
  </strip>
</comics>

</library>
  • 写回答

2条回答 默认 最新

  • dongtao1262 2011-04-08 12:03
    关注

    you are overwriting $result it should be .= and defined before foreach begins

    function _books() {
      $xml = simplexml_load_file('books.xml');
      $result='';
      foreach ($xml->shelfs as $shelf)
      {
        $result.= '<optgroup label="'.$shelf['id'].'">';
        foreach ($shelf->books as $book)
        {
          $result .= '<option value="'.$book->title.'">'.$book->title.'</option>';
        }
        $result .= '</optgroup>';
      }
      return $result;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示