Page 1 of 1

Next & Prev buttons...

Posted: Thu Aug 15, 2002 6:18 am
by chia
Hi...
I've checked out some sites to get examples of how to implement Next and Prev buttons to my website but I seem to be having come problem with it. Could you please take a look at the code below and tell me what I have done wrong?

Code: Select all

// Receiving input from user
// Searchword
$searchword = $_GETї'searchword'];
print "Your word(s) is/are: <b>$searchword</b><p>\n\n";

// Checking the total number of findings
if (! empty($searchword ))&#123; 
	 $query = "SELECT diagram FROM arguments WHERE aml LIKE '%$searchword%'";
   $result1 = mysql_query($query)
				or die ("Query failed");
   $max = 0;
	 
// Determine the number of items containing the $searchword   
	 while ($line1 = mysql_fetch_array($result1))&#123;
	 $max++;
	 &#125;
// Display the findings	 
print "<center>Database Search returned $max items containing "<i>$searchword</i>".</center>"; 

// Free resources
mysql_free_result( $result1 );

&#125;

// The number of results to be displayed on screen
$maxresult = 2; 

// Searching by keyword
// Using user's input -> Note that will only search specific word(s), 
//    i.e. 'chinese' will have a set of results and 'chinese brown' will have a different set of 
//		results, plus 'brown' will have a 3rd different result
if (! empty($searchword ))&#123; 
	 $query = "SELECT aml FROM arguments WHERE aml LIKE '%$searchword%'";
   $result2 = mysql_query($query)
				or die ("Query failed");

// When the current page is yet to be determined
if (!$page) &#123; 
	 $page = 1;
&#125;

// Set up
$backpage = $page - 1;
$nextpage = $page + 1;

if (empty($start))&#123;
	 $start = 0;
&#125;
else &#123;
	 $start = ($maxresult * $page) - $maxresult;
&#125; 

$num_rows = mysql_num_rows($result2); 

// When the query returns less or equal number of rows than the limit set by $maxresult
if ($num_rows <= $maxresult) &#123;
	 $num_pages = 1; 
   &#125; 

// When the query returns the exact limit set by $maxresult
else if (($num_rows % $maxresult) == 0) &#123;
   $num_pages = ($num_rows / $maxresult);
   &#125; 

// For any other cases...
else &#123;
   $num_pages = ($num_rows / $maxresult) + 1;
   &#125; 

// Declared as an integer
$num_pages = (int) $num_pages;

// The current page is greater than the total number of pages or 
//     the current page is less than 0    
if (($page > $num_pages) || ($page < 0)) &#123;
	 error("You have specified an invalid page number");
   &#125;

// Set the limit per page   
$query = $query . " LIMIT $start, $maxresult";

$result2 = mysql_query($query);

// The navigation between pages
// Ensure only display when total number of return results exceeds $maxresult
//     i.e. will not display if only 1 page    
if ($max>$maxresult)&#123;
	 print "<center>- ";
   if ($backpage) &#123; 
   print "<a href="$PHP_SELF?searchword=$searchword&page=$backpage">Prev</a>";
   &#125; 

// If its the first page; have 'Prev' un-clickable
else &#123;
   print "Prev";
   &#125;

for ($i = 1; $i <= $num_pages; $i++) &#123;
   if ($i != $page) &#123; 
	 		print " <a href="$PHP_SELF?searchword=$searchword&page=$i">$i</a> ";
   &#125; 
	 else &#123; 
      print " $i "; 
   &#125; 
&#125;
    
if ($page != $num_pages) &#123;
   print "<a href="$PHP_SELF?searchword=$searchword&page=$nextpage">Next</a> -";
   &#125; 
else &#123;
   print "Next -";
   &#125;
   print "</center>";
&#125;	
	
print "<table border="10"><th BGCOLOR="#ff0000">Results</th>";
while ($line2 = mysql_fetch_array($result2, MYSQL_ASSOC)) &#123;
			print "\t<tr BGCOLOR="#ccffff">\n";
			// The different color, in this case aqua
		  foreach ($line2 as $col_value) &#123;
    					
							// Make into string
							$xml = join($line2,'');

							// Set up strings
							$search = '<!DOCTYPE ARG SYSTEM "argument.dtd">';

							// Replacement operation
							$new_xml = str_replace( $search, "", $xml);

							// Create an array
							$arguments = array('/_xml'=> $new_xml);

							$xsl = "sheet1.xsl";

							// Create an XSLT processor
							$xslthandler = xslt_create();

							// Perform the transformation
							$html = xslt_process($xslthandler, 'arg:/_xml', $xsl,  NULL, $arguments);

							// Detect errors
							if (!$html) die ('XSLT processing error: '.xslt_error($xslthandler));

							// Destroy the XSLT processor
							xslt_free($xslthandler);

							// Output the resulting HTML
							print "\t\t<td><pre>$html</pre></td>\n";	
      &#125;
			print "\t</tr>\n";
&#125;


// Free resources
mysql_free_result( $result2 );

&#125;

if (empty($searchword ))&#123;
	 print "<h1><center><font color='#000080'>You have not entered text to be searched!</center><hl>";
&#125;

// Closing connection
mysql_close( $link );
For clarity I should probably mentioned that this will be used to transform XML from a database to the browser. What I get is the "Prev, $i(s) and Next" buttons set up properly but whenever I hit the "Next" button it gives me the same 1st page. In other words the same (1st page) page is loaded over and over again. Kindly advice...

Thanks you.

Chia

Posted: Thu Aug 15, 2002 9:15 am
by fatalcure
here's a simplified version of your script:

Code: Select all

$totalquery = "SELECT COUNT(diagram) FROM arguments WHERE aml LIKE '%$searchword%'"; 
$totalresult = mysql_fetch_row(mysql_query($totalquery));
$total = $totalresult&#1111;0];   // EDIT!
mysql_free_result($totalresult);

$perpage = 2;
if (!$page || !is_numeric($page)) $page=1;
$limit = ($page - 1) * $perpage;

$numpages = ceil($total / $perpage);
$prev = $page - 1;
$next = $page + 1;

if ($numpages > 1) &#123;
		$pagesvar = "";
		if ($page - 3 > 0) $pagesvar .= "<a href='?page=1'><u><<</u></a> &nbsp;&nbsp;";
		if ($prev > 0 && $prev <= $numpages) $pagesvar .= "<a href='?page=$prev'><u><</u></a> &nbsp;&nbsp;";

		for ($i=$page-2; $i < $page; $i++) &#123;
				if ($i > 0) $pagesvar .= "<a href='?page=$i'><u>$i</u></a> &nbsp;&nbsp;";
		&#125;

		$pagesvar .= "<a>$page</a> &nbsp;&nbsp;";

		for ($i=$page+1; $i <= $page+2; $i++) &#123;
				if ($i <= $numpages) $pagesvar .= "<a href='?page=$i'><u>$i</u></a> &nbsp;&nbsp;";
		&#125;

		if ($next <= $numpages && $next > 0) $pagesvar .= "<a href='?page=$next'><u>></u></a> &nbsp;&nbsp;";
		if ($page + 3 <= $numpages) $pagesvar .= "<a href='?page=$numpages'><u>>></u></a> ";
&#125;

//now where ever you want to display the pages, just echo out $pagesvar
echo $pagesvar;
and to display your results on the page, your second query would look like tihs:

Code: Select all

$query2 = "SELECT  diagram FROM arguments WHERE aml LIKE '%$searchword%' ORDER BY treplydate DESC LIMIT $limit,$perpage";
$result2 = mysql_query($query2);
while ($row = mysql_fetch_array($result2)) &#123;
     //do your output here
&#125;
mysql_free_result($result2);
:)

oh and btw, your code wasn't working b/c this line

Code: Select all

$query = $query . " LIMIT $start, $maxresult";
came after you actually ran the query, so it will always fetch the same information for every page.

You should like the top code better, easier to understand.

Posted: Thu Aug 15, 2002 9:22 am
by chia
Will give it a go and see how it goes. Thanks for the quick reply and help.

Chia :)

question

Posted: Mon Aug 19, 2002 6:23 am
by chia
On the 3rd line you have in quotes
// EDIT
What do you mean?

Posted: Mon Aug 19, 2002 7:33 am
by fatalcure
i had $total = $total[0]; which is wrong.
i edited it to be $total = $totalresult[0];

Posted: Mon Aug 19, 2002 7:48 am
by chia
Thanks... still some minor probs but will get back here if I'm still stuck. Cheers...

Posted: Mon Aug 19, 2002 9:59 am
by chia
After setting things up the screen returns:
Parse error: parse error, unexpected $ in /home/httpd/html/ctan/searchbyword2.php on line 138
line 138 is the last line of my code. What gives? Thank again for helping me out.

Chia

Posted: Mon Aug 19, 2002 6:34 pm
by fatalcure
your probably missing a } or a ), check it carefully.

Thank you very much!!

Posted: Wed Aug 21, 2002 9:07 am
by chia
I've finally got my program working... thanks a lot!
:D

Posted: Wed Aug 21, 2002 10:49 pm
by fatalcure
heh, cool, you're welcome :)