2012-05-08 2 views
1

かなり複雑なPHP/HTMLビューがあります。しかし、私は最近AJAXを使ってサイトを自動的に更新するように変更しました。現在、JSON配列を返します。そのデータを使用してViewを再現する必要があります。ここでビュー内でのJSONデータの使用

は私のAJAXである:ここでは

$(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> 

は、これを達成するあまりにもハードではない方法はありますか?

+0

感謝を。そこにJSONデータを取得するにはどうすればよいですか? –

+0

JSONでHTMLを送信しようとしているのですか、完全にデータ駆動型のアプローチをお探しですか? JSONで未処理のデータを送信し、フロントエンドのHTMLにデータを挿入するだけであれば、そのHTMLをすべてPHPから削除することができます。 – jmort253

答えて

1

header("Content-Type: text/plain");をPHPファイルのTOPに追加してください。

また、<div id='board'>をスクリプト内の変数に移動します(header()関数の後)。

さらに、変数へのすべての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と同じビューでjavascriptを持っていないということです。 rickymason.net/letschat/main/home < - サイトは何が起こっているかについてより良い考えを得るのに役立つはずです。カテゴリをクリックすると、データが取得され、ビューを使用してHTMLを構築する必要があります。私はまだAJAXがどこにあるべきか、そしてPHPファイルがどこに入るのかを追っているわけではありません。 –

+0

リッキー、あなたが必要とするのは、HTMLテンプレートソリューションです。これは、あなたのデータがHTMLテンプレートのどこにあるのかを示すヒシャケのようなものです。 '{" subject ":" subject "、" author ":" John "}'はテンプレート '

{author}
{subject}
'に入ります。私はあなたに本当に素晴らしい例を与えるPHPの知識はありませんが、そこから始めることができます。 Googleの口頭またはHTMLテンプレート。 – jmort253

+0

ありがとう!それを見てください –

関連する問題