分页和搜索引擎

时间:2013-10-15 21:22:43

标签: php html mysql pagination

我使用分页脚本查看搜索结果, 但是当我点击下一页时打破了结果,因为没有在另一页上传递变量。

我在页面“index.php”中创建搜索表单。 和“search.php”查看结果。

请修改我的代码!!! 非常感谢

(抱歉坏的坏英语(我不使用翻译))

“的index.php”

<div id="main" style="width: 1024px; margin: 0 auto; background: #eee;border: solid 1px #000">

    <img  style="margin:30px 300px;"  alt="logoAifa" src="https://www.agenziafarmaco.gov.it/areariservata/sites/all/themes/genesis/genesis_AIFA/img/logo.png"/>
    <a style="font-family: Times;font-size: 24px; font-weight: bold; margin: 30px 390px">Asset Management</a>
    <form style="margin: 30px 30px 30px 30px" method="get" action="php/search.php">
        <table>
            <tr>
                <td><input type="text" placeholder="Cerca" name="cerca"></td><td><input type="submit" value="go"></td>
                <td><a style="font-family: Times;font-size:14px;color: blue;text-decoration: underline;">Ricerca Avanzata</a></td>

            </tr>
        </table>


    </form>

    <div id="db">


        <?php
    include('php/conn.php');    
    session_start();
etc...

“的search.php”

    <?php
    include('conn.php');    
    session_start();
    $cerca = $_REQUEST['cerca'];
    $_SESSION['cerca'] = $cerca;






$cerca =  mysql_escape_string(stripslashes($cerca)); 



$tableName="asset";     
    $targetpage = "search.php";     
    $limit = 50; 

    $query = "SELECT COUNT(*) as num FROM $tableName WHERE Nome LIKE '%$cerca%' OR Cognome LIKE '%$cerca%' OR Utilizzo LIKE '%$cerca%' OR IDAIFA LIKE '%$cerca%' OR Tipologia LIKE '%$cerca%' OR Modello LIKE '%$cerca%' OR Stato LIKE '%$cerca%' ";
    $total_pages = mysql_fetch_array(mysql_query($query));
    $total_pages = $total_pages['num'];

    $stages = 3;

    $page = isset($_GET['page']) ? mysql_real_escape_string($_GET['page']) : 0;
    if($page){
        $start = ($page - 1) * $limit; 
    }else{
        $start = 0; 
        }   

    // Get page data
    $query1 = "SELECT * FROM $tableName WHERE Nome LIKE '%$cerca%' OR Cognome LIKE '%$cerca%' OR Utilizzo LIKE '%$cerca%' OR IDAIFA LIKE '%$cerca%' OR Tipologia LIKE '%$cerca%' OR Modello LIKE '%$cerca%' OR Stato LIKE '%$cerca%' LIMIT $start, $limit ";
    $result = mysql_query($query1);

    // Initial page num setup
    if ($page == 0){$page = 1;}
    $prev = $page - 1;  
    $next = $page + 1;                          
    $lastpage = ceil($total_pages/$limit);      
    $LastPagem1 = $lastpage - 1;                    


    $paginate = '';
    if($lastpage > 1)
    {   




        $paginate .= "<div class='paginate'>";
        // Previous
        if ($page > 1){
            $paginate.= "<a href='$targetpage?page=$prev'>previous</a>";
        }else{
            $paginate.= "<span class='disabled'>previous</span>";   }



        // Pages    
        if ($lastpage < 7 + ($stages * 2))
        {   
            for ($counter = 1; $counter <= $lastpage; $counter++)
            {
                if ($counter == $page){
                    $paginate.= "<span class='current'>$counter</span>";
                }else{
                    $paginate.= "<a href='$targetpage?page=$counter'>$counter</a>";}                    
            }
        }
        elseif($lastpage > 5 + ($stages * 2))
        {

            if($page < 1 + ($stages * 2))       
            {
                for ($counter = 1; $counter < 4 + ($stages * 2); $counter++)
                {
                    if ($counter == $page){
                        $paginate.= "<span class='current'>$counter</span>";
                    }else{
                        $paginate.= "<a href='$targetpage?page=$counter'>$counter</a>";}                    
                }
                $paginate.= "...";
                $paginate.= "<a href='$targetpage?page=$LastPagem1'>$LastPagem1</a>";
                $paginate.= "<a href='$targetpage?page=$lastpage'>$lastpage</a>";       
            }

            elseif($lastpage - ($stages * 2) > $page && $page > ($stages * 2))
            {
                $paginate.= "<a href='$targetpage?page=1'>1</a>";
                $paginate.= "<a href='$targetpage?page=2'>2</a>";
                $paginate.= "...";
                for ($counter = $page - $stages; $counter <= $page + $stages; $counter++)
                {
                    if ($counter == $page){
                        $paginate.= "<span class='current'>$counter</span>";
                    }else{
                        $paginate.= "<a href='$targetpage?page=$counter'>$counter</a>";}                    
                }
                $paginate.= "...";
                $paginate.= "<a href='$targetpage?page=$LastPagem1'>$LastPagem1</a>";
                $paginate.= "<a href='$targetpage?page=$lastpage'>$lastpage</a>";       
            }

            else
            {
                $paginate.= "<a href='$targetpage?page=1'>1</a>";
                $paginate.= "<a href='$targetpage?page=2'>2</a>";
                $paginate.= "...";
                for ($counter = $lastpage - (2 + ($stages * 2)); $counter <= $lastpage; $counter++)
                {
                    if ($counter == $page){
                        $paginate.= "<span class='current'>$counter</span>";
                    }else{
                        $paginate.= "<a href='$targetpage?page=$counter'>$counter</a>";}                    
                }
            }
        }

                // Next
        if ($page < $counter - 1){ 
            $paginate.= "<a href='$targetpage?page=$next'>next</a>";
        }else{
            $paginate.= "<span class='disabled'>next</span>";
            }

        $paginate.= "</div>";       


}
 echo $total_pages.' Results';
 // pagination

?>

<?php 
 echo "<table border='1' style='margin:0 auto;'>";

echo "<tr>",
"<td colspan='3'> Azioni </td> <td> Nome </td> <td> Cognome </td> <td> IDAIFA </td> <td> Utilizzo</td> <td> Tipologia</td> <td>Marca</td> <td>Modello</td> <td>Stato</td>",
"</tr> \n";


        while($dati = mysql_fetch_array($result))
        {

        echo "<tr>";
echo "<td> <a href='detailasset.php?ID=$dati[ID]'><img src='../images/eye.png'></a> </td>";
echo "<td> <a href='modasset.php?ID=$dati[ID]'><img src='../images/edit.png'></a> </a> </td>";
echo "<td> <a href='deleteasset.php?ID=$dati[ID]?confirm=true' class='confirm'> <img src='../images/delete.png'></a></td>";

print "<td> $dati[Nome] </td>";
print "<td> $dati[Cognome] </td>";
print "<td> $dati[IDAIFA] </td>";
print "<td> $dati[Utilizzo] </td>";
print "<td> $dati[Tipologia] </td>";
print "<td> $dati[Marca] </td>";
print "<td> $dati[Modello] </td>";
print "<td> $dati[Stato] </td>";

echo "</tr> \n";

        }

    echo "</table></div>\n";
     echo $paginate;
    ?>

</div>

1 个答案:

答案 0 :(得分:1)

我认为您要说的是搜索字词没有正确传递?

从我在代码中看到的内容,变量$cerca是搜索词。

您可以使用$_GET['cerca']代替$_REQUEST

另外更重要的是,我不认为你可以在查询中这样做:

$query = "SELECT COUNT(*) as num FROM $tableName WHERE Nome LIKE '%$cerca%'

您应该拆分LIKE部分,例如此

$query = "SELECT COUNT(*) as num FROM $tableName WHERE Nome LIKE '%" . $cerca . "%'

在你做LIKE

的所有不同时间都这样做

或者我认为这就是你所要求的......