views:

155

answers:

2

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();
    } 

?>
+1  A: 

you're checking for GET variable incorrectly.

change

$page= $_GET["page"];

to either

if(isset($_GET['page']))
    $page = $_GET['page'];
else
    $page = 1;

or a shorter version:

$page = isset($_GET['page']) ? $_GET['page'] : $page;

Also this:

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

to this: $search_result = isset($_GET['q']) ? trim($_GET['q']) : '';

Andrew M
Some said that Perl died because of lack of readability...
Col. Shrapnel
thanks this works, but now when the user goes to the second page, it gives a search error `Search Error. Please Enter Your Search Query.`
fuz3d
+1  A: 

PHP reminds you that every variable you use must be defined first.

Or, if it coming from outside, checked for existence.

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

$page=1;
if (isset($_GET["page"])) $page= $_GET["page"]; //Get the page number to show
Col. Shrapnel
please see my comment to @Andrew
fuz3d
@fusion as far as I can see, you give your search string variable a different names, `searchstring` or `string` instead of `q`, which seems to be used, dunno why
Col. Shrapnel
thanks Col. Shrapnel, that works, but why is the next page losing its formatting?
fuz3d
i checked the source of the page, it seems it is removing the css and javascript file after the 'next' button is clicked. do you know why?
fuz3d
@fusion nothing being "removed". Everything you see in the page source being printed by your code. If you don't see some HTML, so, it is absent in your code.
Col. Shrapnel
ah, thanks. i've added the css to my code.
fuz3d