我试图从我的mysql调用问题,并在php中的ajax分页中显示它们

时间:2016-12-02 08:02:48

标签: php ajax pagination

我正在使用ajax从我的mysql表中调用问题以分页形式显示它们但我无法根据主题和类别调用它们并以分页形式显示它们。 这是我的代码。

        <?php
    print_r($_GET);
    //echo $_GET['val12'];
    $X=$_GET['val12'];
    $category=$_GET['catg12'];
    //print_r($_GET);

    ?>

    <?php
    //session_start();
    function __autoload($classname)
    {
    include "admin_panel/$classname.php";
    }
    $obj = new connect();
    $st=$obj->con();

    $user= new questionclass();
    $results=$user->getQuestions($_POST);
    //print_r($_POST);
    extract($_POST);

    //$_SESSION['category']=$category;

    //$key_id=$_SESSION['key'];

    $qry = "select * from categories where category_name='$category'";
    $run =mysqli_query($st,$qry);
    $row = mysqli_fetch_assoc($run);

    $testname=$row['test_name'];
    $subject_name=explode(',', $testname);


    ?>
    <div class="loading-div"></div>
    <div id="results"><!-- content will be loaded here --></div>
    <?php

    { //include config file
        //Get page number from Ajax POST
        if(isset($_POST["pagenum"])){
            $page_number = filter_var($_POST["pagenum"], 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=3;   
        //get total number of records from database for pagination
        $results = $st->query("SELECT COUNT(*) FROM question");
        $get_total_rows = $results->fetch_row(); //hold total records in variable
        //break records into pages
        $total_pages = ceil($get_total_rows[0]/$item_per_page);

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

    foreach($subject_name as $values ) {

        $values=trim($values);

    }




        //Limit our results within a specified range. 
        $sel = $st->prepare("SELECT quesname, answer1,answer2,answer3,answer4,correctanswer FROM question where category_name ='$category' && subject_name ='$X' ORDER BY id ASC LIMIT $page_position, $item_per_page");
        $sel->bind_result($quesname,$answer1,$answer2,$answer3,$answer4,$correctanswer); //bind variables to prepared statement

        $sel->execute(); //Execute prepared Query

    //$qrey=mysqli_query($st,$sel);
    //print_r($sel);
    $x=1;

    while($rowin=mysqli_fetch_assoc($sel)){


        //Display records fetched from database.
        echo '<ul class="contents">';
        while($sel->fetch()){ //fetch values
    ?>
    <div class='cont' id="<?php echo $x;  ?>">
    <?php
     echo "<div  id='question_splitter_$x'>";?>
                <div id='subject_name<?php echo $x;?>' >
                                <p class='questions' id="qname<?php echo $x;?>"> <?php echo $x?>.<?php echo $rowin['quesname'];?></p>
                                <input type="radio" value="1" id='radio1_<?php echo $rowin['id'];?>' name='<?php echo $rowin['id'];?>'/><?php echo $rowin['answer1'];?>
                                <br/>
                                <input type="radio" value="2" id='radio1_<?php echo $rowin['id'];?>' name='<?php echo $rowin['id'];?>'/><?php echo $rowin['answer2'];?>
                                <br/>


                                <input type="radio" value="3" id='radio1_<?php echo $rowin['id'];?>' name='<?php echo $rowin['id'];?>'/><?php echo $rowin['answer3'];?>
                                <br/>

                                <input type="radio" value="4" id='radio1_<?php echo $rowin['id'];?>' name='<?php echo $rowin['id'];?>'/><?php echo $rowin['answer4'];?>
                                <br/>


                                <input type="radio" checked='checked' style='display:none' value="smart_quiz" id='radio1_<?php echo $rowin['id'];?>' name='<?php echo $rowin['id'];?>'/>
                                <br/>
                                </div>
    </div>

    <?php



            echo '<li>';
            echo  $quesname. '. <strong>' .$answer1.'</strong> &mdash; '. '. <strong>' .$answer2.'</strong> &mdash; '. '. <strong>' .$answer3.'</strong> &mdash; '. '. <strong>' .$answer4.'</strong> &mdash; '.$correctanswer;
            echo '</li>';
        }
        echo '</ul>';

        echo '<div align="center">';
        /* We call the pagination function here to generate Pagination link for us. 
        As you can see I have passed several parameters to the function. */
        echo paginate_function($item_per_page, $page_number, $get_total_rows[0], $total_pages);
        echo '</div>';

        exit;
    }
    }
    ################ pagination function #########################################
    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">';
            $next           = $current_page + 1; //next link
            $right_links    = $current_page + 3; 
            $previous       = $current_page - 3; //previous 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
    }

    ?>

before using pagination i was using this code to call all the question

$sel="select * from question where category_name='$category' && subject_name='$X'";
$qrey=mysqli_query($st,$sel);
print_r($sel);
$x=1;

这是从页面获取类别和主题的查询。

0 个答案:

没有答案