Jump to content

Need help with mutiple queries being integrated into paging


GL

Recommended Posts

Hi there, Currently I am having problems merging Multiple Queries with the IF ELSE condition into paging. The problem I always get is that the first page is being displayed correctly, but if I move on to the 2nd or the previous page, the results seems to be messed up regardless of the sorting that I have implemented into the query. However, should I remove the IF ELSE condition and use only a single query, this code is workable. Can anyone please help me out with this problem? Much help is appreciated.

 

<?php
include 'config.php';
include 'opendb.php';
include 'function.php';

showindexhyperlink();

$Search = $_POST ['Search'];
//echo "$Search"; //Proves that Retreival of Query is Operational
$SearchSelect = $_POST ['SearchSelect'];
//echo "$SearchSelect"; //Proves that Selection is Operational

	$rowsPerPage = 10; //Variable for how many rows to display per page
	$pageNum = 1; //This is the first page

	// If this is defined, use it as page number.
	if(isset($_GET['page']))
	{
	$pageNum = $_GET['page'];
	}

	//Counting the offset
	$offset = ($pageNum - 1) * $rowsPerPage;

	if ($SearchSelect == 1){
				$query = "SELECT * FROM Asset WHERE Asset_ID LIKE '%$Search%'
							OR Name LIKE '%$Search%'
							OR Description LIKE '%$Search%'
							OR Quantity LIKE '%$Search%'
							OR Location LIKE '%$Search%'
							OR Cost LIKE '%$Search%'
							OR FailureCons LIKE '%$Search%'
							LIMIT $offset, $rowsPerPage";
							$result = mysql_query($query) or die;}

	elseif ($SearchSelect == 2){
				$query = "SELECT * FROM Asset WHERE Quantity LIKE '%$Search'
							ORDER BY Quantity DESC
							LIMIT $offset, $rowsPerPage";
							$result = mysql_query($query) or die;}

	elseif ($SearchSelect == 3){
				$query = "SELECT * FROM Asset WHERE Name LIKE '%$Search%'
							ORDER BY Name ASC
							LIMIT $offset, $rowsPerPage";
							$result = mysql_query($query) or die;} // Searches the Name in the Database

	elseif ($SearchSelect == 4){
				if ($Search == ''){
				$query = "SELECT * FROM Asset
							ORDER BY Asset_ID ASC
							LIMIT $offset, $rowsPerPage";
							$result = mysql_query($query) or die;}

				else{
				$query = "SELECT * FROM Asset WHERE Asset_ID = '$Search'
							ORDER BY Asset_ID ASC
							LIMIT $offset, $rowsPerPage";
							$result = mysql_query($query) or die;} // If Query has a value, shows the ID
								}

	else {$query = "SELECT * FROM Asset WHERE Location LIKE '%$Search%'
							ORDER BY Location ASC
							LIMIT $offset, $rowsPerPage";
							$result = mysql_query($query) or die;} // Searches Locations in the database

		$numrows = mysql_num_rows($result); //Calculates the total amount of rows the query returned
		//echo "$numrows"; //To test out if the Query is operational

		if ($numrows == 0){echo "No results found, please try again";}else{

		ResultTable();//Prints our the Header for Result Table


		//while loop condition to loop out all the results from the query
		while($row = mysql_fetch_array($result))
			{	
				$Asset_ID = $row['Asset_ID'];
				$Name = $row['Name'];
				$Description = $row['Description'];
				$Quantity = $row['Quantity'];
				$Location = $row['Location'];
				$Image = $row['Image'];
				$Cost = $row['Cost'];
				$FailureCons = $row['FailureCons'];

				echo "<tr>";
				echo "<td>".$Asset_ID."</td>";
				echo "<td>".$Name."</td>";
				echo "<td width = 20%>".$Description."</td>";
				echo "<td>".$Quantity."</td>";
				echo "<td>".$Location."</td>";
				echo "<td><img src= '$Image' height = 80 width = 80></td>";
				echo "<td>".$Cost."</td>";
				echo "<td width = 20%>".$FailureCons."</td>";
				echo "</tr>";
			}
		}

	$query = "SELECT Asset_ID FROM Asset"; //Select all Rows from Asset Table
	$result = mysql_query($query) or die; //Execute Query
	$TableRows = mysql_num_rows($result); // Count the total amount of rows in database

	$maxPage = ceil($TableRows/$rowsPerPage);//Calculates how many pages to use when paging
	$self = $_SERVER['PHP_SELF'];

		$nav = '';
for($page = 1; $page <= $maxPage; $page++)
	{
		if ($page == $pageNum)
		{
			$nav .= " $page ";   // no need to create a link to current page
		}
		else
		{
			$nav .= " <a href=\"$self?page=$page\">$page</a> ";
		}
	}

if ($pageNum > 1)
	{
		$page = $pageNum - 1;// Defines which page we are in
		$prev = " <a href=\"$self?page=$page\">[Prev]</a> ";//Creates the Previous Link
		$first = " <a href=\"$self?page=1\">[First Page]</a> ";//Creates the First Page Link
	}
	else
	{
		$prev  = ' [Prev] ';       // If on 1st Page, disable the link
		$first = ' [First Page] '; // As Above
	}

if ($pageNum < $maxPage)
	{
		$page = $pageNum + 1;//Calculates next page
		$next = " <a href=\"$self?page=$page\">[Next]</a> ";//Link to next page
		$last = " <a href=\"$self?page=$maxPage\">[Last Page]</a> ";//Link to Last Page
	}
	else
	{
		$next = ' [Next] ';      // If on Last page, disable link
		$last = ' [Last Page] '; //As Above
	}
	// Navigation Link
	echo $first . $prev . " Showing page <b>$pageNum</b> of <b>$maxPage</b> pages " . $next . $last;

include 'closedb.php';
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.