我有一个问题按类别显示数据

时间:2014-06-12 12:18:47

标签: php jquery mysql ajax

我试图创建一个电影网站。我几乎完成了所有事情。但这是一个问题。这是load_data.php文件的一部分,显示来自数据库的电影链接

$query_pag_num = "SELECT COUNT(*) AS count FROM videos";
$result_pag_num = mysql_query($query_pag_num);
$row = mysql_fetch_array($result_pag_num);
$count = $row['count'];
$no_of_paginations = ceil($count / $per_page);

这就是电影的显示位置

 <ul class="nav nav-tabs responsive" id="myTab">
    <li class="active"><a class="deco-none misc-class" href="#videos">All</a></li>
    <li><a href="#action">Action</a></li>
    <li><a href="#adventure">Adventure</a></li>
    <li><a href="#animation">Animation</a></li>
    <li><a href="#horror">Horror</a></li>
    <li><a href="#crime">Crime</a></li>
    <li><a href="#comedy">Comedy</a></li>
    <li><a href="#romance">Romance</a></li>
    <li><a href="#fantasy">Fantasy</a></li>
    <li><a href="#drama">Drama</a></li>
    <li><a href="#mystery">Mystery</a></li>
    <li><a class="deco-none" href="#thriller">Thriller</a></li>
    <li class="dropdown">
    <a data-toggle="dropdown" href="#">More <span class="caret"></span></a>
    <ul class="dropdown-menu pull-right" role="menu" aria-labelledby="dLabel">
    <li><a href="#war">War</a></li>
    <li><a href="#science-fiction">Science-fiction</a></li>
    <li><a href="#family">Family</a></li>
    </ul>
    </li>
    </ul>
  <div class="tab-content responsive">
    <div class="tab-pane fade in active" id="<?php $table='videos'; echo $table ?>">
<div class="row row1">
                <div style="margin-top:10px;"></div> 
    <div align="center" style="padding-bottom:80px" id="loading"></div>
    <div id="container">
        <div class="data"></div>
        <div class="pagination"></div>
    </div>

      </div>
    </div>
    <div class="tab-pane fade in" id="action">
<div class="row row1">
      </div>
    </div>

电影由jquery&amp; ajax.now一切正常,因为我有包含所有电影的表格视频,我正在显示视频表格中的所有数据,但我想要做的是点击每个类别来显示该类别的电影。 这是完成工作的jquery&amp; ajax

<script type="text/javascript">
            $(document).ready(function(){
                function loading_show(){
                    $('#loading').html("<i class='fa fa-cog fa-spin fa-5x'>    
</i>").fadeIn('fast');
                }
                function loading_hide(){
                    $('#loading').fadeOut('fast');
                }                
                function loadData(page){
                    loading_show();                    
                    $.ajax
                    ({
                        type: "POST",
                        url: "load_data.php",
                        data: "page="+page,
                        success: function(msg)
                        {
                            $("#container").ajaxComplete(function(event, request,
settings)
                            {
                                loading_hide();
                                $("#container").html(msg);
                            });
                        }
                    });
                }
                loadData(1);  // For first time page load default results
                $('#container .pagination li.active').live('click',function(){
                    var page = $(this).attr('p');
                    loadData(page);
                });           
                $('#go_btn').live('click',function(){
                    var page = parseInt($('.goto').val());
                    var no_of_pages = parseInt($('.total').attr('a'));
                    if(page != 0 && page <= no_of_pages){
                        loadData(page);
                    }else{
                        alert('Enter a PAGE between 1 and '+no_of_pages);
                        $('.goto').val("").focus();
                        return false;
                    } 
                });
            });
        </script>

这是完整的load-data.php文件

<?php
ob_start();
if($_POST['page'])
{
$page = $_POST['page'];
$cur_page = $page;
$page -= 1;
$per_page = 3;
$previous_btn = true;
$next_btn = true;
$first_btn = true;
$last_btn = true;
$start = $page * $per_page;
include"database.php";
function html2txt($document){ 
$search = array('@<script[^>]*?>.*?</script>@si','@<[\/\!]*?[^<>]*?>@si','@<style[^>]*?>.*?</style>@siU','@<![\s\S]*?--[ \t\n\r]*>@'); 
$text = preg_replace($search, '', $document); 
return $text; 
}
$query_pag_data = "SELECT * from videos order by id desc LIMIT $start, $per_page";
$result_pag_data = mysql_query($query_pag_data) or die('MySql Error' . mysql_error());
$msg = "";
while ($row = mysql_fetch_array($result_pag_data)) {
    $msg .= "<div class='col-sm-6 col-md-4'><div class='thumbnail'><img class='img-responsive' src='images/".html2txt($row['cover'])."'/><div class='caption'><h3>" . html2txt($row['title']) . "</h3><a href='movies.php?path=".html2txt($row['path'])."&title=".html2txt($row['title'])."&description=".html2txt($row['description'])."&trailer_title=".html2txt($row['trailer_title'])."&trailer_url=".html2txt($row['trailer_url'])."&imdb=".html2txt($row['imdb'])."&category_list=videos' class='btn btn-primary' role='button'><i class='fa fa-play-circle fa-1x'></i> Shiko filmin</a></div></div></div>";
}
$msg = "<div class='data'><ul>" . $msg . "</ul></div>"; // Content for Data


/* --------------------------------------------- */
$query_pag_num = "SELECT COUNT(*) AS count FROM videos";
$result_pag_num = mysql_query($query_pag_num);
$row = mysql_fetch_array($result_pag_num);
$count = $row['count'];
$no_of_paginations = ceil($count / $per_page);

/* ---------------Calculating the starting and endign values for the loop----------------------------------- */
if ($cur_page >= 7) {
    $start_loop = $cur_page - 3;
    if ($no_of_paginations > $cur_page + 3)
        $end_loop = $cur_page + 3;
    else if ($cur_page <= $no_of_paginations && $cur_page > $no_of_paginations - 6) {
        $start_loop = $no_of_paginations - 6;
        $end_loop = $no_of_paginations;
    } else {
        $end_loop = $no_of_paginations;
    }
} else {
    $start_loop = 1;
    if ($no_of_paginations > 7)
        $end_loop = 7;
    else
        $end_loop = $no_of_paginations;
}
/* ----------------------------------------------------------------------------------------------------------- */
$msg .= "<ul class='pagination'><ul>";

// FOR ENABLING THE FIRST BUTTON
if ($first_btn && $cur_page > 1) {
    $msg .= "<li p='1' class='active'>First</li>";
} else if ($first_btn) {
    $msg .= "<li p='1' class='inactive'>First</li>";
}

// FOR ENABLING THE PREVIOUS BUTTON
if ($previous_btn && $cur_page > 1) {
    $pre = $cur_page - 1;
    $msg .= "<li p='$pre' class='active previous'>&laquo;</li>";
} else if ($previous_btn) {
    $msg .= "<li class='inactive'>&laquo;</li>";
}
for ($i = $start_loop; $i <= $end_loop; $i++) {

    if ($cur_page == $i)
        $msg .= "<li p='$i' class='active'>{$i}</li>";
    else
        $msg .= "<li p='$i' class='active'>{$i}</li>";
}

// TO ENABLE THE NEXT BUTTON
if ($next_btn && $cur_page < $no_of_paginations) {
    $nex = $cur_page + 1;
    $msg .= "<li p='$nex' class='active'>&raquo;</li>";
} else if ($next_btn) {
    $msg .= "<li class='inactive'>&raquo;</li>";
}

// TO ENABLE THE END BUTTON
if ($last_btn && $cur_page < $no_of_paginations) {
    $msg .= "<li p='$no_of_paginations' class='active'>Last</li>";
} else if ($last_btn) {
    $msg .= "<li p='$no_of_paginations' class='inactive'>Last</li>";
}
//$goto = "<input type='text' class='goto' size='1'/><input type='button' id='go_btn' class='go_button' value='Go'/>";
$total_string = "<span class='total' a='$no_of_paginations'>Page <b>" . $cur_page . "</b> of <b>$no_of_paginations</b></span>";
$msg = $msg . "</ul>"/* . $goto*/ . $total_string . "</div>";  // Content for pagination
echo $msg;
ob_end_flush();
}

1 个答案:

答案 0 :(得分:0)

您可以在函数中传递第二个属性,如: -

$('#container .pagination li.active').live('click',function(){
                    var page = $(this).attr('p');
                    loadData(page, category_id);
                });