使用PHP和jQuery

时间:2016-04-20 15:58:53

标签: php jquery silverstripe

我在一个列出文章的SilverStripe网站的页面上工作。我已经在可用年份中包含一个下拉列表来返回特定文章。我遇到的麻烦是找到一种方法将下拉列表中的选定日期与文章数据对象的日期字段的数据库值相连接。

这是一篇文章数据对象类,其中包含一个函数,用于在名为getYearCreated的函数中存储每篇文章的年份:

class RecentCoverageDoc extends DataObject {

    private static $db = array(
        'Title' => 'varchar(250)',
        'Publisher' => 'varchar(250)',
        'Date' => 'date',
    );

    private static $has_one = array(
        'ArticleDoc' => 'File',
    );

    private static $summary_fields = array(
        'Title' => 'Title',
        'Publisher' => 'Publisher',
        'Date' => 'Date'
    );

    public function getYearCreated() {
      return date('Y', strtotime($this->Date));
    }

}

在Page.php中,这是我正在进行构建的功能,该功能旨在从年份下拉列表中获取所选值,并使用它来获取与该年度相关的正确文章列表:

public function getDocsByYear(){
        //Get the year selected by the dropdown
        $docYear = $this->getRequest()->param('ID');

        //Group together all docs that are associated with that selected year
        $documents = GroupedList::create(RecentCoverageDoc::get()->sort('Date'));

        $return = array();

        //put the articles into the array that match the selected year
        foreach($documents as $document){
            $docDate = date("Y", strtotime($document->Date));
            $selectedDate = date("Y",strtotime($docYear));
             if($docDate == $selectedDate){
                $return[] = array(
                   'title' => $document->Title,
                   'date' => $document->Date,
                    'publisher' =>$document->Publisher
                );
             }
        }

        return json_encode($return);

    }

最后,jQuery代码获取年份下拉值并将其发送到getDocsByYear函数:

   (function($) {
    $(document).ready(function() {

        var SelectYear = $('#SelectYear');

        SelectYear.change(function() {
            if (SelectYear.val() != "" && SelectYear.val() != null) {
                sendYear();
            }
        });

        function sendYear(){
            var year = SelectYear.find('option:selected').attr('value');
            $.ajax({
                type: "POST",
                url: "/home/getDocsByYear/"+year,
                dataType: "json"
            }).done(function (response) {
                var list = '';
                var docSection = $('.RecentDocs');

                for (var i=0;i<response.length;i++){
                    list += response[i].date + ', ' + response[i].publisher + ', ' + '<a href="' + response[i].title + ' target="_blank">"' + response[i].title +"</a> <br />";
                }
                docSection.empty();
                docSection.append(list);
            });
        }

    });
}(jQuery));

我试图根据从下拉列表中选择的年份来考虑获取文章列表的最佳方法。使用GroupedList功能填充页面上的年份下拉列表和文章列表:

<select id="SelectYear">
    <% loop $GroupedDocsByDate.GroupedBy(YearCreated) %>
        <option value="$YearCreated">$YearCreated</option>
    <% end_loop %>
</select>
<br /><br />
<div class="RecentDocs">
    <% loop $getAllRecentCoverageDocs %>
        $Date, <a href="$ArticleDoc.URL" target="_blank">$Title</a>, $Publisher<br /><br />
    <% end_loop %>
</div>

我认为使用类似的东西可以用来逐年获取文章,但是我仍然坚持如何完成getDocsByYear功能,因此我根据所选年份确定了正确的文章列表。下拉列表。现在,凭借我拥有的东西,无论选择哪一年,我只会收到一篇文章,这是不正确的。任何建议都会有所帮助!

1 个答案:

答案 0 :(得分:2)

好的,终于搞定了!

     public function getDocsByYear(){
        //Get the year selected by the dropdown
        $docYear = $this->getRequest()->param('ID');

        //Group together all docs that are associated with that selected year
        $documents = RecentCoverageDoc::get();
        $return = array();

        //put the articles into the array that match the selected year
        foreach($documents as $document){
            $docDate = date("Y", strtotime($document->Date));

             if($docDate == $docYear){
                $return[] = array(
                   'title' => $document->Title,
                   'date' => $document->Date,
                    'publisher' =>$document->Publisher
                );
             }
        }

        return json_encode($return);

    }

我不需要GroupedList。我让它变得太复杂了。这种方法似乎有效。不确定它是否是最好的方法,但我也没有看到任何错误。