2013-09-30 47 views
0

從這個代碼中,我得到正是我想在XML,但因爲前面有一行打亂了XML使用CodeIgniter

function Users_get(){ 
    $this->load->helper('url'); 
    $this->load->model('User_model'); 
    $data['rows'] = $this->User_model->getUsers(); 

    $f = 1; 
    foreach ($data['rows'] as $r): 
     $users[$f] = array(
      'user_id' => $r->ID, 
      'Destination' => $r->Destination); 
     $f++; 
    endforeach; 

    $this->response($users, 200); // 200 being the HTTP response code 
} 

在XML結果一個空行,它不會在瀏覽器中運行:

LINE 1 :EMPTY LINE MESSING UP THE ENTIRE OUTPUT 
LINE 2 :<?xml version="1.0" encoding="utf-8"?> 
LINE 3 :<xml><item><user_id>1</user_id><Destination>Valencia</Destination></item>... 

由於空行的,我得到錯誤:

"This page contains the following errors:

error on line 2 at column 6: XML declaration allowed only at the start of the document Below is a rendering of the page up to the first error."

JSON輸出確定....

我看來

<body> 

     <a href="<?php echo site_url('userAPI/user/user_id/1');?>">Specific user</a> 
     <br> 

     <a href="<?php echo site_url('userAPI/alluser');?>">All users</a> 
     <!-- <p>FlightFrom: <?php //returnJasonName('2');?></p> --> 
     <br> 
     <a href="<?php echo site_url('userAPI/Users_get/user_id/1');?>">user from database</a> 
     <br> 


     <?php foreach ($rows as $r):?> 
      <p><?php echo $r->ID;?></p> 
      <div><?php echo $r->Destination; ?></div> 

    <?php endforeach; ?> 

</body> 
+0

我不熟悉CodeIgniter,但我會說這是來自控制器的代碼。該視圖中沒有插入空行嗎? –

+0

嘗試http://stackoverflow.com/a/10361155/496176 – quantme

+0

顯示您的視圖的代碼。 –

回答

1

我不是一個笨的專家,所以我真的不能幫你找出問題所在。

但是,一個快速和骯髒的解決方案將是在輸出上使用trim()