paging php error - undefined index

Posted by fusion on Stack Overflow See other posts from Stack Overflow or by fusion
Published on 2010-05-05T20:22:54Z Indexed on 2010/05/05 20:28 UTC
Read the original article Hit count: 278

Filed under:
|
|
|

i've a search form with paging. when the user enters the keyword initially, it works fine and displays the required output; however, it also shows this error:

Notice: Undefined index: page in C:\Users\user\Documents\Projects\Pro\search.php on line 21 Call Stack: 0.0011 372344 1. {main}() C:\Users\user\Documents\Projects\Pro\search.php:0 

. .and if the user clicks on the 'next' page, it shows no output with this error thrown:

Notice: Undefined index: q in C:\Users\user\Documents\Projects\Pro\search.php on line 19 Call Stack: 0.0016 372048 1. {main}() C:\Users\user\Documents\Projects\Pro\search.php:0

this is my code:

<?php

ini_set('display_errors',1);
error_reporting(E_ALL|E_STRICT);

include 'config.php';


$search_result = "";

$search_result = trim($_GET["q"]);

$page= $_GET["page"]; //Get the page number to show
if($page == "") $page=1;

$search_result = mysql_real_escape_string($search_result);

//Check if the string is empty
if ($search_result == "") {
  echo  "<p class='error'>Search Error. Please Enter Your Search Query.</p>" ;
  exit();
      }

if ($search_result == "%" || $search_result == "_" || $search_result == "+" ) {
  echo  "<p class='error1'>Search Error. Please Enter a Valid Search Query.</p>" ;
  exit();
      }

if(!empty($search_result))
{



  // the table to search
  $table = "thquotes";
  // explode search words into an array
  $arraySearch = explode(" ", $search_result);
  // table fields to search
  $arrayFields = array(0 => "cQuotes");
  $countSearch = count($arraySearch);
  $a = 0;
  $b = 0;
  $query = "SELECT cQuotes, vAuthor, cArabic, vReference FROM ".$table." WHERE (";
  $countFields = count($arrayFields);
  while ($a < $countFields)
  {
    while ($b < $countSearch)
    {
      $query = $query."$arrayFields[$a] LIKE '%$arraySearch[$b]%'";
      $b++;
      if ($b < $countSearch)
      {
        $query = $query." AND ";
      }
    }
    $b = 0;
    $a++;
    if ($a < $countFields)
    {
      $query = $query.") OR (";
    }
  }
  $query = $query.")";

     $result = mysql_query($query, $conn)
      or die ('Error: '.mysql_error());

     $totalrows = mysql_num_rows($result);

     if($totalrows < 1)
        {
             echo '<span class="error2">No matches found for "'.$search_result.'"</span>';
         }
        else
        {

            $limit = 3; //Number of results per page
            $numpages=ceil($totalrows/$limit);

            $query = $query." ORDER BY idQuotes LIMIT " . ($page-1)*$limit . ",$limit";
            $result = mysql_query($query, $conn)
             or die('Error:' .mysql_error());
?>

<div class="caption">Search Results</div>
<div class="center_div">
<table>
    <?php while ($row= mysql_fetch_array($result, MYSQL_ASSOC)) {
        $cQuote =  highlightWords(htmlspecialchars($row['cQuotes']), $search_result);
        ?>
        <tr>
        <td style="text-align:right; font-size:15px;"><?php h($row['cArabic']); ?></td>
            <td style="font-size:16px;"><?php echo $cQuote; ?></td>
            <td style="font-size:12px;"><?php h($row['vAuthor']); ?></td>
            <td style="font-size:12px; font-style:italic; text-align:right;"><?php h($row['vReference']); ?></td>
        </tr>
    <?php } ?>
</table>
</div>

<?php
   //Create and print the Navigation bar
       $nav="";
       if($page > 1) {
            $nav .= "<a href=\"search.php?page=" . ($page-1) . "&string=" .urlencode($search_result) . "\"><< Prev</a>";
        }

       for($i = 1 ; $i <= $numpages ; $i++) {
            if($i == $page) {
                $nav .= "<B>$i</B>";
            }else{
                $nav .= "<a href=\"search.php?page=" . $i . "&string=" .urlencode($search_result) . "\">$i</a>";
            }
        }

        if($page < $numpages) {
            $nav .= "<a href=\"search.php?page=" . ($page+1) . "&searchstring=" .urlencode($search_result) . "\">Next >></a>";
        }

        echo "<br /><br />" . $nav;


    }
    }
    else {
        exit();
    } 

?>

© Stack Overflow or respective owner

Related posts about php

Related posts about paging