Jump to content

search problem and paging problem


beboo002

Recommended Posts

hello all

i hav create a search form like

<form name="form1" method="post" action="test3.php">

                     

                <input name=search class="arial"id=Username  value=""  size="15"></td>

            <input type="submit" name="GO" value="GO">             

       

    </form>

 

and  search and paginging code


<?php

 

if($_POST['GO']!="") {

 

 

$link = mysql_connect('localhost', 'root', '');

if (!$link) {

    die('Not connected : ' . mysql_error());

}

 

// make foo the current db

$db_selected = mysql_select_db('admin', $link);

if (!$db_selected) {

    die ('Can\'t connect to database : ' . mysql_error());

}          // All database details will be included here

 

$page_name= "test3.php"; //  If you use this code with a different page ( or file ) name then change this

 

if(!isset($start)) {                        // This variable is set to zero for the first page

$start =0;

}

 

 

$eu = ($start -0);               

$limit = 4;                                // No of records to be shown per page.

$this = $eu + $limit;

$back = $eu - $limit;

$next = $eu + $limit;

 

 

 

 

/////////////// WE have to find out the number of records in our table. We will use this to break the pages///////

$query2=" SELECT * FROM description where cat_ID= '".$_POST[search]."'";//Url  and Title like '%$_POST[search]%'

$result2=mysql_query($query2);

echo mysql_error();

$nume=mysql_num_rows($result2); echo $nume;

/////// The variable nume above will store the total number of records in the table////

 

 

////////////// Now let us start executing the query with variables $eu and $limit  set at the top of the page///////////

$query=" SELECT * FROM description where cat_ID= '".$_POST[search]."' limit  $eu, $limit ";

$result=mysql_query($query);

echo mysql_error();

 

//////////////// Now we will display the returned records in side the rows of the table/////////

echo "<table width=450 cellpadding=0 cellspacing=0 border=0>";

while($noticia = mysql_fetch_array($result))

{

if($bgcolor=='#f1f1f1'){$bgcolor='#ffffff';}

else{$bgcolor='#f1f1f1';}

 

 

echo "<tr><td><a href='http://www.gmail.com'  >$noticia[cat_ID]</a></td></tr>";

echo "<tr><td >$noticia</td></tr>";

 

}

echo "</table>";

////////////////////////////// End of displaying the table with records ////////////////////////

 

///// Variables set for advance paging///////////

$p_limit=1000; // This should be more than $limit and set to a value for whick links to be breaked

if(!isset($p_f)){$p_f=0;}

$p_fwd=$p_f+$p_limit;

$p_back=$p_f-$p_limit;

//////////// End of variables for advance paging ///////////////

/////////////// Start the buttom links with Prev and next link with page numbers /////////////////

echo "<table align = 'center' width='350 '><tr><td  align='left' width='20%'>";

//if($p_f==0){print "<a href='$page_name?start=$p_back&p_f=$p_back'><font face='Verdana' size='2'>PREV $p_limit</font></a>"; }

echo "</td><td  align='left' width='10%'>";

//// if our variable $back is equal to 0 or more then only we will display the link to move back ////////

if($back >=0 and ($back >=$p_f)) {

print "<a href='$page_name?start=$back&p_f=$p_f'><font face='Verdana' size='2' class=toplink>PREV</font></a>";

}

//////////////// Let us display the page links at  center. We will not display the current page as a link ///////////

echo "</td><td align=center width='30%'>";

for($i=$p_f;$i < $nume and $i<($p_f+$p_limit);$i=$i+$limit){

if($i <> $eu){

$i2=$i+$p_f;

//echo " <a href='$page_name?start=$i&p_f=$p_f'><font face='Verdana' size='2'>$i</font></a> ";

}

//else { echo "<font face='Verdana' size='4' color=red>$i</font>";}        /// Current page is not displayed as link and given font color red

 

}

 

 

echo "</td><td  align='right' width='10%'>";

///////////// If we are not in the last page then Next link will be displayed. Here we check that /////

if($this < $nume and $this <($p_f+$p_limit)) {

print "<a href='$page_name?start=$next&p_f=$p_f'><font face='Verdana' size='2' class=toplink>NEXT</font></a>";}

echo "</td><td  align='right' width='20%'>";

//if($p_fwd < 1){

//print "<a href='$page_name?start=$p_fwd&p_f=$p_fwd'><font face='Verdana' size='2'>Last $p_limit 1</font></a>";

//}

echo "</td></tr></table>";

}

 

?>

 

now my problem is this when i send value via form in cat_id=4 then it disply the record of cat_id =4 and when i am click the next  in pagging then it disply the record of cat_id =0

 

Link to comment
Share on other sites

Check this ...

<form name="form1" method="post" action="test3.php">
                     
                <input name=search class="arial"id=Username  value=""  size="15"></td>
             <input type="submit" name="GO" value="GO">               
         
    </form>

and  search and paginging code<?php

if($_POST['GO']!="") {


$link = mysql_connect('localhost', 'root', '');
if (!$link) {
    die('Not connected : ' . mysql_error());
}

// make foo the current db
$db_selected = mysql_select_db('admin', $link);
if (!$db_selected) {
    die ('Can\'t connect to database : ' . mysql_error());
}           // All database details will be included here

$page_name= "test3.php"; //  If you use this code with a different page ( or file ) name then change this

if(!isset($start)) {                         // This variable is set to zero for the first page
$start =0;
}


$eu = ($start -0);               
$limit = 4;                                 // No of records to be shown per page.
$this = $eu + $limit;
$back = $eu - $limit;
$next = $eu + $limit;




/////////////// WE have to find out the number of records in our table. We will use this to break the pages///////
$query2=" SELECT * FROM description where cat_ID='".$_POST['search']."'";//Url  and Title like '%$_POST[search]%'
$result2=mysql_query($query2);
echo mysql_error();
$nume=mysql_num_rows($result2); echo $nume;
/////// The variable nume above will store the total number of records in the table////


////////////// Now let us start executing the query with variables $eu and $limit  set at the top of the page///////////
$query=" SELECT * FROM description where cat_ID='".$_POST['search']."' limit  $eu, $limit ";
$result=mysql_query($query);
echo mysql_error();

//////////////// Now we will display the returned records in side the rows of the table/////////
echo "<table width=450 cellpadding=0 cellspacing=0 border=0>";
while($noticia = mysql_fetch_array($result))
{
if($bgcolor=='#f1f1f1'){$bgcolor='#ffffff';}
else{$bgcolor='#f1f1f1';}


echo "<tr><td><a href='http://www.gmail.com'  >$noticia[cat_ID][/url]</td></tr>";
echo "<tr><td >$noticia</td></tr>"; }echo "</table>";////////////////////////////// End of displaying the table with records ///////////////////////////// Variables set for advance paging///////////$p_limit=1000; // This should be more than $limit and set to a value for whick links to be breakedif(!isset($p_f)){$p_f=0;}$p_fwd=$p_f+$p_limit;$p_back=$p_f-$p_limit;//////////// End of variables for advance paging ////////////////////////////// Start the buttom links with Prev and next link with page numbers /////////////////echo "<table align = 'center' width='350 '><tr><td  align='left' width='20%'>";//if($p_f==0){print "<a href='$page_name?start=$p_back&p_f=$p_back'><font face='Verdana' size='2'>PREV $p_limit</font>"; }
echo "</td><td  align='left' width='10%'>";
//// if our variable $back is equal to 0 or more then only we will display the link to move back ////////
if($back >=0 and ($back >=$p_f)) {
print "<a href='$page_name?start=$back&p_f=$p_f'><font face='Verdana' size='2' class=toplink>PREV</font>[/url]";
}
//////////////// Let us display the page links at  center. We will not display the current page as a link ///////////
echo "</td><td align=center width='30%'>";
for($i=$p_f;$i < $nume and $i<($p_f+$p_limit);$i=$i+$limit){
if($i <> $eu){
$i2=$i+$p_f;
//echo " <a href='$page_name?start=$i&p_f=$p_f'><font face='Verdana' size='2'>$i</font>[/url] ";
}
//else { echo "<font face='Verdana' size='4' color=red>$i</font>";}        /// Current page is not displayed as link and given font color red

}


echo "</td><td  align='right' width='10%'>";
///////////// If we are not in the last page then Next link will be displayed. Here we check that /////
if($this < $nume and $this <($p_f+$p_limit)) {
print "<a href='$page_name?start=$next&p_f=$p_f'><font face='Verdana' size='2' class=toplink>NEXT</font>[/url]";}
echo "</td><td  align='right' width='20%'>";
//if($p_fwd < 1){
//print "<a href='$page_name?start=$p_fwd&p_f=$p_fwd'><font face='Verdana' size='2'>Last $p_limit 1</font>[/url]";
//}
echo "</td></tr></table>";
}

?>

Link to comment
Share on other sites

i think main problem is form bcoz when form submit it give value in mysql query and it gives the result when we click for next record the form is reset it gives value 0 to mysql query and show the record of 0 .

i am use session also here but the value of session is also reset bcoz of form

 

is i am right?

 

now how i can hold form value for next page?

Link to comment
Share on other sites

See this example I did for Quiz.. it is almost same like yours hope this helps ....

 

<? session_start();
ob_start();
include("./include/userconfig.php");

/// here is the function to show the paging 
function showPaging($curpage)
{
global $sCurpageurl;
global $offset;
global $limit;
global $nTotalrows;
global $nTotalpage;
global $sOrderpagetype;
global $sOrderfield;

echo" <table width=100% border=0 cellspacing=0 cellpadding=5 class=flash_info >";
echo "<tr>";
$lastrowno=$offset+$limit;
if($lastrowno>=$nTotalrows)
$lastrowno=$nTotalrows;
if($nTotalrows==0)
$startrow=0;
else
$startrow=$offset+1;

echo"<td align=left width=50% class='flash_info'><b></b> " .$startrow ." - ". ($lastrowno) ." of ". $nTotalrows ."</td>";
echo"<td align=right width=50% class='flash_info'>";
echo "<b>Page : </b>";


/*  -- for prev
if($curpage<=$totalpage&&$curpage!=1)
{
$prepage=$curpage-1;
echo"<a href=broadcast.php?pno=".$prepage. "&field=".$orderfield."&pord=".$orderpagetype."> Prev</a>  ";
}
*/
for($i=1;$i<=$nTotalpage;$i++)
{
$curOffset=($i-1)*$limit;
	if($i==$curpage)
	echo "<b> ".$i."</b>";
	else
		if ( strpos($sCurpageurl,"?")>0)
	{
    echo"  <a href=".$sCurpageurl."&offset=".$curOffset."&pno=".$i."&field=".$sOrderfield."&pord=".$sOrderpagetype." admin_menu_link>".$i."</a>"  ;
	}
	else
	{
         echo"  <a href=".$sCurpageurl."?offset=".$curOffset."&pno=".$i."&field=".$sOrderfield."&pord=".$sOrderpagetype." admin_menu_link>".$i."</a>"  ;
}
}

/*  for next
if($curpage<$totalpage&&$curpage!=$totalpage)
{
     $nextpage=$curpage+1;
echo" <a href=broadcast.php?pno=".$nextpage ."&field=".$orderfield."&pord=".$orderpagetype.">Next</a>";
}
*/
echo"</td></tr></table>";



}
function totalPages($strsql){
global $limit;
global $nTotalrows;
global $nTotalpage;
$rs1=mysql_query( $strsql)or die("query failed11".mysql_error());
$rowno=mysql_num_rows($rs1);

if($rowno>0)
$nTotalpage=ceil($rowno/$limit);
else
$nTotalpage=0;

$nTotalrows=$rowno;

}

// end of functions

// request id from  Main Page
$id=$_REQUEST['tid'];


Connect();
$sTable ="quiz_question";
$sCurpageurl="permis_test.php";

// store the requsted variable in the id 
$tid = $id;



// PAging code 
if(!isset($_REQUEST["offset"])) {                         // This variable is set to zero for the first page
$offset = 0;
	}
else
$offset=$_REQUEST["offset"];

// passing offeset value in next page
if (isset($_POST['offset'])) 
$offset = $_POST['offset'];
//echo $offset;
//
//
$eu = ($offset - 0); 
$limit =5; // no. of result to be shown 
// check for pno also.. it is passed from hidden value when submit is pressed
if(isset($_REQUEST["pno"])) 
{
$current = $_REQUEST["pno"]; 
}
else 
{
	if (isset($_POST['pno']) ) 
	{
	$current=$_POST['pno']; 
	}
	else 
	{
	$current=1;
	}
}

$back = $eu - $limit; 
$next = $eu + $limit; 
$nextoffset = ($offset + 5);
$nextpno = $current + 1;

// select questions

$qry="SELECT * FROM $sTable where  test_id=".$id." order by que_id asc";
totalPages($qry);

$query=$qry." limit ".$offset .",".$limit;
$result=mysql_query($query);



?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />

</head>

<body>



				 <form name="frmsubmit" method="POST" action="permis_test.php?tid=<?=$id?>" enctype="multipart/form-data">	
             
                  <tr>
                    <td height="33" colspan="3" align="center" valign="top"> <input type="image" name="btnSubmit" id="btnSubmit" src="images/checkanswer.gif"   value="Check" />
				  <!--<input type="image" name="btnSubmit" id="btnSubmit" src="images/checkanswer.jpg"   value="Check" /> -->
                      <input type="hidden" name="offset" value="<?=$offset?>">
                      <input type="hidden" name="pno" value="<?=$current?>">
				  <input type="hidden" name="questions" value="questions">
				  </td>
                          <td width="3"></td>
                  </tr>
                  <tr>
                    <td width="78" height="28" valign="middle" class="contactTextBold">    <a href="permis.php" onmouseout="MM_swapImgRestore()" onmouseover="MM_swapImage('retour','','images/retour_on.gif',1)"><img src="images/retour_off.gif" alt="Re Tour" name="retour" width="55" height="14" border="0" id="retour" /></a><a href="permis.php" class="contactTextBold"></a> </td>
                    
                    <td width="159" align="center" valign="middle" class="home_dynamic_text">

				<a href="test.php?offset=<?=$nextoffset?>&pno=<?=$nextpno?>&tid=<?=$tid?>" class="contactTextBold">
				Next >></a>
								</td>
                    <td></td>
                  </tr>
                  
                 
              </form>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.