Keeping the selected option to any pages during pagination.

Ask about general coding issues or problems here.

Moderators: egami, macek, gesf

Post Reply
alfred5063
New php-forum User
New php-forum User
Posts: 1
Joined: Sat Mar 18, 2017 10:58 pm

Sat Mar 18, 2017 11:17 pm

Dear respected programmers.
I am currently developing a database. On the search page, I have few dropdown menus for search. One of them has the code as below:

Code: Select all

	$keyword = "project";
	$results = mysqli_query($conn, "SHOW TABLES LIKE '%" .stripslashes($keyword). "%'");
	if(mysqli_num_rows($results))
	{
		$select='<select name="select" id="select">';
		while ($row = mysqli_fetch_array($results))
		{
			$select.='<option value="'.$row[0].'">'.$row[0].'</option>';
		}
		
		$select.='</select>';
	}
	echo $select;
[/i]
So what I am trying to do here is that, the options come from my MySQL database. Whenever a user creates a new project, the name of the project will be listed in this option function. It works well to parse the value='select' to another variable. I am using this variable to query a project in order to get two rows I need. The following codes explain the thing I wanted to get:

Code: Select all

[i]	// Get the annotation file from relevent project
	$selected_project = $_REQUEST['select'];
	$project = mysqli_query($conn, "SELECT * FROM $selected_project");
	while($row = mysqli_fetch_array($project))
	{
		$projectcode = $row['projectcode'];
		$projectname = $row['projectname'];
	}	
	$table_name = "$projectcode"."_annotation";[/i]
I need the value for $table_name to be used to perform another MySQL query as below:

Code: Select all

	$sql = mysqli_query($conn, "SELECT DISTINCT $column_id, $column_pfam, $column_gopfam FROM $table_name WHERE $column2 LIKE '%" .stripslashes($keyword). "%'");
[/i]

So what is my problem?
I can select different options and upon clicking the "submit" button, able to show the queried results that I wanted. To maneuver these hits, I have used the following codes to do the pagination:


Code: Select all

if($pagenum < 1) {
	}
	else{
		echo " <a href='{$_SERVER['PHP_SELF']}?project=".$selected_project."&search=".$column2."&db=".$table_name."&keyword=".$keyword."&limit=".$page_rows."&pagenum=1'>First</a> ";
		echo " | ";
		$previous = $pagenum-1;
		echo "<a href='{$_SERVER['PHP_SELF']}?project=".$selected_project."&search=".$column2."&db=".$table_name."&keyword=".$keyword."&limit=".$page_rows."&pagenum=".$previous."'>Previous</a> ";
		echo " | ";
	} 
	
	//This does the same as above, only checking if we are on the last page, and then generating the Next and Last links
	if ($pagenum == $last){
	}
	else{
		$next = $pagenum+1;
		echo "<a href='{$_SERVER['PHP_SELF']}?project=".$selected_project."&search=".$column2."&db=".$table_name."&keyword=".$keyword."&limit=".$page_rows."&pagenum=".$next."'>Next</a> ";
		echo " | ";
		echo " <a href='{$_SERVER['PHP_SELF']}?project=".$selected_project."&search=".$column2."&db=".$table_name."&keyword=".$keyword."&limit=".$page_rows."&pagenum=".$last."'>Last</a> ";
		echo " | ";
	}
[/i]

However, clicking "Next" and/or "Last" will return a blank page. To make things more interesting, after clicking "Next", and the web address is as below:

Code: Select all

http://localhost/web/testdb/test2.php?project=project_bacrna&search=gene_id&db=BACRNA_annotation&keyword=&limit=10&pagenum=2
will ONLY return and show the results of a second page IF I were to click the "submit" button once again.



I need some help to tweak this issue.
Help!

Thanks in advance.
alfred5063.

Post Reply