2012-05-08 5 views
1

나는 꽤 복잡한 PHP/HTML보기를 가지고있다. 그러나 최근에 AJAX를 사용하여 자동으로 새로 고치기 위해 사이트를 변경했습니다. 현재 JSON 배열을 반환하고 해당 데이터를 사용하여 내 View를 재현해야합니다.보기에서 JSON 데이터 사용하기

다음
$(function() { 
    $("#selectable").selectable({ 
     selected: updatefilters, 
    }); 
    function updatefilters(ev, ui){ 
     // get the selected filters 
     var $selected = $('#selectable').children('.ui-selected'); 
     // create a string that has each filter separated by a pipe ("|") 
     var filters = $selected.map(function(){return this.id;}).get().join("\|"); 
     $.ajax({ 
      type: "POST", 
      url: 'updatefilters', 
      dataType: 'json', 
      data: { filters: filters }, 
      success: function(data){ 
       var html = "<div id ='board'><table>"; 
       for (i=0 ; i< data.length ; i++) 
       { 
        html += "<tr><td>" + data[i].subject + "</td><td>" + data[i].body + "</td></tr>"; 
       } 
       html += "</table></div>"; 
       $('#board').html(html); 
      } 
     }); 
    } 
}); 

내 PHP 파일입니다 :

<div id='board'> 
<?php 
if ($threads) 
    { 
     $count = count($threads); 
     $perpage = 17; 
     $startlist = 3; 
     $numpages = ceil($count/$perpage); 
     if ($page == 'home') {$page = 1;} 
     $threadstart = ($page * $perpage) - $perpage; 
     $i = 0; 
     echo "<table class='board'> 
    <tr> 
      <th width='5%'><img src='".base_url()."img/board/icons/category_icon.png' alt='category_icon'/></th> 
      <th width='5%'>Tag</th> 
      <th width='50%'>Subject</th> 
      <th width='7.5%'>Author</th> 
      <th width='7.5%'>Replies/Views</th> 
      <th width='15%'>Last Post</th> 
    </tr>"; 
     foreach ($threads as $list) 
     { $i++; 
      $thread_owner = $this->thread_model->get_owner($list['owner_id']); 
      $thread_id = $list['thread_id']; 
      $query = $this->db->query("SELECT f.tag FROM filter_thread AS ft 
             INNER JOIN filter AS f ON ft.filter_id = f.filter_id 
             WHERE thread_id = $thread_id"); 
      $result = $query->result_array(); 
      $trunc_body = $this->thread_model->get_reply_summary($thread_id); 
      $filter = ""; 
      $filter2 =""; 
      $ent = "entertainment"; 
      foreach ($result as $string) 
      { 
       if ($string['tag'] == $ent) { 
        $filter2 .= "<div id='entertainment'><p>"; 
        $filter2 .= "<img src='".base_url()."img/board/icons/entertainment_icon.png' title='Entertainment' alt='Entertainment'/>"; 
        $filter2 .= "</p></div>"; 
       } else { 
       $filter2 .= "<div id='misc'><p>"; 
       $filter2 .= "<img src='".base_url()."img/board/icons/misc_icon.png' title='Misc' alt='Misc'/>"; 
       $filter2 .= "</p></div>"; 
       $filter .= " [".$string['tag']."]"; 
       } 
      } 
      if (!$result) { $filter = "" AND $filter2 =""; } 
      if ($i > $threadstart AND $i <= $threadstart + $perpage) 
      { 
       echo "<tr>"; 
       echo "<td>".$filter2."</td>"; 
       echo "<td>".$filter."</td>"; 
       echo "<td title='".$trunc_body['0']['body']."'><a href=".base_url()."main/thread/".$list['slug'].">".ucfirst($list['subject'])."<div id='filter'><p></p></div></a></td>"; 
       echo "<td><p>".$thread_owner."</p></td>"; 
       echo "<td align='right'>Replies: ".$list['replies_num']."<br>Views: ".$list['views']."</td>"; 
       $owner = $this->thread_model->get_owner($list['last_post_id']); 
       echo "<td>".$owner." <br>".$list['replystamp'] ."</td></tr>"; 
      } 
     } 
     echo "</table>"; 
     echo "<br>"; 
     echo "Current Page: $page | ";  
     echo "Page: "; 

     for ($n = 1; $n < $numpages+1; $n++) 
     { 
      echo "<a href=".base_url()."main/home/$n>$n</a> | "; 
     } 
    } 
?> 
</div> 

이 달성의 너무 어려운 방법이 여기에

내 AJAX입니까?

+0

감사. JSON 데이터를 어떻게 얻을 수 있습니까? –

+0

JSON에서 HTML을 보내려 하시겠습니까? 아니면 완전히 데이터 중심의 접근 방식을 찾고 계십니까? JSON에서 원시 데이터를 보내고 프론트 엔드의 HTML에 데이터를 삽입하는 것을 처리하는 경우 PHP에서 모든 HTML을 유지할 수 있습니다. – jmort253

답변

1

header("Content-Type: text/plain");을 PHP 파일의 맨 위에 추가하십시오.

또한 함수 후에 <div id='board'>을 스크립트 내부의 변수로 이동하십시오.

게다가, 모든 변수 echo 년대 변경 배열이 변수를 넣고

json_encode($array); 

예 사용 개시 용

$output = "<div id='board'>"; 

$output .= "<table class='board'> 
    <tr> 
      <th width='5%'><img src='".base_url()."img/board/icons/category_icon.png' alt='category_icon'/></th> 
      <th width='5%'>Tag</th> 
      <th width='50%'>Subject</th> 
      <th width='7.5%'>Author</th> 
      <th width='7.5%'>Replies/Views</th> 
      <th width='15%'>Last Post</th> 
    </tr>"; 

echo json_encode(array('output'=>$output)); 
+0

내가 혼란스럽게 생각하는 부분은 현재 HTML과 동일한보기에서 자바 스크립트가 없다는 것입니다. rickymason.net/letschat/main/home <- 사이트가 무슨 일이 벌어지고 있는지 더 잘 이해하는 데 도움이됩니다. 카테고리를 클릭하면 데이터를 가져오고 뷰를 사용하여 HTML을 만들어야합니다. 나는 AJAX가 있어야하는 곳과 PHP 파일이 전체 방정식을 입력해야하는 곳을 계속 따라 가지 않습니다. –

+0

리키, 필요한 HTML 템플릿 솔루션입니다. 콧수염처럼 데이터가 HTML 템플릿에 들어가는 위치를 나타냅니다. '{ "subject": "제목", "저자": "John"}'은 템플릿'

{author}
{subject}
'에 들어갈 수 있습니다. 필자는 PHP에 대한 지식이 없기 때문에 실제로 시작할 수 있습니다. Google 콧수염 또는 HTML 템플릿. – jmort253

+0

감사합니다! 그것으로 들여다 볼 것입니다 –

관련 문제