Jump to content

Queries work in PHPMyAdmin but not in code


blackdogupya
Go to solution Solved by mac_gyver,

Recommended Posts

Hi Guys and Girls!

 

I've got a search query that i'm trying to execute via a PHP script.  When I run the statements in PHPMyAdmin, I get perfect results.

 

When I run it in the PHP script, it's just giving me a 500 error and won't process the query.

 

Here's the code:

<?php
	/*
		Make connections to other scripts that have code in them
	*/
	include('format.php'); // includes formatting for results
	include('config.php');	// include your code to connect to DB.

	$tbl_name="animal_info"; //name of the table within the MySQL Database that we're going to search
	
	// How many adjacent pages should be shown on each side?
	$adjacents = 3;

	//Let's not forget the register globals off crap
			$animalID = $_GET['animalID'];
			$status = $_GET['status'];
			$date = $_GET['date'];
			$species = $_GET['species'];
			$breed = $_GET['breed'];
			$sex = $_GET['sex'];
			$primary_colour = $_GET['primary_colour'];
			$colour = $_GET['colour'];
			$distinctive_traits = $_GET['distinctive_traits'];
			$fur_length = $_GET['fur_length'];
			$age = $_GET['age'];
			$desexed = $_GET['desexed'];
			$microchipped = $_GET['microchipped'];
			$suburb = $_GET['suburb'];
			$pound_area = $_GET['pound_area'];
			$contact = $_GET['contact'];
			$link = $_GET['link'];
			$columns = $_GET['columns'];
			$find = $_GET['find'];
			$searching = $_GET['searching'];


	// We perform a bit of filtering 
	   	  	$find = strtoupper($find); 
			$find = strip_tags($find); 
			$find = trim ($find); 
	
	/* 
	   Here we're going to get the total number of rows in the database to calculate how many pages of data there are depending on the search terms
	*/
	$query = "SELECT COUNT(*) as num FROM animal_info WHERE MATCH(animalID, status, date, species, breed, sex, primary_colour, colour, distinctive_traits, fur_length, age, desexed, microchipped, suburb, pound_area, contact, link, notes)
AGAINST('%find%' IN BOOLEAN MODE);"

	$total_pages = mysql_fetch_array(mysql_query($query));
	$total_pages = $total_pages[num];
	
	/* Setup vars for query. */
	$targetpage = "process_search.php"; 	//your file name  (the name of this file)
	$limit = 1; 								//how many items to show per page
	$page = $_GET['page'];
	if($page) 
		$start = ($page - 1) * $limit; 			//first item to display on this page
	else
		$start = 0;								//if no page var is given, set start to 0
	
	/* Get data. */
	$sql = "SELECT * from animal_info where MATCH(animalID, status, date, species, breed, sex, primary_colour, colour, distinctive_traits, fur_length, age, desexed, microchipped, suburb, pound_area, contact, link, notes)
AGAINST('%find%' IN BOOLEAN MODE);"
	$result = mysql_query($sql);
	
	/* Setup page vars for display. */
	if ($page == 0) $page = 1;					//if no page var is given, default to 1.
	$prev = $page - 1;							//previous page is page - 1
	$next = $page + 1;							//next page is page + 1
	$lastpage = ceil($total_pages/$limit);		//lastpage is = total pages / items per page, rounded up.
	$lpm1 = $lastpage - 1;						//last page minus 1
	
	/* 
		Now we apply our rules and draw the pagination object. 
		We're actually saving the code to a variable in case we want to draw it more than once.
	*/
	$pagination = "";
	if($lastpage > 1)
	{	
		$pagination .= "<div class=\"pagination\">";
		//previous button
		if ($page > 1) 
			$pagination.= "<a href=\"$targetpage?page=$prev\">? previous</a>";
		else
			$pagination.= "<span class=\"disabled\">? previous</span>";	
		
		//pages	
		if ($lastpage < 7 + ($adjacents * 2))	//not enough pages to bother breaking it up
		{	
			for ($counter = 1; $counter <= $lastpage; $counter++)
			{
				if ($counter == $page)
					$pagination.= "<span class=\"current\">$counter</span>";
				else
					$pagination.= "<a href=\"$targetpage?page=$counter\">$counter</a>";					
			}
		}
		elseif($lastpage > 5 + ($adjacents * 2))	//enough pages to hide some
		{
			//close to beginning; only hide later pages
			if($page < 1 + ($adjacents * 2))		
			{
				for ($counter = 1; $counter < 4 + ($adjacents * 2); $counter++)
				{
					if ($counter == $page)
						$pagination.= "<span class=\"current\">$counter</span>";
					else
						$pagination.= "<a href=\"$targetpage?page=$counter\">$counter</a>";					
				}
				$pagination.= "...";
				$pagination.= "<a href=\"$targetpage?page=$lpm1\">$lpm1</a>";
				$pagination.= "<a href=\"$targetpage?page=$lastpage\">$lastpage</a>";		
			}
			//in middle; hide some front and some back
			elseif($lastpage - ($adjacents * 2) > $page && $page > ($adjacents * 2))
			{
				$pagination.= "<a href=\"$targetpage?page=1\">1</a>";
				$pagination.= "<a href=\"$targetpage?page=2\">2</a>";
				$pagination.= "...";
				for ($counter = $page - $adjacents; $counter <= $page + $adjacents; $counter++)
				{
					if ($counter == $page)
						$pagination.= "<span class=\"current\">$counter</span>";
					else
						$pagination.= "<a href=\"$targetpage?page=$counter\">$counter</a>";					
				}
				$pagination.= "...";
				$pagination.= "<a href=\"$targetpage?page=$lpm1\">$lpm1</a>";
				$pagination.= "<a href=\"$targetpage?page=$lastpage\">$lastpage</a>";		
			}
			//close to end; only hide early pages
			else
			{
				$pagination.= "<a href=\"$targetpage?page=1\">1</a>";
				$pagination.= "<a href=\"$targetpage?page=2\">2</a>";
				$pagination.= "...";
				for ($counter = $lastpage - (2 + ($adjacents * 2)); $counter <= $lastpage; $counter++)
				{
					if ($counter == $page)
						$pagination.= "<span class=\"current\">$counter</span>";
					else
						$pagination.= "<a href=\"$targetpage?page=$counter\">$counter</a>";					
				}
			}
		}
		
		//next button
		if ($page < $counter - 1) 
			$pagination.= "<a href=\"$targetpage?page=$next\">next ?</a>";
		else
			$pagination.= "<span class=\"disabled\">next ?</span>";
		$pagination.= "</div>\n";		
	}
?>

	<?php
		while($row = mysql_fetch_array($result))
		{
	
//Build the HTML for the results in the table
echo "$resultsHTMLabove";
echo "<br><BR>";
//Here's the table
echo "<table width=\"100%\" border=\"0\" cellspacing=\"5\" cellpadding=\"2\">";
echo "<tr>";
echo "<th scope=\"row\" align=\"left\">Animal ID</th>";
echo "<td>".$row['animalID']."</td>";
echo "</tr>";
echo "<tr>";
echo "<th scope=\"row\" align=\"left\">Status</th>";
echo "<td>".$row['status']."</td>";
echo "</tr>";
echo "<tr>";
echo "<th scope=\"row\" align=\"left\">Date</th>";
echo "<td>".$row['date']."</td>";
echo "</tr>";
echo "<tr>";
echo "<th scope=\"row\" align=\"left\">Species</th>";
echo "<td>".$row['species']."</td>";
echo "</tr>";
echo "<tr>";
echo "<th scope=\"row\" align=\"left\">Breed</th>";
echo "<td>".$row['breed']."</td>";
echo "</tr>";
echo "<tr>";
echo "<th scope=\"row\" align=\"left\">Sex</th>";
echo "<td>".$row['sex']."</td>";
echo "</tr>";
echo "<tr>";
echo "<th scope=\"row\" align=\"left\">Primary Colour</th>";
echo "<td>".$row['primary_colour']."</td>";
echo "</tr>";
echo "<tr>";
echo "<th scope=\"row\" align=\"left\">Colour</th>";
echo "<td>".$row['colour']."</td>";
echo "</tr>";
echo "<tr>";
echo "<th scope=\"row\" align=\"left\">Distinctive Traits</th>";
echo "<td>".$row['distinctive_traits']."</td>";
echo "</tr>";
echo "<tr>";
echo "<th scope=\"row\" align=\"left\">Fur Length</th>";
echo "<td>".$row['fur_length']."</td>";
echo "</tr>";
echo "<tr>";
echo "<th scope=\"row\" align=\"left\">Age</th>";
echo "<td>".$row['age']."</td>";
echo "</tr>";
echo "<tr>";
echo "<th scope=\"row\" align=\"left\">Desexed?</th>";
echo "<td>".$row['desexed']."</td>";
echo "</tr>";
echo "<tr>";
echo "<th scope=\"row\" align=\"left\">Microchipped?</th>";
echo "<td>".$row['microchipped']."</td>";
echo "</tr>";
echo "<tr>";
echo "<th scope=\"row\" align=\"left\">Suburb</th>";
echo "<td>".$row['suburb']."</td>";
echo "</tr>";
echo "<tr>";
echo "<th scope=\"row\" align=\"left\">Pound Area</th>";
echo "<td>".$row['pound_area']."</td>";
echo "</tr>";
echo "<tr>";
echo "<th scope=\"row\" align=\"left\">Contact</th>";
echo "<td>".$row['contact']."</td>";
echo "</tr>";
echo "<tr>";
echo "<th scope=\"row\" align=\"left\">Link</th>";
echo "<td><a href='".$row['link']."' target=_blank >Link to Facebook</a></td>";
echo "</tr>";
echo "<tr>";
echo "<th scope=\"row\" align=\"left\">Notes</th>";
echo "<td>".$row['notes']."</td>";
echo "</tr>";
echo "<tr>";
echo "<th scope=\"row\" align=\"left\">Photo</th>";
echo "<td>";
echo "<img src=\"getimage.php?id=".$row['ID']."\" width=\"200px\" height=\"150px\" />";
echo "</td>";
echo "</tr>";
echo "</table>";
echo "<BR><BR>";
//And we remind them what they searched for 
echo "<b>You searched for:</b>".$find; 
echo "</div>";
//Build the footer part of the page
echo "$resultsHTMLbelow";
} 
//If there wasn't any results, we say thanks for searching, but we couldn't find anything that was worth showing and ask them to try again
if ($counter == 0) 
{ 
//Once again, build the HTML output of the page for the "No Search Results" page
echo "$noResultsSearch";
} 
	?>

<?=$pagination?>
	

If anyone can see anything obvious, I would love to hear from you!

 

Thanks in advance!

 

Cheers,

 

Dave

Link to comment
Share on other sites

http 500 responses from php scripts generally mean that your script had a fatal parse or fatal runtime error and didn't produce a complete response.

 

in your case there's a fatal parse error because your the php statement on line 44/45 doesn't have a closing ; on it.

 

please develop and debug php code with php's error_reporting set to E_ALL and display_errors set to ON so that php will help you. these settings must be in your php.ini to show parse errors in your main file.

 

edit: once you fix that parse error, you have at least one more later in your code. TURN ON PHP'S ERROR_REPORTING/DISPLAY_ERRORS before you spend any more time trying to do this.

Edited by mac_gyver
Link to comment
Share on other sites

Thanks for that.  I found that one before your reply, but I'm moving house and have no internet at present! :)

 

So this is what I'm using now:

<?php
	/*
		Make connections to other scripts that have code in them
	*/
	include('format.php'); // includes formatting for results
	include('config.php');	// include your code to connect to DB.

	$tbl_name="animal_info"; //name of the table within the MySQL Database that we're going to search
	
	// How many adjacent pages should be shown on each side?
	$adjacents = 3;

	//Let's not forget the register globals off crap
			$animalID = $_GET['animalID'];
			$status = $_GET['status'];
			$date = $_GET['date'];
			$species = $_GET['species'];
			$breed = $_GET['breed'];
			$sex = $_GET['sex'];
			$primary_colour = $_GET['primary_colour'];
			$colour = $_GET['colour'];
			$distinctive_traits = $_GET['distinctive_traits'];
			$fur_length = $_GET['fur_length'];
			$age = $_GET['age'];
			$desexed = $_GET['desexed'];
			$microchipped = $_GET['microchipped'];
			$suburb = $_GET['suburb'];
			$pound_area = $_GET['pound_area'];
			$contact = $_GET['contact'];
			$link = $_GET['link'];
			$columns = $_GET['columns'];
			$find = $_GET['find'];
			$searching = $_GET['searching'];


	// We perform a bit of filtering 
	   	  	$find = strtoupper($find); 
			$find = strip_tags($find); 
			$find = trim ($find); 
	
	/* 
	   Here we're going to get the total number of rows in the database to calculate how many pages of data there are depending on the search terms
	*/
	$query = "SELECT COUNT(*) as num FROM $tbl_name WHERE MATCH('animalID', 'status', 'date', 'species', 'breed', 'sex', 'primary_colour', 'colour', 'distinctive_traits', 'fur_length', 'age', 'desexed', 'microchipped', 'suburb', 'pound_area', 'contact', 'link', 'notes')
AGAINST('%find%' IN BOOLEAN MODE)";

	$total_pages = mysql_fetch_array(mysql_query($query));
	$total_pages = $total_pages[num];
	
	/* Setup vars for query. */
	$targetpage = "process_search.php"; 	//your file name  (the name of this file)
	$limit = 1; 								//how many items to show per page
	$page = $_GET['page'];
	if($page) 
		$start = ($page - 1) * $limit; 			//first item to display on this page
	else
		$start = 0;								//if no page var is given, set start to 0
	
	/* Get data. */
	$sql = "SELECT * from $tbl_name where MATCH('animalID', 'status', 'date', 'species', 'breed', 'sex', 'primary_colour', 'colour', 'distinctive_traits', 'fur_length', 'age', 'desexed', 'microchipped', 'suburb', 'pound_area', 'contact', 'link', 'notes')
AGAINST('%find%' IN BOOLEAN MODE)";
	$result = mysql_query($sql);
	
	/* Setup page vars for display. */
	if ($page == 0) $page = 1;					//if no page var is given, default to 1.
	$prev = $page - 1;							//previous page is page - 1
	$next = $page + 1;							//next page is page + 1
	$lastpage = ceil($total_pages/$limit);		//lastpage is = total pages / items per page, rounded up.
	$lpm1 = $lastpage - 1;						//last page minus 1
	
	/* 
		Now we apply our rules and draw the pagination object. 
		We're actually saving the code to a variable in case we want to draw it more than once.
	*/
	$pagination = "";
	if($lastpage > 1)
	{	
		$pagination .= "<div class=\"pagination\">";
		//previous button
		if ($page > 1) 
			$pagination.= "<a href=\"$targetpage?page=$prev\">? previous</a>";
		else
			$pagination.= "<span class=\"disabled\">? previous</span>";	
		
		//pages	
		if ($lastpage < 7 + ($adjacents * 2))	//not enough pages to bother breaking it up
		{	
			for ($counter = 1; $counter <= $lastpage; $counter++)
			{
				if ($counter == $page)
					$pagination.= "<span class=\"current\">$counter</span>";
				else
					$pagination.= "<a href=\"$targetpage?page=$counter\">$counter</a>";					
			}
		}
		elseif($lastpage > 5 + ($adjacents * 2))	//enough pages to hide some
		{
			//close to beginning; only hide later pages
			if($page < 1 + ($adjacents * 2))		
			{
				for ($counter = 1; $counter < 4 + ($adjacents * 2); $counter++)
				{
					if ($counter == $page)
						$pagination.= "<span class=\"current\">$counter</span>";
					else
						$pagination.= "<a href=\"$targetpage?page=$counter\">$counter</a>";					
				}
				$pagination.= "...";
				$pagination.= "<a href=\"$targetpage?page=$lpm1\">$lpm1</a>";
				$pagination.= "<a href=\"$targetpage?page=$lastpage\">$lastpage</a>";		
			}
			//in middle; hide some front and some back
			elseif($lastpage - ($adjacents * 2) > $page && $page > ($adjacents * 2))
			{
				$pagination.= "<a href=\"$targetpage?page=1\">1</a>";
				$pagination.= "<a href=\"$targetpage?page=2\">2</a>";
				$pagination.= "...";
				for ($counter = $page - $adjacents; $counter <= $page + $adjacents; $counter++)
				{
					if ($counter == $page)
						$pagination.= "<span class=\"current\">$counter</span>";
					else
						$pagination.= "<a href=\"$targetpage?page=$counter\">$counter</a>";					
				}
				$pagination.= "...";
				$pagination.= "<a href=\"$targetpage?page=$lpm1\">$lpm1</a>";
				$pagination.= "<a href=\"$targetpage?page=$lastpage\">$lastpage</a>";		
			}
			//close to end; only hide early pages
			else
			{
				$pagination.= "<a href=\"$targetpage?page=1\">1</a>";
				$pagination.= "<a href=\"$targetpage?page=2\">2</a>";
				$pagination.= "...";
				for ($counter = $lastpage - (2 + ($adjacents * 2)); $counter <= $lastpage; $counter++)
				{
					if ($counter == $page)
						$pagination.= "<span class=\"current\">$counter</span>";
					else
						$pagination.= "<a href=\"$targetpage?page=$counter\">$counter</a>";					
				}
			}
		}
		
		//next button
		if ($page < $counter - 1) 
			$pagination.= "<a href=\"$targetpage?page=$next\">next ?</a>";
		else
			$pagination.= "<span class=\"disabled\">next ?</span>";
		$pagination.= "</div>\n";		
	}
?>

	<?php
		while($row = mysql_fetch_array($result))
		{
	
		// Your while loop here
//Build the HTML for the results in the table
echo "$resultsHTMLabove";
echo "<br><BR>";
//Here's the table
echo "<table width=\"100%\" border=\"0\" cellspacing=\"5\" cellpadding=\"2\">";
echo "<tr>";
echo "<th scope=\"row\" align=\"left\">Animal ID</th>";
echo "<td>".$row['animalID']."</td>";
echo "</tr>";
echo "<tr>";
echo "<th scope=\"row\" align=\"left\">Status</th>";
echo "<td>".$row['status']."</td>";
echo "</tr>";
echo "<tr>";
echo "<th scope=\"row\" align=\"left\">Date</th>";
echo "<td>".$row['date']."</td>";
echo "</tr>";
echo "<tr>";
echo "<th scope=\"row\" align=\"left\">Species</th>";
echo "<td>".$row['species']."</td>";
echo "</tr>";
echo "<tr>";
echo "<th scope=\"row\" align=\"left\">Breed</th>";
echo "<td>".$row['breed']."</td>";
echo "</tr>";
echo "<tr>";
echo "<th scope=\"row\" align=\"left\">Sex</th>";
echo "<td>".$row['sex']."</td>";
echo "</tr>";
echo "<tr>";
echo "<th scope=\"row\" align=\"left\">Primary Colour</th>";
echo "<td>".$row['primary_colour']."</td>";
echo "</tr>";
echo "<tr>";
echo "<th scope=\"row\" align=\"left\">Colour</th>";
echo "<td>".$row['colour']."</td>";
echo "</tr>";
echo "<tr>";
echo "<th scope=\"row\" align=\"left\">Distinctive Traits</th>";
echo "<td>".$row['distinctive_traits']."</td>";
echo "</tr>";
echo "<tr>";
echo "<th scope=\"row\" align=\"left\">Fur Length</th>";
echo "<td>".$row['fur_length']."</td>";
echo "</tr>";
echo "<tr>";
echo "<th scope=\"row\" align=\"left\">Age</th>";
echo "<td>".$row['age']."</td>";
echo "</tr>";
echo "<tr>";
echo "<th scope=\"row\" align=\"left\">Desexed?</th>";
echo "<td>".$row['desexed']."</td>";
echo "</tr>";
echo "<tr>";
echo "<th scope=\"row\" align=\"left\">Microchipped?</th>";
echo "<td>".$row['microchipped']."</td>";
echo "</tr>";
echo "<tr>";
echo "<th scope=\"row\" align=\"left\">Suburb</th>";
echo "<td>".$row['suburb']."</td>";
echo "</tr>";
echo "<tr>";
echo "<th scope=\"row\" align=\"left\">Pound Area</th>";
echo "<td>".$row['pound_area']."</td>";
echo "</tr>";
echo "<tr>";
echo "<th scope=\"row\" align=\"left\">Contact</th>";
echo "<td>".$row['contact']."</td>";
echo "</tr>";
echo "<tr>";
echo "<th scope=\"row\" align=\"left\">Link</th>";
echo "<td><a href='".$row['link']."' target=_blank >Link to Facebook</a></td>";
echo "</tr>";
echo "<tr>";
echo "<th scope=\"row\" align=\"left\">Notes</th>";
echo "<td>".$row['notes']."</td>";
echo "</tr>";
echo "<tr>";
echo "<th scope=\"row\" align=\"left\">Photo</th>";
echo "<td>";
echo "<img src=\"getimage.php?id=".$row['ID']."\" width=\"200px\" height=\"150px\" />";
echo "</td>";
echo "</tr>";
echo "</table>";
echo "<BR><BR>";
//And we remind them what they searched for 
echo "<b>You searched for:</b>".$find; 
echo "</div>";
//Build the footer part of the page
echo "$resultsHTMLbelow";
} 
	?>

<?=$pagination?>
	

But now it's just returning a blank page.  I'm on shared hosting, so have no access to the php.ini at the moment, but it might just be that I have to run a local server and turn it on.  But if there's anything that I'm missing again, then I would love some input.  I've run the code through a code checker and it's coming back ok.

 

Thanks for the help so far.

 

Cheers,

Dave

Link to comment
Share on other sites

  • Solution

shared web hosting would allow you to use a local php.ini

 

also, if the code acts like it doesn't have a fatal parse error in the main file, you can set php's error_reporting/display_errors in the code.

 

edit: the only obvious thing wrong with the code now is you are not using the $find variable in the queries. you have the literal string find (missing the $ to make it the variable.)

Edited by mac_gyver
Link to comment
Share on other sites

Thanks for the help! :)

 

After going cross-eyed looking at my mistakes, I've finally (with your input) worked it out.  See below for completed code:

<?php
	/*
		Make connections to other scripts that have code in them
	*/
	include('format.php'); // includes formatting for results
	include('config.php');	// include your code to connect to DB.

	$tbl_name="animal_info"; //name of the table within the MySQL Database that we're going to search
	
	// How many adjacent pages should be shown on each side?
	$adjacents = 3;

	//Let's not forget the register globals off crap
			$animalID = $_GET['animalID'];
			$status = $_GET['status'];
			$date = $_GET['date'];
			$species = $_GET['species'];
			$breed = $_GET['breed'];
			$sex = $_GET['sex'];
			$primary_colour = $_GET['primary_colour'];
			$colour = $_GET['colour'];
			$distinctive_traits = $_GET['distinctive_traits'];
			$fur_length = $_GET['fur_length'];
			$age = $_GET['age'];
			$desexed = $_GET['desexed'];
			$microchipped = $_GET['microchipped'];
			$suburb = $_GET['suburb'];
			$pound_area = $_GET['pound_area'];
			$contact = $_GET['contact'];
			$link = $_GET['link'];
			$columns = $_GET['columns'];
			$find = $_GET['find'];
			$searching = $_GET['searching'];


	// We perform a bit of filtering 
	   	  	$find = strtoupper($find); 
			$find = strip_tags($find); 
			$find = trim ($find); 
	
	/* 
	   Here we're going to get the total number of rows in the database to calculate how many pages of data there are depending on the search terms
	*/
	$query = "SELECT COUNT(*) as num FROM $tbl_name WHERE MATCH(animalID, status, date, species, breed, sex, primary_colour, colour, distinctive_traits, fur_length, age, desexed, microchipped, suburb, pound_area, contact, link, notes)
AGAINST('$find' IN BOOLEAN MODE)";

	$total_pages = mysql_fetch_array(mysql_query($query));
	$total_pages = $total_pages[num];
		
	/* Setup vars for query. */
	$targetpage = "process_search.php"; 	//your file name  (the name of this file)
	$limit = 1; 								//how many items to show per page
	$page = $_GET['page'];
	if($page) 
		$start = ($page - 1) * $limit; 			//first item to display on this page
	else
		$start = 0;								//if no page var is given, set start to 0
	
	/* Get data. */
	$sql = "SELECT * from $tbl_name where MATCH(animalID, status, date, species, breed, sex, primary_colour, colour, distinctive_traits, fur_length, age, desexed, microchipped, suburb, pound_area, contact, link, notes)
AGAINST('$find' IN BOOLEAN MODE) LIMIT $start, 1";
	$result = mysql_query($sql);
	
	/* Setup page vars for display. */
	if ($page == 0) $page = 1;					//if no page var is given, default to 1.
	$prev = $page - 1;							//previous page is page - 1
	$next = $page + 1;							//next page is page + 1
	$lastpage = ceil($total_pages/$limit);		//lastpage is = total pages / items per page, rounded up.
	$lpm1 = $lastpage - 1;						//last page minus 1
	
	/* 
		Now we apply our rules and draw the pagination object. 
		We're actually saving the code to a variable in case we want to draw it more than once.
	*/
	$pagination = "";
	if($lastpage > 1)
	{	
		$pagination .= "<div class=\"pagination\">";
		//previous button
		if ($page > 1) 
			$pagination.= "<a href=\"$targetpage?&find=$find&searching=$searching&search=Search&page=$prev\">< previous</a>";
		else
			$pagination.= "<span class=\"disabled\">< previous</span>";	
		
		//pages	
		if ($lastpage < 7 + ($adjacents * 2))	//not enough pages to bother breaking it up
		{	
			for ($counter = 1; $counter <= $lastpage; $counter++)
			{
				if ($counter == $page)
					$pagination.= "<span class=\"current\">$counter</span>";
				else
					$pagination.= "<a href=\"$targetpage?&find=$find&searching=$searching&search=Search&page=$counter\">$counter</a>";					
			}
		}
		elseif($lastpage > 5 + ($adjacents * 2))	//enough pages to hide some
		{
			//close to beginning; only hide later pages
			if($page < 1 + ($adjacents * 2))		
			{
				for ($counter = 1; $counter < 4 + ($adjacents * 2); $counter++)
				{
					if ($counter == $page)
						$pagination.= "<span class=\"current\">$counter</span>";
					else
						$pagination.= "<a href=\"$targetpage?&find=$find&searching=$searching&search=Search&page=$counter\">$counter</a>";					
				}
				$pagination.= "...";
				$pagination.= "<a href=\"$targetpage?&find=$find&searching=$searching&search=Search&page=$lpm1\">$lpm1</a>";
				$pagination.= "<a href=\"$targetpage?&find=$find&searching=$searching&search=Search&page=$lastpage\">$lastpage</a>";		
			}
			//in middle; hide some front and some back
			elseif($lastpage - ($adjacents * 2) > $page && $page > ($adjacents * 2))
			{
				$pagination.= "<a href=\"$targetpage?&find=$find&searching=$searching&search=Search&page=1\">1</a>";
				$pagination.= "<a href=\"$targetpage?&find=$find&searching=$searching&search=Search&page=2\">2</a>";
				$pagination.= "...";
				for ($counter = $page - $adjacents; $counter <= $page + $adjacents; $counter++)
				{
					if ($counter == $page)
						$pagination.= "<span class=\"current\">$counter</span>";
					else
						$pagination.= "<a href=\"$targetpage?&find=$find&searching=$searching&search=Search&page=$counter\">$counter</a>";					
				}
				$pagination.= "...";
				$pagination.= "<a href=\"$targetpage?&find=$find&searching=$searching&search=Search&page=$lpm1\">$lpm1</a>";
				$pagination.= "<a href=\"$targetpage?&find=$find&searching=$searching&search=Search&page=$lastpage\">$lastpage</a>";		
			}
			//close to end; only hide early pages
			else
			{
				$pagination.= "<a href=\"$targetpage?&find=$find&searching=$searching&search=Search&page=1\">1</a>";
				$pagination.= "<a href=\"$targetpage?&find=$find&searching=$searching&search=Search&page=2\">2</a>";
				$pagination.= "...";
				for ($counter = $lastpage - (2 + ($adjacents * 2)); $counter <= $lastpage; $counter++)
				{
					if ($counter == $page)
						$pagination.= "<span class=\"current\">$counter</span>";
					else
						$pagination.= "<a href=\"$targetpage?&find=$find&searching=$searching&search=Search&page=$counter\">$counter</a>";					
				}
			}
		}
		
		//next button
		if ($page < $counter - 1) 
			$pagination.= "<a href=\"$targetpage?&find=$find&searching=$searching&search=Search&page=$next\">next ></a>";
		else
			$pagination.= "<span class=\"disabled\">next ></span>";
		$pagination.= "</div>\n";		
	}
?>
	<?php
		while($row = mysql_fetch_array($result))
		{
	
		// Your while loop here
//Build the HTML for the results in the table
echo "$resultsHTMLabove";
echo "<br><BR>";
//Here's the table
echo "<table width=\"100%\" border=\"0\" cellspacing=\"5\" cellpadding=\"2\">";
echo "<tr>";
echo "<th scope=\"row\" align=\"left\">Animal ID</th>";
echo "<td>".$row['animalID']."</td>";
echo "</tr>";
echo "<tr>";
echo "<th scope=\"row\" align=\"left\">Status</th>";
echo "<td>".$row['status']."</td>";
echo "</tr>";
echo "<tr>";
echo "<th scope=\"row\" align=\"left\">Date</th>";
echo "<td>".$row['date']."</td>";
echo "</tr>";
echo "<tr>";
echo "<th scope=\"row\" align=\"left\">Species</th>";
echo "<td>".$row['species']."</td>";
echo "</tr>";
echo "<tr>";
echo "<th scope=\"row\" align=\"left\">Breed</th>";
echo "<td>".$row['breed']."</td>";
echo "</tr>";
echo "<tr>";
echo "<th scope=\"row\" align=\"left\">Sex</th>";
echo "<td>".$row['sex']."</td>";
echo "</tr>";
echo "<tr>";
echo "<th scope=\"row\" align=\"left\">Primary Colour</th>";
echo "<td>".$row['primary_colour']."</td>";
echo "</tr>";
echo "<tr>";
echo "<th scope=\"row\" align=\"left\">Colour</th>";
echo "<td>".$row['colour']."</td>";
echo "</tr>";
echo "<tr>";
echo "<th scope=\"row\" align=\"left\">Distinctive Traits</th>";
echo "<td>".$row['distinctive_traits']."</td>";
echo "</tr>";
echo "<tr>";
echo "<th scope=\"row\" align=\"left\">Fur Length</th>";
echo "<td>".$row['fur_length']."</td>";
echo "</tr>";
echo "<tr>";
echo "<th scope=\"row\" align=\"left\">Age</th>";
echo "<td>".$row['age']."</td>";
echo "</tr>";
echo "<tr>";
echo "<th scope=\"row\" align=\"left\">Desexed?</th>";
echo "<td>".$row['desexed']."</td>";
echo "</tr>";
echo "<tr>";
echo "<th scope=\"row\" align=\"left\">Microchipped?</th>";
echo "<td>".$row['microchipped']."</td>";
echo "</tr>";
echo "<tr>";
echo "<th scope=\"row\" align=\"left\">Suburb</th>";
echo "<td>".$row['suburb']."</td>";
echo "</tr>";
echo "<tr>";
echo "<th scope=\"row\" align=\"left\">Pound Area</th>";
echo "<td>".$row['pound_area']."</td>";
echo "</tr>";
echo "<tr>";
echo "<th scope=\"row\" align=\"left\">Contact</th>";
echo "<td>".$row['contact']."</td>";
echo "</tr>";
echo "<tr>";
echo "<th scope=\"row\" align=\"left\">Link</th>";
echo "<td><a href='".$row['link']."' target=_blank >Link to Facebook</a></td>";
echo "</tr>";
echo "<tr>";
echo "<th scope=\"row\" align=\"left\">Notes</th>";
echo "<td>".$row['notes']."</td>";
echo "</tr>";
echo "<tr>";
echo "<th scope=\"row\" align=\"left\">Photo</th>";
echo "<td>";
echo "<img src=\"getimage.php?id=".$row['ID']."\" width=\"200px\" height=\"150px\" />";
echo "</td>";
echo "</tr>";
echo "</table>";
echo "<BR><BR>";
//And we remind them what they searched for 
echo "<b>You searched for:</b>".$find; 
echo $pagination;
echo "</div>";
//Build the footer part of the page
echo "$resultsHTMLbelow";
} 
?>

Thanks, mac_gyver!

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.