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
<br> Region: $region </td> </tr>";
}
}
}
echo "</tbody>";