0

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

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

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

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

“索引.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...

“搜索.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>
4

1 回答 1

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

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

于 2013-10-15T21:29:19.720 回答