I have a search code that uses pagination to display results but the pagination is not displaying or working. I want such that 7 results are displayed per page view but cant seem to get it to work.
Kindly help me point out the issue and i need to understand why the pagination isn't working even though the search query is.
Below are my codes:
if (isset($_GET["mainSearch"]))
{
$condition = '';
$mainSearch = SQLite3::escapeString($_GET['mainSearch']);
$searchquery = $_GET['mainSearch'];
$query = explode(" ", $searchquery);
$perpageview = 7; // amount of results to display per page
if ($_GET["pageno"])
{
$page = $_GET["pageno"];
}
else
{
$page = 1;
}
$frompage = $page*$perpageview-$perpageview;
$csql = "SELECT COUNT(*) FROM questions WHERE question LIKE '%$searchquery%' OR answer LIKE '%$searchquery%'";
$cret = $db->querySingle($csql);
$pagecount = ceil($cret/$perpageview);
if ($cret == 0)
{
echo "<div class='sug'>Did you mean to ask any of this?</div>";
$sortFullMatch = "(CASE WHEN question LIKE '%".SQLite3::escapeString($searchquery)."%' OR answer LIKE '%".SQLite3::escapeString($searchquery)."%' THEN 1 ELSE 0 END) as fullmatch";
foreach ($query as $word)
{
$x++;
if ($x == 1)
{
$condition .= "question LIKE '%$word%'";
}
else
{
$condition .= "OR question LIKE '%$word%'";
}
}
$order = " ORDER BY fullmatch DESC, quiz_id DESC ";
$condition_q = "SELECT *,". $sortFullMatch ." FROM questions WHERE ".$condition.' '.$order.' LIMIT '.$frompage.','.$perpageview;
$result = $db->query($condition_q);
$concount = "SELECT COUNT(*) as count FROM questions WHERE " . $condition;
$resultCount = $db->querySingle($concount);
$pagecount = ceil($resultCount/$perpageview);
if ($resultCount > 0)
{
if ($result)
{
while ($row = $result->fetchArray(SQLITE3_ASSOC))
{
foreach($query as $text)
{
$wording = str_replace($text, "<span style='font-weight: bold; color: #1a0dab;'>".$text."</span>", $row['answer']);
}
echo '<div class="quesbox_3">
<div class="questitle">
<h2>'.$row["question"].'</h2>
</div>
<div class="quesanswer">'.$wording.'</div>
</div>';
}
echo '<div class="modulelinks">
<div class="mfound">Founded: <span>'.$founded.'</span></div>
<div class="mowned">Ownd By: <span>'.$owner.'</span></div>
</div>
</div>';
// <div class="mavailable">Available for Export Loan: <span>'.$available.'</span></div>
}
echo '<div class="page_num">';
for ($i=1; $i <= $pageount; $i++)
{
echo ''.$i.'';
}
echo '</div>';
}
}
}
else
{
$sql = $db->prepare("SELECT * FROM questions WHERE question LIKE '%$searchquery%' or answer LIKE '%$searchquery%' LIMIT '$frompage','$perpageview'");
$bsql = $db->prepare("SELECT * FROM banks WHERE bname LIKE '%$searchquery%' or bankbrief LIKE '%$searchquery%' LIMIT 1");
$sqlcount = "SELECT COUNT(*) as count FROM questions WHERE question LIKE '%$searchquery%' OR answer LIKE '%$searchquery%'";
$retCount = $db->querySingle($sqlcount);
$pagecount = ceil($retCount/$perpageview);
$ret = $sql->execute();
$bret = $bsql->execute();
while ($row = $ret->fetchArray(SQLITE3_ASSOC))
{
$wording = str_replace($searchquery, "<span style='font-weight: bold; color: #1a0dab;'>".$searchquery."</span>", $row['answer']);
echo '<div class="quesbox_3">
<div class="questitle">
<h2>'.$row["question"].'</h2>
</div>
<div class="quesanswer">'.$wording.'</div>
</div>';
}
?>
//for page links
<div class="page_num">
<?php
for ($i=1; $i <= $pagecount; $i++)
{
echo ''.$i.'';
}
?>
</div>
<?php
}
?>
What could be the issue with this? Thanks.
Related
How do i add PHP pagination to results coming from search query. I have tried here andhere but didn't seem to get how these works or should work with my code. I have written the search query before i thought of pagination cause i was using the load on scroll before. How do i make the results show paginated.
Below is my code to display searchresults.php.
if (isset($_GET["mainSearch"]))
{
$condition = '';
// $mainSearch = SQLite3::escapeString($_GET['mainSearch']);
$keyword = $_GET['mainSearch'];
$query = explode(" ", $keyword);
foreach ($query as $text)
{
$condition .= "question LIKE '%".SQLite3::escapeString($text)."%' OR answer LIKE '%".SQLite3::escapeString($text)."%' OR ";
}
$condition = substr($condition, 0, -4);
$order = " ORDER BY quiz_id DESC ";
$sql_query = "SELECT * FROM questions WHERE " . $condition . ' '. $order;
$sql_query_count = "SELECT COUNT(*) as count FROM questions WHERE " . $condition .' '. $order;
$result = $db->query($sql_query);
$resultCount = $db->querySingle($sql_query_count);
if ($resultCount > 0)
{
if ($result)
{
while ($row = $result->fetchArray(SQLITE3_ASSOC))
{
$wording = str_replace($text, "<span style='font-weight: bold;'>".$text."</span>", $row['answer']);
echo '<div class="quesbox_3">
<div class="questitle">
<h2>'.$row["question"].'</h2>
</div>
<div class="quesanswer">'.$wording.'</div>
</div>';
}
}
}
else
{
echo "No results found";
}
}
I was using this along side javascript to load more result on scroll
if (isset($_POST['limit']) && isset($_POST['start'])) {
$start = $_POST["start"];
$limit = $_POST["limit"];
$query =<<<EOF
SELECT * FROM questions ORDER BY quiz_id DESC LIMIT '$start', '$limit';
EOF;
// echo $query;
$result = $db->query($query);
while ($row = $result->fetchArray(SQLITE3_ASSOC)) {
echo '<div class="quesbox_2">
<div class="questitle">
<h2>'.$row["question"].'</h2>
</div>
<div class="quesanswer">'.$row["answer"].'</div>
<div class="quesdatetime"><img src="images/questime.png" alt="export question">'.$row["date"].'</div>
</div>';
}
}
How do i add pagination to my searchresults.php?
Thanks.
try this code without using jquery
<?php
if (isset($_GET["mainSearch"]))
{
$condition = '';
// $mainSearch = SQLite3::escapeString($_GET['mainSearch']);
$keyword = $_GET['mainSearch'];
$query = explode(" ", $keyword);
$perpageview=10;
if($_GET["pageno"]){
$page=$_GET["pageno"];
}else{
$page=1;
}
$frompage = $page*$perpageview-$perpageview;
foreach ($query as $text)
{
$condition .= "question LIKE '%".SQLite3::escapeString($text)."%' OR answer LIKE '%".SQLite3::escapeString($text)."%' OR ";
}
$condition = substr($condition, 0, -4);
$order = " ORDER BY quiz_id DESC ";
$sql_query = "SELECT * FROM questions WHERE " . $condition . ' '. $order.' LIMIT '.$frompage.','.$perpageview;
$sql_query_count = "SELECT COUNT(*) as count FROM questions WHERE " . $condition .' '. $order;
$result = $db->query($sql_query);
$resultCount = $db->querySingle($sql_query_count);
$pagecount = ceil($resultCount/$perpageview);
if ($resultCount > 0)
{
if ($result)
{
while ($row = $result->fetchArray(SQLITE3_ASSOC))
{
$wording = str_replace($text, "<span style='font-weight: bold;'>".$text."</span>", $row['answer']);
echo '<div class="quesbox_3">
<div class="questitle">
<h2>'.$row["question"].'</h2>
</div>
<div class="quesanswer">'.$wording.'</div>
</div>';
}
for ($i=1; $i <= $pagecount; $i++) {
echo ''.$i.'';
}
}
}
else
{
echo "No results found";
}
}
?>
I have the below code and i am trying to highlight the search keywords on echoing the results. I have tried what is here
and here
but didn't work.
Please where am i getting it wrong.
Below is my code
if (isset($_GET["mainSearch"]))
{
$condition = '';
// $mainSearch = SQLite3::escapeString($_GET['mainSearch']);
$keyword = $_GET['mainSearch'];
$query = explode(" ", $keyword);
foreach ($query as $text)
{
$condition .= "question LIKE '%".SQLite3::escapeString($text)."%' OR ";
}
$condition = substr($condition, 0, -4);
$order = " ORDER BY quiz_id DESC ";
$sql_query = "SELECT * FROM questions WHERE " . $condition . ' '. $order;
$sql_query_count = "SELECT COUNT(*) as count FROM questions WHERE " . $condition .' '. $order;
$result = $db->query($sql_query);
$resultCount = $db->querySingle($sql_query_count);
if ($resultCount > 0)
{
if ($result)
{
while ($row = $result->fetchArray(SQLITE3_ASSOC))
{
//TRYING TO HIGHLIGHT SEARCH KEYWORD HERE.
$wording = str_replace($keyword, "<span style='font-weight: bold;'>".$keyword."</span>", $row['answer']);
echo '<div class="quesbox_3">
<div class="questitle">
<h2>'.$row["question"].'</h2>
</div>
<div class="quesanswer">'.$wording.'</div>
</div>';
}
}
}
else
{
echo "No results found";
}
}
Thank you very much.
I have a php search script that search and echos out result from database. The issue is that i am trying to paginate the search results but when i click on a different page, it shows the same result. That is the search results are not shared across the pages. Please how do i fix?
Below is my code
if (isset($_GET["mainSearch"]))
{
$condition = '';
$mainSearch = SQLite3::escapeString($_GET['mainSearch']);
$keyword = $_GET['mainSearch'];
$query = explode(" ", $keyword);
$perpageview = 10;
$page = $_GET["pageno"];
$frompage = $page*$perpageview+1-$perpageview;
foreach ($query as $text)
{
$condition .= "question LIKE '%".SQLite3::escapeString($text)."%' OR answer LIKE '%".SQLite3::escapeString($text)."%' OR ";
}
$condition = substr($condition, 0, -4);
$order = " ORDER BY quiz_id DESC ";
$sql_query = "SELECT * FROM questions WHERE " . $condition . ' '. $order;
$sql_query_count = "SELECT COUNT(*) as count FROM questions WHERE " . $condition .' '. $order;
$result = $db->query($sql_query);
$resultCount = $db->querySingle($sql_query_count);
$pagecount = ceil($resultCount/$perpageview);
if ($resultCount > 0)
{
if ($result)
{
while ($row = $result->fetchArray(SQLITE3_ASSOC))
{
$wording = str_replace($text, "<span style='font-weight: bold;'>".$text."</span>", $row['answer']);
echo '<div class="quesbox_3">
<div class="questitle">
<h2>'.$row["question"].'</h2>
</div>
<div class="quesanswer">'.$wording.'</div>
</div>';
}
for ($i=1; $i <= $pagecount; $i++)
{
echo ''.$i.'';
}
}
}
else
{
echo "No results found";
}
}
Thanks very much.
I have mentioned in your previous request check
if (isset($_GET["mainSearch"]))
{
$condition = '';
// $mainSearch = SQLite3::escapeString($_GET['mainSearch']);
$keyword = $_GET['mainSearch'];
$query = explode(" ", $keyword);
$perpageview=10;
if($_GET["pageno"]){
$page=$_GET["pageno"];
}else{
$page=1;
}
$frompage = $page*$perpageview-$perpageview;
foreach ($query as $text)
{
$condition .= "question LIKE '%".SQLite3::escapeString($text)."%' OR answer LIKE '%".SQLite3::escapeString($text)."%' OR ";
}
$condition = substr($condition, 0, -4);
$order = " ORDER BY quiz_id DESC ";
$sql_query = "SELECT * FROM questions WHERE " . $condition . ' '. $order.' LIMIT '.$frompage.','.$perpageview;
$sql_query_count = "SELECT COUNT(*) as count FROM questions WHERE " . $condition .' '. $order;
$result = $db->query($sql_query);
$resultCount = $db->querySingle($sql_query_count);
$pagecount = ceil($resultCount/$perpageview);
if ($resultCount > 0)
{
if ($result)
{
while ($row = $result->fetchArray(SQLITE3_ASSOC))
{
$wording = str_replace($text, "<span style='font-weight: bold;'>".$text."</span>", $row['answer']);
echo '<div class="quesbox_3">
<div class="questitle">
<h2>'.$row["question"].'</h2>
</div>
<div class="quesanswer">'.$wording.'</div>
</div>';
}
for ($i=1; $i <= $pagecount; $i++) {
echo ''.$i.'';
}
}
}
else
{
echo "No results found";
}
}
Please help me, i tried to create search engine with pagination, everything seems fine except when i click back to page1's link, it pull out data from mysql orderly not from my inputted query, but this happen only on my page one.when i click i need really help on this!!!
<?php
session_start();
error_reporting(E_ERROR);
$link = mysql_connect("localhost","root","1234");
mysql_query("Use igstore");
$pagesquery = mysql_query("SELECT COUNT('creatorid') FROM `creator`");
$pages = ceil(mysql_result($pagesquery, 0)/$perpage);
$page = (isset($_GET['page']))?(int)$_GET['page']:1;
$start = ($page-1)*$perpage;
//to filter from inputted data from textbox and combobox
if($value == null&&$genre == null&&$star == null){
$sql = "SELECT * FROM `creator` WHERE `title` LIKE '%$find%' OR `creatorid` LIKE '$item' LIMIT $start, $perpage";}
else if($value != null){
$sql = "SELECT * FROM `creator` WHERE `aprice` = '$value' OR `creatorid` LIKE '$item' LIMIT $start, $perpage";}
else if($genre != null){
$sql = "SELECT * FROM `creator` WHERE `type` = '$genre' OR `creatorid` LIKE '$item' LIMIT $start, $perpage";}
else if($star != null){
$sql = "SELECT * FROM `creator` WHERE `popular` = '$star' OR `creatorid` LIKE '$item' LIMIT $start, $perpage";}
$result = mysql_query($sql) or die(mysql_error());
while($_GET = mysql_fetch_array($result)){
$head = $_GET['title'];
$def = $_GET['description'];
$pric = $_GET['aprice'];
$blob = $_GET['img1'];
echo"<html>";
echo'<table height="180" width="1300">';
echo"<tr>";
echo'<th align="center">';
echo'<div class="ab">';
echo'<img src="data:image/jpeg;base64,'.base64_encode($blob).'"/ width="150px" height="150px">';
echo'</div>';
echo'</th>';
echo'<th align="left">';
echo'<div class="container">';
echo"<a href='#'><h3>$head</h3></a>";
echo"<textarea style='resize: none;border: none' rows='5' cols='60' readonly>$def</textarea><br>";
echo"<label class='control-label col-sm-2'><h5>ราคา: $pric</h5></label><br><br>";
echo"</div>";
echo'</th>';
echo"</tr>";
echo'</table>';
echo'<hr width="100%">';
echo"</html>";
}
//this is for showing page number
$prev = $page - 1;
$next = $page + 1;
echo "<a href='result.php?page=$prev' style='display: inline-block';>Prev</a>";
for($index=1;$index<=$pages;$index++){
echo ($page == $index)?'<b><a href="?page='.$index.'" style="display: inline-block"; >'.$index.'</a> </b>':'<a href="?page='.$index.'" style="display: inline-block"; >'.$index.'</a>';
}
echo "<a href='result.php?page=$next' style='display: inline-block';>Next</a>";
?>
Thanks Ben, but now the other issue that I am facing is that result is not being displayed first time when I add the pagination to the code. See the second half of the code below. Please help
if(isset($_GET['k'])){ $k1 = $_GET['k']; } else { $k1 = ''; }
echo $k1;
$term = explode(" ", $k1);
$query = "SELECT * FROM database ";
foreach ($term as $each)
{
echo $each;
$i++;
if($i==1)
{
$query .= "WHERE keywords LIKE '%$each%' ";
}
else {
$query .= "OR WHERE keywords LIKE '%$each%' ";
}
}
$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());
$page = (isset($_GET['page'])) ? (int)
($_GET['page']) : 1;
$start = ($page - 1) * $per_pages;
$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 ''.$x.' ';
}
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 <br>Theme: $theme <br> Region: $region
</td> </tr>";
}
}
}
echo "</tbody>";
Below is a portion of my code where I am trying to search a word/ phrase entered in the text box. When I catch the value from the form in a php file using "$k1 = isset($_GET['k']);" the value which get's stored in variable "$each" is "1" and NOT the word or phrase entered by the user. This messess up the query which is performing the search function. Please help me to locate the error.
Please note that 'k' is the name of text box as defined in the form code below.
<form name="keywordquery" method="get" action="page2.php">
<fieldset class="fieldsetclass"><legend class="legendclass">Search by Keywords</legend>
<div id="searchbox">
<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";
$k1 = isset($_GET['k']);
echo $k1;
$term = explode(" ", $k1);
$query = "SELECT * FROM datatable ";
foreach ($term as $each)
{
echo $each;
$i++;
if($i==1)
{
$query .= "WHERE keywords LIKE '%$each%' ";
}
else {
$query .= "OR WHERE keywords LIKE '%$each%' ";
}
}
$k1 = isset($_GET['k']);
It's setting $k1 as 1 as it's ckecking to see if it's been set - in this case it has been and isset() is returning true, or 1.
what you want is:
if(isset($_GET['k'])){ $k1 = $_GET['k']; } else { $k1 = ''; }
or similar.
Thats because isset() is a bool function, check following link http://php.net/manual/en/function.isset.php
just do this
if(isset($_GET['k']))
$k1 = $_GET['k'];