954,568 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

How can we do the Pagination of Search Results.

Dear All,,

I have a search query for searching 40 fields that are belonging to 5 different tables..
I have used INNER JOIN for this purpose...

I have stored the search conditions into a $string variable and the result is getting correctly..

Here comes the problem....
I want to paginate the result of my search query comprises of JOINing of 5 tables...
I had done it as i know. but it has error... The last page will be displayed correctly and as i click on the FIRST link it will give the below error message...

SELECT * FROM reg_personal inner join reg_english_level inner join reg_services inner join reg_status ON(reg_personal.personal_code=reg_english_level.english_code AND reg_english_level.english_code=reg_services.services_code AND reg_services.services_code=reg_status.status_code)WHERE LIMIT 0,5
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'LIMIT 0,5' at line 1

I am giving the complete code with this thread..

Any one please help me..
Thanks in advance.
Jino..

<?php
require "config.php";
session_start();
if(!session_is_registered('code'))
{ header('Location:index.php?msg=Please login by using your username and password');
exit;}
if(session_is_registered('username'))
{ $username=$_SESSION['username'];
if($username=='Adminpanel'){$home='main-menu.php';}else if($username!='Adminpanel'){$home='main-menu1.php';}
}
$name=$_POST['name'];
$agefrom=$_POST['agefrom'];
$ageto=$_POST['ageto'];
$sex=$_POST['sex'];
$marital_status=$_POST['marital_status'];
$mobile=$_POST['mobile'];
$phone=$_POST['phone'];
$email=$_POST['email'];
$s1=$_POST['services1'];
$lc1=split(',',$s1);
$j1=count($lc1);
$service= $lc1[0];
$country=$_POST['Category'];
$location=$_POST['SubCat'];
$level=$_POST['level1'];
$university=$_POST['universities1'];
$training=$_POST['training1'];
$course=$_POST['course1'];
$ref=$_POST['refered_by'];
$ref_lc=split(',',$ref);
$ref=count($ref_lc);
$refered_by=$ref_lc[0];
$eng1=$_POST['english_level'];
$eng1_lc=split(',',$eng1);
$enj1=count($eng1_lc);
$english_level= $eng1_lc[0];
$total_score1=$_POST['total_score1'];
$total_score2=$_POST['total_score2'];
$writing_score1=$_POST['writing_score1'];
$writing_score2=$_POST['writing_score2'];
$speaking_score1=$_POST['speaking_score1'];
$speaking_score2=$_POST['speaking_score2'];
$listening_score1=$_POST['listening_score1'];
$listening_score2=$_POST['listening_score2'];
$reading_score1=$_POST['reading_score1'];
$reading_score2=$_POST['reading_score2'];
$result=$_POST['result'];
$date_from=$_POST['datefrom'];
$dat_com1=split('-',$date_from);
$da_no=count($dat_com1);
$d=$dat_com1[0];
$m=$dat_com1[1];
$y=$dat_com1[2];
$datefrom=date("$y-$m-$d");
$date_to=$_POST['dateto'];
$dat_com2=split('-',$date_to);
$da_no=count($dat_com2);
$d=$dat_com2[0];
$m=$dat_com2[1];
$y=$dat_com2[2];
$dateto=date("$y-$m-$d");
//Query for simply getting the total no. of records...
$query_pagi = " SELECT count(personal_code) FROM reg_personal ";
$result_pagi = mysql_query($query_pagi, $link) or trigger_error("SQL", E_USER_ERROR);
$query_data = mysql_fetch_row($result_pagi);
$numrows = $query_data[0];
$rows_per_page1 = 15;
$limit = 'LIMIT ' .($pageno - 1) * $rows_per_page .',' .$rows_per_page;


$query_array=array();
if($date_to!='' && $date_from!='')
{ $query_array[]= "reg_personal.personal_date BETWEEN '". $datefrom ."' AND '". $dateto ."'";}
else if($date_from!='' && $date_to==''){
$query_array[]= " reg_personal.personal_date = '". $datefrom ."'";}
if($name!=''){
$query_array[]= " reg_personal.personal_name = '". $name . "'";}
if($sex!=''){
$query_array[]= "reg_personal.personal_sex='".$sex."'";}
if($phone!=''){
$query_array[]= " reg_personal.personal_phone = '". $phone . "'";}
if($mobile!=''){
$query_array[]= " reg_personal.personal_mobile = '".$mobile."'";}
if($email!=''){
$query_array[]= " reg_personal.personal_email = '". $email . "'";}
if($marital_status!=''){
$query_array[]= " reg_personal.personal_matrital_status = '".$marital_status . "'";}
if($service!='0'){
$query_array[]= " reg_services.services_service1 ='". $service ."' OR reg_services.services_service2='". $service ."'";}
if($country!='0'){
$query_array[]= "reg_services.services_country1 ='". $country ."' OR reg_services.services_country2='". $country ."'";}
if($location!='0'){
$query_array[]= " reg_services.services_location1 ='". $location ."' OR reg_services.services_location2 ='". $location ."'";}
if($level!='0'){
$query_array[]= " reg_services.services_level1 ='". $level ."' OR reg_services.services_level2='". $level ."'";}
if($university!='0'){
$query_array[]= " reg_services.services_universities1 ='". $university ."' OR reg_services.services_universities2='". $university ."'";}
if($training!='0'){
$query_array[]= " reg_services.services_training1 ='". $training ."' OR reg_services.services_training2='". $training ."'";}
if($course!='0'){$query_array.= " reg_services.services_course1 ='". $course ."' OR reg_services.services_course2='". $course ."'";}


if($english_level!=0){
$query_array[]= "reg_english_level.english_level1 = '".$english_level. "' OR reg_english_level.english_level2 = '".$english_level. "' OR reg_english_level.english_level3 = '".$english_level. "'";}
if($total_score!=''){
$query_array[]= " reg_english_level.english_total_score1 = '". $total_score . "' OR reg_english_level.english_total_score2 = '". $total_score . "' OR reg_english_level.english_total_score3 = '". $total_score . "'";}
if($writing_score!=''){
$query_array[]= " reg_english_level.english_writing_score1 = '". $writing_score . "' OR reg_english_level.english_writing_score2 = '". $writing_score . "' OR reg_english_level.english_writing_score3 = '". $writing_score . "'";}
if($speaking_score!=''){
$query_array[]= "reg_english_level.english_speaking_score1 = '". $speaking_score . "' OR reg_english_level.english_speaking_score2 = '". $speaking_score . "' OR reg_english_level.english_speaking_score3 = '". $speaking_score . "'";}
if($listining_score!=''){
$query_array[]= " reg_english_level.english_listening_score1 = '". $listining_score . "' OR reg_english_level.english_listening_score2 = '". $listining_score . "' OR reg_english_level.english_listening_score3 = '". $listining_score . "'";}
if($reading_score!=''){
$query_array[]= " reg_english_level.english_reading_score1 = '". $reading_score . "' OR reg_english_level.english_reading_score2 = '". $reading_score . "' OR reg_english_level.english_reading_score3 = '". $reading_score . "'";}
if($result!=0){
$query_array[]= "reg_english_level.english_result1 = '". $result . "' OR reg_english_level.english_result2 = '". $result . "' OR reg_english_level.english_result3 = '". $result . "'";}
if($refered_by!=0){
$query_array[]= "reg_status.status_refered_by ='". $refered_by ."'";
}

$query_string=implode(" AND ",$query_array);

echo "

query_string=".$query_string;
$query_string1="$query_string";
echo "
query_string1_updated=".$query_string1;

if($query_string1!='')
{ $query_data=" SELECT SQL_CALC_FOUND_ROWS * FROM reg_personal inner join reg_english_level inner join reg_services inner join reg_status ON(reg_personal.personal_code=reg_english_level.english_code AND reg_english_level.english_code=reg_services.services_code AND reg_services.services_code=reg_status.status_code) WHERE $query_string1 ";
}else{
$query_data="SELECT SQL_CALC_FOUND_ROWS * FROM reg_personal inner join reg_english_level inner join reg_services inner join reg_status ON(reg_personal.personal_code=reg_english_level.english_code AND reg_english_level.english_code=reg_services.services_code AND reg_services.services_code=reg_status.status_code)";}
echo "
First Query".$query_data;
$result_data=mysql_query($query_data,$link) or die(mysql_error());

if($pageno <= $lastpage) {
$prevpage = $pageno-1;}
if ($pageno <= $lastpage){
$nextpage = $pageno+1;}
//Query for getting the count of records satisfying the search conditions...
$query1 = "select FOUND_ROWS()";
$result = mysql_query($query1,$link) or trigger_error("SQL", E_USER_ERROR);
$row_no = mysql_fetch_assoc($result);
$numrows1 = $row_no['FOUND_ROWS()'];
$rows_per_page2 = 5;
$lastpage = ceil($numrows1/$rows_per_page2);
echo '
$_get_pageno='.$_GET['pageno'].'
';
if (isset($_GET['pageno']))
{ $pageno = $_GET['pageno'];
} else{
$pageno = $lastpage;}
$pageno = (int)$pageno;
if ($pageno < 1) {
$pageno = 1;
} elseif ($pageno > $lastpage){
$pageno = $lastpage;}

$limit1 = 'LIMIT ' .($pageno - 1) * $rows_per_page2 .',' .$rows_per_page2;
echo "
Inner_Query_string1_new=".$query_string1;
$query_data1=" SELECT * FROM reg_personal inner join reg_english_level inner join reg_services inner join reg_status ON(reg_personal.personal_code=reg_english_level.english_code AND reg_english_level.english_code=reg_services.services_code AND reg_services.services_code=reg_status.status_code)WHERE $query_string1 $limit1";
print "
Second Query=".$query_data1."
";
$result_data1=mysql_query($query_data1) or die(mysql_error());

if($pageno <= $lastpage) { $prevpage = $pageno-1;} if ($pageno <= $lastpage){ $nextpage = $pageno+1;} if($numrows=='0') { header('Location:registeration-reports.php?regmsg=No Results. Try Again!'); } function headerdisp() { if(session_is_registered('username')) { $username=$_SESSION['username']; if($username=='Adminpanel'){$home='main-menu.php';}else if($username!='Adminpanel'){$home='main-menu1.php';} } echo" International Academy Office Softwarelogo.jpg








Main Menu
>>$username>> Payment
Results




Logout



";
}
echo "
Prev_page=".$prevpage;
echo "
Query_string1_test=".$query_string1;
function pagination($nextpage,$lastpage,$prevpage,$pageno,$query_string1)
{ echo "  
 <<First                             
<<Prev
   
                        [
$pageno - $lastpage
]                  
     Next>>                     
Last>>       


";
}
function disp($row)
{ $p_code=$row[personal_code];
$sql1=mysql_query(" SELECT * FROM reg_english_level WHERE english_code='$p_code' ") or die(mysql_error());
while($raw1=mysql_fetch_array($sql1))
{ $eng11=mysql_query(" SELECT course_name FROM englesh_level WHERE englesh_level_code='$raw1[english_level1]' ") or die(mysql_error());
while($eng1=mysql_fetch_array($eng11))
{ $course_name1=$eng1['course_name'];}
$eng2=$raw1['english_level2'];
$eng22=mysql_query(" SELECT course_name FROM englesh_level WHERE englesh_level_code='$raw1[english_level2]' ") or die(mysql_error());
while($eng2=mysql_fetch_array($eng22))
{ $course_name2=$eng2['course_name'];}
$eng3=$raw1['english_level3'];
$eng33=mysql_query(" SELECT course_name FROM englesh_level WHERE englesh_level_code='$raw1[english_level3]' ") or die(mysql_error());
while($eng3=mysql_fetch_array($eng33))
{ $course_name3=$eng3['course_name']; }
$eng_date=$raw1['english_date'];}
$email=$row['personal_email'];
$sql2=mysql_query("SELECT * FROM reg_payment WHERE payment_code='$p_code'") or die(mysql_error());
while($raw2=mysql_fetch_array($sql2))
{ $total_paid=$raw2['payment_amount_paid'];
$payment_date=$raw2['payment_date'];
$total_amount=$raw2['payment_total_amount'];
$balance=$total_amount-$total_paid;}
echo"

$row[personal_name];($row[personal_code]),$row[personal_present_address],Tel: $row[personal_phone], E-mail:

$email
,Joined $course_name1,$course_name2,$course_name3,  at
$eng_date - Total Paid $total_paid/-. Balance $balance/- payment $payment_date.


Continue to Payment >>
";
}
$val='search';
if($val=='search')
{ headerdisp();
echo "Total Search Results = ".$numrows1;
pagination($nextpage,$lastpage,$prevpage,$pageno,$query_string1);
while($row=mysql_fetch_array($result_data1))
{ disp($row);
}
} echo"

";
?>

jino
Junior Poster
117 posts since Feb 2008
Reputation Points: 10
Solved Threads: 10
 

I had done the pagination as i know. but it has error... The last page will be displayed correctly and as i click on the FIRST link it will give the below error message...

SELECT * FROM reg_personal inner join reg_english_level inner join reg_services inner join reg_status ON(reg_personal.personal_code=reg_english_level.english_code AND reg_english_level.english_code=reg_services.services_code AND reg_services.services_code=reg_status.status_code)WHERE LIMIT 0,5
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'LIMIT 0,5' at line 1


Please help me
Jino..

jino
Junior Poster
117 posts since Feb 2008
Reputation Points: 10
Solved Threads: 10
 

img1.jpg Your Search Results:

Institution Name





<?php
//Connect to DB
mysql_connect("localhost","root","") or die("Unable to connect to SQL server");
mysql_select_db("schooldb") or die("Unable to SELECT DB");

$Limit = 2; //Number of results per page
$SearchString=$_POST["searchname"]; // Get the search tearm

If($SearchString == "")
$SearchString=$_GET["searchname"]; // Get the search tearm

If($SearchString == "") {
Echo"Please type text to search for...";
exit();
}

$page=$_GET["page"]; //Get the page number to show
If($page == "") $page=1; //If no page number is set, the default page is 1

//Get the number of results
$SearchResult=mysql_query("SELECT * FROM schooltb WHERE name LIKE '%$SearchString%' ORDER BY serial_no") or die(mysql_error());
$NumberOfResults=mysql_num_rows($SearchResult);

//Get the number of pages
$NumberOfPages=ceil($NumberOfResults/$Limit);

$SearchResult=mysql_query("SELECT * FROM schooltb WHERE name LIKE '%$SearchString%' ORDER BY serial_no LIMIT " . ($page-1)*$Limit . ",$Limit") or die(mysql_error());


While($row = mysql_fetch_object($SearchResult))
{
//Echo $row->name . "
";

?>


<?php echo $row->path2; ?>
center>Your Logo Name of Institution <?php echo $row->name;?> Address <?php echo $row->address;;?> City <?php echo $row->city; ?> PTCL City Code No. <?php echo $row->citycode;?> PTCL Phone No. <?php echo $row->ptclno;?> Sector <?php echo $row->sector;?> Study Level <?php echo $row->level;?> Language <?php echo $row->language;?> Education Type <?php echo $row->edutype;?> Affiliated with <?php echo $row->affiliated;?> Em@il (Optional) <?php echo $row->email_address;?> Web URL (Optional) <?php echo $row->weburl;?> Transport Route <?php echo $row->transport;?> Location Map * <?php echo $row->path;?>     <?php

}

$Nav="";

If($page > 1)
{
$Nav .= " Prev ";
}

For($i = 1 ; $i <= $NumberOfPages ; $i++)
{
If($i == $page)
{
$Nav .= "  $i  ";
}
else
{
$Nav .= " $i ";
}
}

If($page < $NumberOfPages)
{
$Nav .= " Next ";
}

Echo "

" . $Nav. "

";
echo "" . mysql_num_rows($SearchResult). " Record(s) Founded on This Page
";

?>


For This Search Result Display Code of PHP Pagewise using textbox this is the Style sheet file please note it carefully:

div.pagination {padding: 1px;margin: 1px;}
div.pagination a {padding: 1px 2px 1px 2px; margin: 1px; border: 1px solid #AAAADD; text-decoration: none; font-size: 12px; color: #000099;}

div.pagination a:hover, div.pagination a:active {border: 1px solid #000099; color: #000;}

div.pagination span.current {padding: 1px 2px 1px 2px; margin: 1px; border: 1px solid #000099; font-weight: bold; font-size: 12px;background-color: #000099;color: #FFF;}

div.pagination span.disabled {padding: 1px 2px 1px 2px;margin: 1px;border: 1px solid #EEE;font-size: 12px;color: #DDD;} For This Search Result Display Code of PHP Pagewise using textbox this is the Style sheet file please note it carefully:

div.pagination {padding: 1px;margin: 1px;}
div.pagination a {padding: 1px 2px 1px 2px; margin: 1px; border: 1px solid #AAAADD; text-decoration: none; font-size: 12px; color: #000099;}

div.pagination a:hover, div.pagination a:active {border: 1px solid #000099; color: #000;}

div.pagination span.current {padding: 1px 2px 1px 2px; margin: 1px; border: 1px solid #000099; font-weight: bold; font-size: 12px;background-color: #000099;color: #FFF;}

div.pagination span.disabled {padding: 1px 2px 1px 2px;margin: 1px;border: 1px solid #EEE;font-size: 12px;color: #DDD;}

kashif farooq
Newbie Poster
12 posts since Sep 2008
Reputation Points: 10
Solved Threads: 0
 

Iam also sending the attached zip file for display results page wise using search textbox

Attachments searchingfilephp-with-stylesheet.zip (5.26KB)
kashif farooq
Newbie Poster
12 posts since Sep 2008
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You