查询结果和分页仅显示第一页

时间:2015-10-22 16:53:20

标签: search mysqli

我正在尝试构建一个仅用于教育目的的刮刀,我使用Phasher类生成十六进制哈希并存储在数据库中然后搜索存储的图像以查找类似的图像,我几天前写了一些东西试图显示结果为了搜索类似的图片但我无法弄清楚为什么它只显示第一页上的结果而其他页面没有显示它们当我按下第1页时它没有显示任何东西,但结果是正确的,生成的数量是正确的我是PHP新手,我试图通过任何帮助来学习它将提前感谢它。

这是index.php

<?php include('header.php'); ?>

<nav class="navbar navbar-default">
  <div class="container-fluid">
    <div class="navbar-header">
      <a class="navbar-brand" href="index.php">
        <img alt="FBpp logo" src="images/logo.png">
      </a>
    </div>
  </div>
</nav>

    <div class="container"><!--container-->

        <h3>Search Facebook Profiles Pictures For Similar Pictures.</h3>
        <p>Please upload a picture, Allowed extensions are (jpg, jpeg, pjpeg, png, x-png) and maximum size is 5 Mb...</p>

    <?php

    //Require config.php file to connect with mysql server and the db.
    require_once('config.php');

    //Check if the database is empty or if there are hashed pictures then show the number of hashed pictures.
    $check = mysqli_query($con, "SELECT id FROM images ORDER BY id DESC LIMIT 1;");
    if(mysqli_num_rows($check) > 0){

        $max_id = mysqli_fetch_row($check);

        $id = $max_id[0];

        echo 'We scraped '; echo '<span class="bg-info">'.$id.'</span>'; echo ' pictures...';
    }else{
        echo 'The database is empty you need to run scraper.php';
    }
    ?>

    <br /><br />
    <form action="search.php" method="post" class="form-inline reset-margin" enctype="multipart/form-data">
        <div class="form-group">
        <input type="file" name="image" class="file-input">
        <button type="submit" name="submit" class="btn btn-primary"><span class="glyphicon glyphicon-search" aria-hidden="true"></span></button>
    </div>
    </form>
    <br />

    <?php include('footer.php'); ?>

这是search.php

<?php

include('header.php');

//Require config.php file to connect with mysql server and the db.
require_once('config.php');

include_once('classes/phasher.class.php');
$I = PHasher::Instance();

require_once('classes/paginator.class.php');

$limit      = ( isset( $_GET['limit'] ) ) ? $_GET['limit'] : 10;
$page       = ( isset( $_GET['page'] ) ) ? $_GET['page'] : 1;
$links      = ( isset( $_GET['links'] ) ) ? $_GET['links'] : 7;


if(isset($_POST['submit'])){

    $allowedExts = array('jpg', 'jpeg', 'pjpeg', 'png', 'x-png');
    $temp = explode(".", $_FILES["image"]["name"]);
    $extension = end($temp);

    //Check if the extenstion of the uploaded picture is correct and the max size is 5*1024*1024 Megabits.
    if((($_FILES["image"]["type"] == "image/jpg")
        || ($_FILES["image"]["type"] == "image/jpeg")
        || ($_FILES["image"]["type"] == "image/pjpeg")
        || ($_FILES["image"]["type"] == "image/png")
        || ($_FILES["image"]["type"] == "image/x-png"))
        && ($_FILES["image"]["size"] <= 5242880)
        && in_array($extension, $allowedExts)){

        //Check if there is an error in the file, If not upload it to tmp folder then check db for similar pictures.
        if($_FILES["image"]["error"] > 0){
            echo "Return Code: " .$_FILES["image"]["error"]."<br />";
        } else {

            move_uploaded_file($_FILES["image"]["tmp_name"], dirname(__file__)."/tmp/".$_FILES["image"]["name"]);

            $uploadedImage = dirname(__file__)."/tmp/".$_FILES["image"]["name"];

            if($_FILES["image"]["size"] > 0){

                $hash = $I->FastHashImage($uploadedImage);
                $hex = $I->HashAsString($hash);

                $query = "SELECT `fid`,`hash` FROM `images` WHERE `hash` LIKE '%".$hex."%'";

                $queryResult = mysqli_query($con, $query);

                $numrows = mysqli_num_rows($queryResult);

                echo "<p>" .$numrows. " results found for " .$_FILES['image']['name']. "</p><br />";

                $Paginator  = new Paginator( $con, $query );
                $results    = $Paginator->getData( $limit, $page );

                //Loop through result set.
                /*while($row = mysqli_fetch_array($selectQuery)){

                    if($row['hash'] == $hex){

                        $fid = $row['fid'];
                        echo "<a href='https://www.facebook.com/$fid/' target='_blank'><img src='http://localhost/fbpp/test_pics/$fid.jpg' alt='' class='img-responsive'></a><br />";
                        // echo "<a href='https://www.facebook.com/$fid/' target='_blank'><img src='https://graph.facebook.com/$fid/picture?type=large' alt='' class='img-responsive'></a><br />";
                    }
                }*/



                echo '<div class="col-md-10 col-md-offset-1">
                <table class="table table-striped table-condensed table-bordered table-rounded"><tbody>';

                for( $i = 0; $i < count( $results->data ); $i++ ){

                    if($results->data[$i]["hash"] == $hex){

                        echo '<tr>';
                        $fid = $results->data[$i]['fid'];
                        echo "<td><a href='https://www.facebook.com/$fid/' target='_blank'><img src='http://localhost/fbpp/test_pics/$fid.jpg' alt='' class='img-responsive'></a></td>";
                        // echo "<td><a href='https://www.facebook.com/$fid/' target='_blank'><img src='https://graph.facebook.com/$fid/picture?type=large' alt='' class='img-responsive'></a></td>";
                        echo '</tr>';
                    }
                }

                if($numrows <= 10)
                {
                    echo "";
                } else {
                    echo '</tbody></table>';
                    echo $Paginator->createLinks( $links, 'pagination pagination-sm' );
                    echo '</div>';
                }



        }
        //Else after checking the file size.
        else {
                echo "Picture is corrupted the size is 0";
            }

        } //Else after error check.
    }

    // This else after checking the picture extenstion and max size.
    else {
            echo "<p>Please Upload A Picture, Max. size is 5 Mb.</p>";
        }
}

include('footer.php');

?>

如果您想查看它,这是分页类:

<?php

class Paginator {

private $_conn;
private $_limit;
private $_page;
private $_query;
private $_total;

public function __construct( $conn, $query ) {

    $this->_conn = $conn;
    $this->_query = $query;

    $rs= $this->_conn->query( $this->_query );
    $this->_total = $rs->num_rows;

}

public function getData( $limit = 10, $page = 1 ) {

    $this->_limit   = $limit;
    $this->_page    = $page;

    if ( $this->_limit == 'all' ) {
        $query      = $this->_query;
    } else {
        $query      = $this->_query . " LIMIT " . ( ( $this->_page - 1 ) * $this->_limit ) . ", $this->_limit";
    }

    $rs             = $this->_conn->query( $query );

    while ( $row = $rs->fetch_assoc() ) {
        $results[]  = $row;
    }

    $result         = new stdClass();
    $result->page   = $this->_page;
    $result->limit  = $this->_limit;
    $result->total  = $this->_total;
    $result->data   = $results;

    return $result;
}

public function createLinks( $links, $list_class ) {
    if ( $this->_limit == 'all' ) {
        return '';
    }

    $last       = ceil( $this->_total / $this->_limit );

    $start      = ( ( $this->_page - $links ) > 0 ) ? $this->_page - $links : 1;
    $end        = ( ( $this->_page + $links ) < $last ) ? $this->_page + $links : $last;

    $html       = '<ul class="' . $list_class . '">';

    $class      = ( $this->_page == 1 ) ? "disabled" : "";
    $html       .= '<li class="' . $class . '"><a href="?limit=' . $this->_limit . '&page=' . ( $this->_page - 1 ) . '">&laquo;</a></li>';

    if ( $start > 1 ) {
        $html   .= '<li><a href="?limit=' . $this->_limit . '&page=1">1</a></li>';
        $html   .= '<li class="disabled"><span>...</span></li>';
    }

    for ( $i = $start ; $i <= $end; $i++ ) {
        $class  = ( $this->_page == $i ) ? "active" : "";
        $html   .= '<li class="' . $class . '"><a href="?limit=' . $this->_limit . '&page=' . $i . '">' . $i . '</a></li>';
    }

    if ( $end < $last ) {
        $html   .= '<li class="disabled"><span>...</span></li>';
        $html   .= '<li><a href="?limit=' . $this->_limit . '&page=' . $last . '">' . $last . '</a></li>';
    }

    $class      = ( $this->_page == $last ) ? "disabled" : "";
    $html       .= '<li class="' . $class . '"><a href="?limit=' . $this->_limit . '&page=' . ( $this->_page + 1 ) . '">&raquo;</a></li>';

    $html       .= '</ul>';

    return $html;
}

}

有关此内容的更多信息,您可以查看我的github帐户上的最新提交:

github.com/jadolyo/FBpp

先谢谢,我感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

有很多思考阅读和调试我不得不在OOP中从头开始编写代码,我发现我需要使用会话,因为当我按任何页面时图像路径的值为空或未完成如果有人在寻找同样问题的答案,那么这里的分页器就是完整的代码......

<?php

session_start();

class Search{

    function __construct()
    {

    }

    /**
    * Upload posted image from index.php to tmp dir
    * @return string
    */
    function uploadImage()
    {

        if(isset($_POST['submit']))
        {
        move_uploaded_file($_FILES['image']['tmp_name'], dirname(__file__).'/tmp/'.$_FILES['image']['name']);

        $uploadedImage = dirname(__file__).'/tmp/'.$_FILES['image']['name'];

        $_SESSION['image'] = $uploadedImage;
    }
        return $_SESSION['image'];
    }

    function imageHashing()
    {
        include_once('classes/phasher.class.php');
        $I = PHasher::Instance();

        $hash = $I->FastHashImage(Search::uploadImage());
        $hex = $I->HashAsString($hash);

        $query = "SELECT `fid`,`hash` FROM `images` WHERE `hash` LIKE '%".$hex."%'";

        //echo $query;
        return $query;
    }

    function imageResults()
    {
        require_once('config.php');

        require_once('classes/paginator.class.php');

        $limit      = ( isset( $_GET['limit'] ) ) ? $_GET['limit'] : 10;
        $page       = ( isset( $_GET['page'] ) ) ? $_GET['page'] : 1;
        $links      = ( isset( $_GET['links'] ) ) ? $_GET['links'] : 7;


        $queryResults = mysqli_query($con, Search::imageHashing());

        $numrows = mysqli_num_rows($queryResults);

        echo "<p>" .$numrows. " results found.</p><br />";

        $Paginator  = new Paginator( $con, Search::imageHashing() );
        $results    = $Paginator->getData( $limit, $page );

        for( $i = 0; $i < count( $results->data ); $i++ ){
                echo '<tr>';
                $fid = $results->data[$i]['fid'];
                echo '<td>';
                echo "<a href='https://www.facebook.com/$fid/' target='_blank'>https://www.facebook.com/$fid/</a>";
                echo "<a href='https://www.facebook.com/$fid/' target='_blank'><img src='https://graph.facebook.com/$fid/picture?type=large' alt='' class='img-responsive'></a>";
                $name = 'https://graph.facebook.com/'.$fid.'?fields=name&access_token=748352698603001|94fc98094ca42f974879c56f3229c5e4';
                $response = file_get_contents($name);
                $user = json_decode($response,true);
                echo $user['name'];
                echo '</td>';
                echo '</tr>';
            }
            if($numrows <= 10){
                echo "";
            } else {
                echo '</tbody></table>';
                echo $Paginator->createLinks( $links, 'pagination pagination-sm' );
                echo '</div>';
            }
    }
}

//Search::uploadImage();
//Search::imageHashing();
Search::imageResults();
?>