分页未定义索引

时间:2015-08-16 05:54:22

标签: php pagination

您好我在这里有一个代码,它将搜索一个名称然后返回结果分页。我在第19行和第20行出错了

  

注意:未定义的索引:在第19行的C:\ xampp \ htdocs \ eventreg \ trypaginate2.php中提交 - 现在确定

  

注意:未定义的索引:在第20行的C:\ xampp \ htdocs \ eventreg \ trypaginate2.php中搜索 - 现在确定

但它正在发挥作用。当我转到下一页时,它不会显示任何结果只是一个空白页。

这是我的HTML代码:

            <html>
            <head>
            <title>Title of your search engine</title>
            </head>
            <body>
            <form action='trypaginate2.php' method='POST'>
            <center>
            <h1>My Search Engine</h1>
            <input type='text' size='90' name='search'></br></br>
            <input type='submit' name='submit' value='Search source code' ></br></br></br>
            </center>
            </form>
            </body>
            </html>

然后php代码在这里:

                            <?php

                if(array_key_exists('search', $_GET))
                {
                    echo "hello";
                    $search = $_GET['search'];
                    echo "$search";
                }
                  if(isset($_POST['submit']))
                  {
                if(array_key_exists('search', $_POST))
                {
                    echo "hi";
                    $search = $_POST['search'];
                }
                  }


            if(strlen($search)<=1)
            echo "Search term too short";

            else
                {
            echo "You searched for <b>$search</b> <hr size='1'></br>";

            include("dbcon.php");

            $search_exploded = explode (" ", $search);

            $x = "";
            $construct = "";  

            foreach($search_exploded as $search_each)
                    {
                        $x++;
                        if($x==1)
                        $construct .="name LIKE '%$search_each%'";
                        else
                        $construct .="AND name LIKE '%$search_each%'";

                    }
             echo $construct;
            $constructs ="SELECT * FROM reginformation WHERE $construct";
            $run = mysql_query($constructs);

            $foundnum = mysql_num_rows($run);

            if ($foundnum==0)
            echo "Sorry, there are no matching result for <b>$search</b>.</br></br>1. 
            Try more general words. for example: If you want to search 'how to create a website'
            then use general keyword like 'create' 'website'</br>2. Try different words with similar
             meaning</br>3. Please check your spelling";
            else
                    { 

            echo "$foundnum results found !<p>";

            $per_page = 5;
            $start = isset($_GET['start']) ? $_GET['start']: '';
            $max_pages = ceil($foundnum / $per_page);
            if(!$start)
            $start=0; 
            $getquery = mysql_query("SELECT * FROM reginformation WHERE $construct LIMIT $start, $per_page");

            /*while($runrows = mysql_fetch_assoc($getquery))
                        {
                            $title = $runrows ['regID'];
                            $desc = $runrows ['name'];
                            $url = $runrows ['address'];

                            echo "
                            <a href='$url'><b>$title</b></a><br>
                            $desc<br>
                            <a href='$url'>$url</a><p>
                            ";
                        }*/

              if(mysql_num_rows($getquery)>0)
                        {
                    while ($row = mysql_fetch_assoc($getquery)) {
                    // echo data
                    echo "<tr onClick =window.location='infodetailsresp.php?id=$row[regID]'><td>$row[name]</td><td>$row[emailadd]</td><td>$row[contactno]</td><td>$row[event]</td><td>$row[date_register]</td></tr><br>";

                    } // end while
                        }
                    else
                        {
                            echo ("<SCRIPT LANGUAGE='JavaScript'>
                                window.alert('No record found.');
                                window.location.href='reglistresp.php';
                            </SCRIPT>");
                        }

            //Pagination Starts
            echo "<center>";

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

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

            if($max_pages > 1)
                        {   
            //previous button
            if (!($start<=0)) 
            echo " <a href='trypaginate2.php?search=$search&submit=Search+source+code&start=$prev'>Prev</a> ";    

            //pages 
            if ($max_pages < 7 + ($adjacents * 2))   //not enough pages to bother breaking it up
                            {
            $i = 0;   
            for ($counter = 1; $counter <= $max_pages; $counter++)
                                {
            if ($i == $start)
                {
                    echo " <a href='trypaginate2.php?search=$search&submit=Search+source+code&start=$i'><b>$counter</b></a> ";
                }
            else 
                {
                    echo " <a href='trypaginate2.php?search=$search&submit=Search+source+code&start=$i'>$counter</a> ";
                }  
            $i = $i + $per_page;                 
                                }
                            }
            elseif($max_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='trypaginate2.php?search=$search&submit=Search+source+code&start=$i'><b>$counter</b></a> ";
                }
            else 
                {
                    echo " <a href='trypaginate2.php?search=$search&submit=Search+source+code&start=$i'>$counter</a> ";
                } 
            $i = $i + $per_page;                                       
                        }

                    }
            //in middle; hide some front and some back
            elseif($max_pages - ($adjacents * 2) > ($start / $per_page) && ($start / $per_page) > ($adjacents * 2))
                {
                    echo " <a href='trypaginate2.php?search=$search&submit=Search+source+code&start=0'>1</a> ";
                    echo " <a href='trypaginate2.php?search=$search&submit=Search+source+code&start=$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='trypaginate2.php?search=$search&submit=Search+source+code&start=$i'><b>$counter</b></a> ";
                        }
                        else 
                        {
                            echo " <a href='trypaginate2.php?search=$search&submit=Search+source+code&start=$i'>$counter</a> ";
                        }   
                        $i = $i + $per_page;                
                    }

                }
            //close to end; only hide early pages
            else
            {
                echo " <a href='trypaginate2.php?search=$search&submit=Search+source+code&start=0'>1</a> ";
                echo " <a href='trypaginate2.php?search=$search&submit=Search+source+code&start=$per_page'>2</a> .... ";

                $i = $start;                
                for ($counter = ($start / $per_page) + 1; $counter <= $max_pages; $counter++)
                {
                    if ($i == $start)
                    {
                        echo " <a href='trypaginate2.php?search=$search&submit=Search+source+code&start=$i'><b>$counter</b></a> ";
                    }
                    else 
                    {
                        echo " <a href='trypaginate2.php?search=$search&submit=Search+source+code&start=$i'>$counter</a> ";   
                    } 
                    $i = $i + $per_page;              
                }
            }
                }

            //next button
            if (!($start >=$foundnum-$per_page))
            echo " <a href='trypaginate2.php?search=$search&submit=Search+source+code&start=$next'>Next</a> ";    
                        }   
            echo "</center>";
                    } 
                } 

            ?>

编辑!现在更改了我的PHP代码我在第34行收到错误。错误是注意:未定义的变量:在C:\ xampp \ htdocs中搜索第34行的\ eventreg \ trypaginate2.php

1 个答案:

答案 0 :(得分:0)

你不应该通过盲目地将变量分配给这些值甚至使用array_key_exists('submit', $_GET)来检查数组中是否存在某个键(这是$ _POST和$ _GET是什么) - 即使我知道这似乎喜欢它会有意义。这就是PHP向您发送警告的行为。该错误来自于尝试查找诸如$ _POST [&#39; search&#39;]或$ _GET [&#39; submit&#39;]之类的值,当这些值未在该数组中设置时。

而是使用array_key_exists('search', $_POST)GetAsyncKeyState来检查是否设置了这些值。如果该表达式的计算结果为true,那么您可以继续访问代码中的这些值,并且不应在日志中收到任何通知。