jjzjj

javascript - 将用户直接带到事件日期与当前日期匹配的页面(分页)

coder 2024-04-05 原文

所以我有以下功能来获取我的帖子,但我希望用户直接进入帖子日期为“今天”的页面,这不是事件发布日期,而是事件发生的日期,就像我有具有 man_date 字段的事件 2015-4-12 今天是 2015-4-12 我将直接看到没有该事件的页面将这些事件放在第一页。

function get_table(){
                global $wpdb;
                $c_cid = $_REQUEST['cat'];
                if(isset($_REQUEST["page"])){
                    $page_number = filter_var($_REQUEST["page"], FILTER_SANITIZE_NUMBER_INT, FILTER_FLAG_STRIP_HIGH); //filter number
                    if(!is_numeric($page_number)){die('Invalid page number!');} //incase of invalid page number
                }else{
                    $page_number = 1; //if there's no page number, set it to 1
                }
                $item_per_page = 20;
                //get total number of records from database for pagination
                $get_total_row = $wpdb->get_results("SELECT wp_posts.* FROM wp_posts INNER JOIN wp_term_relationships ON (wp_posts.ID = wp_term_relationships.object_id) INNER JOIN wp_term_taxonomy ON (wp_term_relationships.term_taxonomy_id = wp_term_taxonomy.term_taxonomy_id) WHERE wp_term_taxonomy.taxonomy = 'tipo_manifestazione' AND wp_term_taxonomy.term_id = $c_cid AND wp_posts.post_status = 'publish' AND (wp_posts.vip = 'NULL' OR wp_posts.vip IS NULL) AND wp_posts.post_type = 'manifestazione' GROUP BY wp_posts.ID ORDER BY wp_posts.ID DESC");
                $get_total_rows = count($get_total_row);
                //break records into pages
                $total_pages = ceil($get_total_rows/$item_per_page);

                //get starting position to fetch the records
                $page_position = (($page_number-1) * $item_per_page);

                ?>
            <section class="table_container">
                                            <div class="controls_container">
                                              <div class="control">
                                                <select id="months" name="months">
                                                  <option value="" selected>Seleziona Mese</option>
                                                  <option value="01">Gennaio</option>
                                                  <option value="02">Febbraio</option>
                                                  <option value="03">Marzo</option>
                                                  <option value="04">Aprile</option>
                                                  <option value="05">Maggio</option>
                                                  <option value="06">Giugno</option>
                                                  <option value="07">Luglio</option>
                                                  <option value="08">Agosto</option>
                                                  <option value="09">Settembre</option>
                                                  <option value="10">Ottobre</option>
                                                  <option value="11">Novembre</option>
                                                  <option value="12">Dicembre</option>
                                                </select>
                                                </div>
                                                <div class="control">
                                                  <input type="text" name="location_search" id="location_search" placeholder="Luogo..."/>
                                                </div>
                                                <div class="control">
                                                  <div class="loading" style="display:none;"></div>
                                                </div>
                                                <div class="control right">
                                                <?php echo paginate_function($item_per_page, $page_number, $get_total_rows, $total_pages); ?>
                                            </div>
                                            </div>
                                            <header class="table_header">
                                              <div class="sec_th first">
                                                <span class="sec_th_t">Voto</span>
                                              </div>
                                              <div class="sec_th second">
                                                <span class="sec_th_t">Data</span>
                                              </div>
                                              <div class="sec_th third">
                                                <span class="sec_th_t">Manifestazione</span>
                                              </div>
                                              <div class="sec_th fourth">
                                                <span class="sec_th_t">Località</span>
                                              </div>
                                              <div class="sec_th fifth">
                                                <span class="sec_th_t">Percorso Lungo</span>
                                              </div>
                                              <div class="sec_th sixth">
                                                <span class="sec_th_t">Info</span>
                                              </div>
                                              </header>
                                              <div id="rows_container">
                                              <?php
                                                   $query = "SELECT wp_posts.* FROM wp_posts INNER JOIN wp_term_relationships ON (wp_posts.ID = wp_term_relationships.object_id) INNER JOIN wp_term_taxonomy ON (wp_term_relationships.term_taxonomy_id = wp_term_taxonomy.term_taxonomy_id) WHERE wp_term_taxonomy.taxonomy = 'tipo_manifestazione' AND wp_term_taxonomy.term_id = $c_cid AND wp_posts.post_status = 'publish' AND (wp_posts.vip = 'NULL' OR wp_posts.vip IS NULL) AND wp_posts.post_type = 'manifestazione' GROUP BY wp_posts.ID ORDER BY wp_posts.ID DESC LIMIT $page_position, $item_per_page";

                                                      $posts = $wpdb->get_results($query);
                                                      foreach($posts as $post){
                                                        $permalink = get_permalink($post->ID);
                                                        $locations = $wpdb->get_results("SELECT * FROM wp_locations WHERE post_id = $post->ID");
                                                        $l = $locations[0];
                                                  ?>
                                              <article id="row_<?php echo $post->ID;?>" class="table_row">
                                                <div class="ar_td first">
                                                  <span class="ar_td_t"><?php echo do_shortcode('[ratings id="'.$post->ID.'"]');?></span>
                                                </div>
                                                <div class="ar_td second">
                                                  <span class="ar_td_t"><?php echo date("d-m-Y", strtotime(get_field('m_f_data',$post->ID)));?></span>
                                                </div>
                                                <div class="ar_td third">
                                                  <a href="<?php echo $permalink;?>"><span class="ar_td_t"><?php echo $post->post_title;?></span></a>
                                                </div>
                                                <div class="ar_td fourth">
                                                 <a href="<?php echo site_url();?>/geo/?location=<?php echo $l->luogo;?>"> <span class="ar_td_t"><?php echo $l->luogo;?></span></a>
                                                </div>
                                                <div class="ar_td fifth">
                                                  <span class="ar_td_t"><?php echo get_field('m_f_percorso_lungo',$post->ID);?></span>
                                                </div>
                                                <div class="ar_td sixth">
                                                  <a href="javascript:void(0);" onclick="show_info('div_<?php echo $post->ID;?>','row_<?php echo $post->ID;?>');" class="show_button">Info +</a>
                                                </div>
                                              </article>
                                              <div id="div_<?php echo $post->ID;?>" class="div_content">
                                                <?php get_post_fields($post->ID);?>
                                              </div>
                                                  <?php
                                                  }
                                                ?>
                                              </div>
                                          </section>
                <?php
                exit();
            }

function paginate_function($item_per_page, $current_page, $total_records, $total_pages)
    {
        $pagination = '';
        if($total_pages > 0 && $total_pages != 1 && $current_page <= $total_pages){ //verify total pages and current page number
            $pagination .= '<ul class="pagination">';

            $right_links    = $current_page + 3; 
            $previous       = $current_page - 3; //previous link 
            $next           = $current_page + 1; //next link
            $first_link     = true; //boolean var to decide our first link

            if($current_page > 1){
                $previous_link = ($previous==0)?1:$previous;
                $pagination .= '<li class="first"><a href="#" data-page="1" title="First">&laquo;</a></li>'; //first link
                $pagination .= '<li><a href="#" data-page="'.$previous_link.'" title="Previous">&lt;</a></li>'; //previous link
                    for($i = ($current_page-2); $i < $current_page; $i++){ //Create left-hand side links
                        if($i > 0){
                            $pagination .= '<li><a href="#" data-page="'.$i.'" title="Page'.$i.'">'.$i.'</a></li>';
                        }
                    }   
                $first_link = false; //set first link to false
            }

            if($first_link){ //if current active page is first link
                $pagination .= '<li class="first active">'.$current_page.'</li>';
            }elseif($current_page == $total_pages){ //if it's the last active link
                $pagination .= '<li class="last active">'.$current_page.'</li>';
            }else{ //regular current link
                $pagination .= '<li class="active">'.$current_page.'</li>';
            }

            for($i = $current_page+1; $i < $right_links ; $i++){ //create right-hand side links
                if($i<=$total_pages){
                    $pagination .= '<li><a href="#" data-page="'.$i.'" title="Page '.$i.'">'.$i.'</a></li>';
                }
            }
            if($current_page < $total_pages){ 
                    $next_link = ($i > $total_pages)? $total_pages : $i;
                    $pagination .= '<li><a href="#" data-page="'.$next_link.'" title="Next">&gt;</a></li>'; //next link
                    $pagination .= '<li class="last"><a href="#" data-page="'.$total_pages.'" title="Last">&raquo;</a></li>'; //last link
            }

            $pagination .= '</ul>'; 
        }
        return $pagination; //return pagination links
    }

Ajax :

jQuery("#get_table").load(ajaxurl,{'action':'get_table','cat':cat,'page':page});

代码运行良好,获取帖子和分页也运行良好,唯一我无法弄清楚的是,我如何才能使这件事直接在当前匹配日期的帖子所在的页面上使用.

最佳答案

这是我自己想出来的。

通过使用下面的查询,我得到了当前日期和过去日期的帖子。

$date = date("Y-m-d");
$posts_c = $wpdb->get_results("SELECT * FROM wp_posts WHERE wp_posts.man_date <= $date");

使用上面的代码,我将获取行,现在让我们将它们转换为计数

$get_count = count($posts_c);

现在我所做的是将帖子数除以每页的帖子数,即 20

 $page_on = ceil($get_count / 20);

就是这样 $page_on 对象包含我们将发送给用户的页面的值。

关于javascript - 将用户直接带到事件日期与当前日期匹配的页面(分页),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30398289/

有关javascript - 将用户直接带到事件日期与当前日期匹配的页面(分页)的更多相关文章

  1. ruby 正则表达式 - 如何替换字符串中匹配项的第 n 个实例 - 2

    在我的应用程序中,我需要能够找到所有数字子字符串,然后扫描每个子字符串,找到第一个匹配范围(例如5到15之间)的子字符串,并将该实例替换为另一个字符串“X”。我的测试字符串s="1foo100bar10gee1"我的初始模式是1个或多个数字的任何字符串,例如,re=Regexp.new(/\d+/)matches=s.scan(re)给出["1","100","10","1"]如果我想用“X”替换第N个匹配项,并且只替换第N个匹配项,我该怎么做?例如,如果我想替换第三个匹配项“10”(匹配项[2]),我不能只说s[matches[2]]="X"因为它做了两次替换“1fooX0barXg

  2. ruby - 匹配未转义的平衡定界符对 - 2

    如何匹配未被反斜杠转义的平衡定界符对(其本身未被反斜杠转义)(无需考虑嵌套)?例如对于反引号,我试过了,但是转义的反引号没有像转义那样工作。regex=/(?!$1:"how\\"#expected"how\\`are"上面的正则表达式不考虑由反斜杠转义并位于反引号前面的反斜杠,但我愿意考虑。StackOverflow如何做到这一点?这样做的目的并不复杂。我有文档文本,其中包括内联代码的反引号,就像StackOverflow一样,我想在HTML文件中显示它,内联代码用一些spanMaterial装饰。不会有嵌套,但转义反引号或转义反斜杠可能出现在任何地方。

  3. ruby-on-rails - 使用 rails 4 设计而不更新用户 - 2

    我将应用程序升级到Rails4,一切正常。我可以登录并转到我的编辑页面。也更新了观点。使用标准View时,用户会更新。但是当我添加例如字段:name时,它​​不会在表单中更新。使用devise3.1.1和gem'protected_attributes'我需要在设备或数据库上运行某种更新命令吗?我也搜索过这个地方,找到了许多不同的解决方案,但没有一个会更新我的用户字段。我没有添加任何自定义字段。 最佳答案 如果您想允许额外的参数,您可以在ApplicationController中使用beforefilter,因为Rails4将参数

  4. ruby-on-rails - date_field_tag,如何设置默认日期? [ rails 上的 ruby ] - 2

    我想设置一个默认日期,例如实际日期,我该如何设置?还有如何在组合框中设置默认值顺便问一下,date_field_tag和date_field之间有什么区别? 最佳答案 试试这个:将默认日期作为第二个参数传递。youcorrectlysetthedefaultvalueofcomboboxasshowninyourquestion. 关于ruby-on-rails-date_field_tag,如何设置默认日期?[rails上的ruby],我们在StackOverflow上找到一个类似的问

  5. ruby-on-rails - Ruby 检查日期时间是否为 iso8601 并保存 - 2

    我需要检查DateTime是否采用有效的ISO8601格式。喜欢:#iso8601?我检查了ruby​​是否有特定方法,但没有找到。目前我正在使用date.iso8601==date来检查这个。有什么好的方法吗?编辑解释我的环境,并改变问题的范围。因此,我的项目将使用jsapiFullCalendar,这就是我需要iso8601字符串格式的原因。我想知道更好或正确的方法是什么,以正确的格式将日期保存在数据库中,或者让ActiveRecord完成它们的工作并在我需要时间信息时对其进行操作。 最佳答案 我不太明白你的问题。我假设您想检查

  6. ruby - 检查日期是否在过去 7 天内 - 2

    我的日期格式如下:"%d-%m-%Y"(例如,今天的日期为07-09-2015),我想看看是不是在过去的七天内。谁能推荐一种方法? 最佳答案 你可以这样做:require"date"Date.today-7 关于ruby-检查日期是否在过去7天内,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/32438063/

  7. ruby - 匹配大写字母并用后续字母填充,直到一定的字符串长度 - 2

    我有一个驼峰式字符串,例如:JustAString。我想按照以下规则形成长度为4的字符串:抓取所有大写字母;如果超过4个大写字母,只保留前4个;如果少于4个大写字母,则将最后大写字母后的字母大写并添加字母,直到长度变为4。以下是可能发生的3种情况:ThisIsMyString将产生TIMS(大写字母);ThisIsOneVeryLongString将产生TIOV(前4个大写字母);MyString将生成MSTR(大写字母+tr大写)。我设法用这个片段解决了前两种情况:str.scan(/[A-Z]/).first(4).join但是,我不太确定如何最好地修改上面的代码片段以处理最后一种

  8. ruby-on-rails - 将 Ruby 中的日期/时间格式化为 YYYY-MM-DD HH :MM:SS - 2

    这个问题在这里已经有了答案:Railsformattingdate(4个答案)关闭4年前。我想格式化Time.Now函数以显示YYYY-MM-DDHH:MM:SS而不是:“2018-03-0909:47:19+0000”该函数需要放在时间中.现在功能。require‘roo’require‘roo-xls’require‘byebug’file_name=ARGV.first||“Template.xlsx”excel_file=Roo::Spreadsheet.open(“./#{file_name}“,extension::xlsx)xml=Nokogiri::XML::Build

  9. ruby - 查找字符串中的内容类型(数字、日期、时间、字符串等) - 2

    我正在尝试解析一个CSV文件并使用SQL命令自动为其创建一个表。CSV中的第一行给出了列标题。但我需要推断每个列的类型。Ruby中是否有任何函数可以找到每个字段中内容的类型。例如,CSV行:"12012","Test","1233.22","12:21:22","10/10/2009"应该产生像这样的类型['integer','string','float','time','date']谢谢! 最佳答案 require'time'defto_something(str)if(num=Integer(str)rescueFloat(s

  10. ruby-on-rails - 简单的 Ruby on Rails 问题——如何将评论附加到用户和文章? - 2

    我意识到这可能是一个非常基本的问题,但我现在已经花了几天时间回过头来解决这个问题,但出于某种原因,Google就是没有帮助我。(我认为部分问题在于我是一个初学者,我不知道该问什么......)我也看过O'Reilly的RubyCookbook和RailsAPI,但我仍然停留在这个问题上.我找到了一些关于多态关系的信息,但它似乎不是我需要的(尽管如果我错了请告诉我)。我正在尝试调整MichaelHartl'stutorial创建一个包含用户、文章和评论的博客应用程序(不使用脚手架)。我希望评论既属于用户又属于文章。我的主要问题是:我不知道如何将当前文章的ID放入评论Controller。

随机推荐