0

Below is a portion my code. I have created a text box and when the user enters a word/ phrase in the text box it searches the keyword in the database. This code also contains the code for pagination. The problem that I am facing in this code is that first time it displays the page number but at the first instance it doesn't displays the desired result. When I move from one page to another it loses the value from the variable "$each" and displays all results from the database. Whereas this code is supposed to display only the part of data which meets the criteria of the query

    <input type="text" name="k" 
        value="<?php if(isset($_GET['k'])){echo htmlentities($_GET['k']);} ?>" style="border: 1px, thin; width:92%; "/> 
    <input type="image" style="margin-bottom: 0; margin-top: 2px;" src="search.png"  value="submit" />
    </div>
    </fieldset>
</form>
</div>
<table cellpadding="0" cellspacing="0" border="1">
<tbody>    
<?php
$connection = mysql_connect('', '', '');
if(!$connection)
    echo "No database connected";
$dbase = mysql_select_db("", $connection);
if(!$dbase)
    echo "No datatable connected";

if(isset($_GET['k'])){ 
    $k1 = $_GET['k']; 
} else { 
    $k1 = ''; 
}

echo $k1;

$term = explode(" ", $k1);

$query = "SELECT * FROM kcpdatabase ";

foreach ($term as $each) {
   echo $each;
    $i++;
   if($i==1) {
        $query .= "WHERE keywords LIKE '%$each%' ";
   } else {
        $query .= "OR WHERE keywords LIKE '%$each%' ";
    }
}

echo $query;

$per_pages=3;
$page_query = mysql_query("SELECT COUNT('title') FROM kcpdatabase");

$pages = ceil(mysql_result($page_query, 0)/$per_pages)  or die($page_query."<br/><br/>".mysql_error()); //Calculating the number of pages and round it to higher side

$page = (isset($_GET['page'])) ? (int) ($_GET['page']) : 1; //Checking wheather value of page is set or not. 
                                                         //If not then assign it as a default value of 1.
$start = ($page - 1) * $per_pages; // Point to the record zero always to start displaying the data

$query .= "LIMIT $start, $per_pages";

$ourquery1 = mysql_query ($query);
if(!$ourquery1)
    echo "No query found";

    $row1 = mysql_num_rows ($ourquery1);

if($pages >= 1 && $page <= $pages){ 
   for($x = 1; $x <= $pages; $x++) {
        echo '<a href="?page='.$x.'">'.$x.'</a> ';
   }

    if ($row1 > 0) {

        while($result = mysql_fetch_assoc($ourquery1)) {
         echo "<tr>";
            echo "<td>";
            $title = $result['title'];
            $link = $result['link'];
            $region = $result['region'];
            $sector = $result['sector'];
            $theme = $result['theme'];      
            echo "<td> <a href=$link><h3>$title<h3></a>";
            echo "<h4>Sector: $sector Theme: $theme &nbsp; 
            <br> Region: $region </td> </tr>";
        }
    }   
}
echo "</tbody>";
TNK
  • 4,263
  • 15
  • 58
  • 81

2 Answers2

0

Please, don't use mysql_* functions in new code. They are no longer maintained and are officially deprecated. See the red box? Learn about prepared statements instead, and use PDO or MySQLi - this article will help you decide which. If you choose PDO, here is a good tutorial.

Community
  • 1
  • 1
TNK
  • 4,263
  • 15
  • 58
  • 81
  • Although this is great information, this should be a comment, not an answer, as it does nothing to help solve the OP's question. – Sean Mar 04 '13 at 03:00
0

You need to add your search criteria to your pagination links (k=$k1), as it is currently only being set when you submit the form. Try something like -

if($pages >= 1 && $page <= $pages){ 
   for($x = 1; $x <= $pages; $x++) {
        echo '<a href="?k='.$k1.'&page='.$x.'">'.$x.'</a> ';
   }

note- you are currently using unsanitized user input in $k1 => $each.

Sean
  • 12,443
  • 3
  • 29
  • 47