display the search result in view mode

This public forum is for user-to-user discussions of PHPMaker. Note that this is not support forum.
Post Reply
nirvana
User
Posts: 41

display the search result in view mode

Post by nirvana »

Hi there,

I am trying to display the record in view mode if there is only one record in the search result. I'd appreciate if anyone could provide a way to achieve this.

Thank you.


sangnandar
User
Posts: 980

Post by sangnandar »

Interesting case...

I didn't test this code, it might not work but I hope you get the logic.

Page_Redirecting() {
if ($this->TotalRecs == 1) {
$this->id->ListViewValue(); // get record id
// clear session search, so next time you back to this page it will not redirect.
$url = "viewpage.php?id=".$id;
}
}


mobhar
User
Posts: 11747

Post by mobhar »

Try the customized version below:

if ($this->TotalRecs == 1) {
$id = $this->ID->CurrentValue; // get record id, for example: ID
// clear session search, so next time you back to this page it will not redirect.
$this->resetSearchParms();
$this->resetBasicSearchParms();
$this->resetAdvancedSearchParms();
$url = "modelsview.php?ID=".$id; // adjust "models" to your table name
}


nirvana
User
Posts: 41

Post by nirvana »

Hi there,
I couldn't get the view page when there is only one record in the search result by adding the code snippet into the Page_Redirecting event of List Page. It took a long time to load the page but it only displays the search result. I tried printing the URL for the redirect and it prints a long list of URLs in the browser. I haven't gone through the manual in depth regarding the server events. So, I'd appreciate if you could just provide the hint regarding the server events to look into to achieve the result. There is also Page Rendering event for Search Page as well. I also tried Recordset_Selected events of the List Page but still couldn't achieve the result. I tried the following code for the two events:

function Recordset_Selected(&$rs) {
	if( $rs->RecordCount() ==1) {
		$id = $rs->fields("application_number");
		// clear session search, so next time you back to this page it will not redirect.
		$this->resetSearchParms();
		$this->resetBasicSearchParms();
		$this->resetAdvancedSearchParms();
		$url = "_isbn_issued_listview.php?showdetail=&application_number=".$id; 
                    print $url;
		$this->Page_Redirecting($url);
	}
} 

function Page_Redirecting(&$url) {
	if($this->TotalRecs==1)
	{
		$id = $this->application_number->CurrentValue;

		// clear session search, so next time you back to this page it will not redirect.
		$this->resetSearchParms();
		$this->resetBasicSearchParms();
		$this->resetAdvancedSearchParms();
		$url = "_isbn_issued_listview.php?showdetail=&application_number=".$id; 
		//print $url;
		$this->Page_Redirecting($url);
		exit;
	}

}

nirvana
User
Posts: 41

Post by nirvana »

This is just to post the finding regarding the lots of output while printing url during debugging and it was because of the recursive call to the same Page_Redirecting() function.

I achieved the intended result by using the header function but I am not sure this is the right approach for redirecting the page. Please provide the suggestions regarding the server events and better approach for redirecting the page to achieve the intended result. I used the following code in the Page_Redirecting event.

function Page_Redirecting(&$url) {
if($this->TotalRecs==1)
{
$id = $this->application_number->CurrentValue;

		// clear session search, so next time you back to this page it will not redirect.
		$this->resetSearchParms();
		$this->resetBasicSearchParms();
		$this->resetAdvancedSearchParms();
		$url = "_isbn_issued_listview.php?showdetail=&application_number=".$id; 
		//print $url;
		//$this->Page_Redirecting($url);
		header("Location: $url");
		exit;
	}

}

sangnandar
User
Posts: 980

Post by sangnandar »

Well, this line
$id = $this->application_number->CurrentValue;
is kinda hacky.

That's why,
nirvana wrote:
I tried printing the URL for the redirect and it prints a long list of URLs in the
browser.
is happening and $this->Page_Redirecting($url) loaded with those long URL.
Then you have to forced it with header().


Perhaps,
$sql = "select id from (".$this->ListSQL().") as x";
$id = ExecuteScalar($sql);


nirvana
User
Posts: 41

Post by nirvana »

sangnandar wrote:
Well, this line
$id = $this->application_number->CurrentValue;
is kinda hacky.

since the statement is within the condition, it will get the id from the single record, isn't it?

That's why,
nirvana wrote:
I tried printing the URL for the redirect and it prints a long list of URLs in
the
browser.
is happening and $this->Page_Redirecting($url) loaded with those long URL.
Then you have to forced it with header().

Earlier there was a recursive function call which slowed down because of infinite calls, so I had to replace it with header.


Perhaps,
$sql = "select id from (".$this->ListSQL().") as x";
$id = ExecuteScalar($sql);

This seems a quick solution. I will try it. Thank you for your input!


mobhar
User
Posts: 11747

Post by mobhar »

I tried the customized code that I gave you above in the demo project, and it works properly.


nirvana
User
Posts: 41

Post by nirvana »

mobhar wrote:
I tried the customized code that I gave you above in the demo project, and
it works properly.
Sure, mobhar! I will try your code to check the result. Thank you again for your inputs.


nirvana
User
Posts: 41

Post by nirvana »

mobhar wrote:
I tried the customized code that I gave you above in the demo project, and
it works properly.

I had implemented your code including the idea of sangnandar in the Page_Redirecting function on the Class of list page. The idea works but the pager is not shown in the view mode although resets are being performed in the code. Would you like to share your idea about how it can be fixed? Thank you. The code that I have used is provided below.

// Page Redirecting event
function Page_Redirecting(&$url) {
if($this->TotalRecs==1)
{
$id = $this->application_number->CurrentValue;

	// clear session search, so next time you back to this page it will not redirect.
	$this->resetSearchParms();
	$this->resetBasicSearchParms();
	$this->resetAdvancedSearchParms();
	$url = "_isbn_issued_listview.php?showdetail=&application_number=".$id;
	header("Location: $url");
	exit;
}

}


nirvana
User
Posts: 41

Post by nirvana »

Hi there,

I checked the code in the view page and the following condition is restricting the display of pager in the view mode whenever there is a single record in the search result. The code "$_isbn_issued_list_view->Pager->Visible" is false whenever there is a single result.

So, I added the following code after the instantiation of the Pager object and it works well now.

$_isbn_issued_list_view->Pager->Visible = true;


mobhar
User
Posts: 11747

Post by mobhar »

Just use my code above, since you use it inside the Page_Redirecting server event. In other words, no need to use the following ones (just remove it):

header("Location: $url");
exit;


Post Reply