用分页php mysql错误搜索

时间:2013-11-27 00:39:34

标签: javascript php mysql pagination

这是我的HTML

<form name="search" method="GET" action="select1WSearch.php" target="iframe_a" onsubmit="changeLink('select1.php')">
Seach for: <input type="text" name="find" /> in 
<Select NAME="field">
<Option VALUE="no">Number</option>
<Option VALUE="CPUname">CPU Name</option>
</Select>
<input type="hidden" name="searching" value="yes" />
<input type="submit" name="search" value="Search" />
</form>

我的php很遗憾

<?php
include('conn.php');
$skip;
 $per_page = 10;
 $num="";
 $json="";
 //$searching = $_GET['searching'];
 $field = $_GET['field'];
 //If they did not enter a search term we give them an error 
 $find = $_GET['find'];
 if ($find == "") 
 { 
 echo "<p>You forgot to enter a search term"; 
 exit; 

 } 
baba: 
 //Now we search for our search term, in the field the user specified 
$data = mysql_query("SELECT * FROM tablepcname WHERE upper($field) LIKE'%$find%'"); 
$skip=1;
 //This counts the number or results - and if there wasn't any it gives them a little message explaining that 
 $anymatches=mysql_num_rows($data); 
 if ($anymatches == 0) 
 { 
 echo "Sorry, but we can not find an entry to match your query<br><br>"; 
 } 

 //And we remind them what they searched for 
 echo "<b>Searched For:</b> " .$find; 
 echo "<table border='1' width='280'>
</br>
<tr>
<th> <img src='Addicon.png' width='17' alt='Add' title='ADD DATA' onClick='swipe();' style='cursor: pointer;'/;</th>
<th></th>
<th></th>
<th><font size='3'>No.</font></th>
<th><font size='3'>CPU Name<font></th>

</tr>";
 $result = mysql_query("SELECT * FROM tablepcname WHERE upper($field) LIKE'%$find%'"); 
//$result=mysql_query($data);
  $total_results = mysql_num_rows($result);
  $total_pages = ceil($total_results / $per_page);
      // check if the 'page' variable is set in the URL (ex: select1new.php?page=1)
        if (isset($_GET['page']) && is_numeric($_GET['page']))
        {
                $show_page = $_GET['page'];

                // make sure the $show_page value is valid
                if ($show_page > 0 && $show_page <= $total_pages)
                {
                        $start = ($show_page -1) * $per_page;
                        $end = $start + $per_page; 
                        var_dump ($start);

                }
                else
                {
                        // error - show first set of results
                        $start = 0;
                        $end = $per_page; 


                }           
        }
        else
        {
                // if page isn't set, show first set of results
                $start = 0;
                $end = $per_page; 
        }
        // loop through results of database query, displaying them in the table 
        for ($i = $start; $i < $end; $i++)
        {
                // make sure that PHP doesn't try to show results that don't exist
                if ($i == $total_results) { break; }
       // echo out the contents of each row into a table
            echo "<tr>";
echo ("<td>
    <img src='view.jpg' width='14' alt='View' title='VIEW DATA' 
    onClick=\"swipe4('". mysql_result($result, $i, 'no') ."');\"
   style='cursor:pointer;'>
</td>");    
echo ("<td>
    <img src='edit.jpg' width='14' alt='Edit' title='EDIT DATA' 
    onClick=\"swipe2('". mysql_result($result, $i, 'no') ."');\"
   style='cursor:pointer;'>
</td>");
echo ("<td>
    <img src='delete.jpg' width='14' alt='Delete' title='DELETE DATA' 
    onClick=\"swipe3('". mysql_result($result, $i, 'no') ."');\"
   style='cursor:pointer;'>
</td>");    

             echo strtoupper('<td> <font size=3>' . mysql_result($result, $i, 'no') . '</td>');
             echo strtoupper('<td> <font size=3>' . mysql_result($result, $i, 'CPUname') . '</td>');

            }    
                echo "</tr>";   

        echo "</table>"; 

//Pagination Starts
echo "<center>";

$prev = $start - $per_page;
$next = $start + $per_page;

$adjacents = 3;
$last = $total_pages - 1;

if($total_pages > 1)
{  
//previous button
if (!($start<=0))
echo " <a href='select1WSearch.php?page=$prev'>Prev</a> ";   

//pages
if ($total_pages < 7 + ($adjacents * 2))   //not enough pages to bother breaking it up
{
$i = 0;  
for ($counter = 1; $counter <= $total_pages; $counter++)
{
if ($i == $start){
echo " <a href='select1WSearch.php?page=$i'><b>$counter</b></a> ";
}
else {
echo " <a href='select1WSearch.php?page=$i'>$counter</a> ";
} 
$i = $i + $per_page;                
}
}
elseif($total_pages > 5 + ($adjacents * 2))    //enough pages to hide some
{
//close to beginning; only hide later pages
if(($start/$per_page) < 1 + ($adjacents * 2))       
{
$i = 0;
for ($counter = 1; $counter < 4 + ($adjacents * 2); $counter++)
{
if ($i == $start){
echo " <a href='select1WSearch.php?page=$i'><b>$counter</b></a> ";

}
else {
echo " <a href='select1WSearch.php?page=$i'>$counter</a> ";
}
$i = $i + $per_page;                                      
}

}
//in middle; hide some front and some back
elseif($total_pages - ($adjacents * 2) > ($start / $per_page) && ($start / $per_page) > ($adjacents * 2))
{
echo " <a href='select1WSearch.php?page=0'>1</a> ";
echo " <a href='select1WSearch.php?page=$per_page'>2</a> .... ";

$i = $start;                
for ($counter = ($start/$per_page)+1; $counter < ($start / $per_page) + $adjacents + 2; $counter++)
{
if ($i == $start){
echo " <a href='select1WSearch.php?page=$i'><b>$counter</b></a> ";
}
else {
echo " <a href='select1WSearch.php?page=$i'>$counter</a> ";
}  
$i = $i + $per_page;               
}

}
//close to end; only hide early pages
else
{
echo " <a href='select1WSearch.php?page=0'>1</a> ";
echo " <a href='select1WSearch.php?page=$per_page'>2</a> .... ";

$i = $start;               
for ($counter = ($start / $per_page) + 1; $counter <= $total_pages; $counter++)
{
if ($i == $start){
echo " <a href='select1WSearch.php?page=$i'><b>$counter</b></a> ";
}
else {
echo " <a href='select1WSearch.php?page=$i'>$counter</a> ";  
}
$i = $i + $per_page;             
}
}
}

//next button
if (!($start >=$total_results-$per_page))
echo " <a href='select1WSearch.php?page=$next'>Next</a> ";   
}  
echo "</center>";
?>

搜索没问题,当结果超过10时显示分页页面。问题是当我点击分页的“数字如1 2 3 4,上一页和下一页”时,它没有指向点击的分页页面,这意味着它不会更改页面和错误显示像未定义的值我“$_GET”我认为我也有链接a href='select1WSearch.php?=$i的问题,但我也改变了,我遇到了错误。请帮助,如果你建议新的代码生病尝试。提前谢谢^^

0 个答案:

没有答案