Jump to content

Problem with find method (sort and more)


Remi
 Share

Recommended Posts

Hi!

I've got a page with text area where user can check some codes divided by space, semicolon or comma.

I can use two solutions to search for these codes:

First method:

$prCodesArray = preg_split('/[\\s,;]+/', $prCodes, -1);
$selector = "title=" . implode("|", $prCodesArray) . ", template=pr-code, include=all, limit=100";
$prCodePages = $pages->find($selector);
$prCodeCount = count($prCodePages);
if ($prCodeCount) {
	foreach ($prCodePages as $prCodePage) {
		$content .= $prCodePage->title . " - " . "<span title=\"". $prCodePage->parent->pr_code_group_description . "\">" . $prCodePage->parent->title . "</span> - " . $prCodePage->pr_code_description . "<br>";
	}
} else {
	$content .= $prCode . __(' - Not found.') . "<br>";
}

Advantages:
+ it's fast ~0.21s for ~40 codes.

Disadvantages:
- alphabetical results sorting while I want input order,
- doesn't show not found codes.

 

Second method:

$prCodesArray = preg_split('/[\\s,;]+/', $prCodes, -1);
foreach ($prCodesArray as $prCode){
	$selector = "title={$prCode}, template=pr-code, include=all, limit=10";
	$prCodePages = $pages->find($selector);
	$prCodeCount = count($prCodePages);
	if ($prCodeCount) {
		foreach ($prCodePages as $prCodePage) {
			$content .= $prCodePage->title . " - " . "<span title=\"". $prCodePage->parent->pr_code_group_description . "\">" . $prCodePage->parent->title . "</span> - " . $prCodePage->pr_code_description . "<br>";
		}
		else {
			$content .= $prCode . __(' - Not found.') . "<br>";
		}
	}
}

Advantages:
+ sorting like input order,
+ shows not found codes.

Disadvantages:
- it's slow as hell ~2.5s for ~40 codes.

 

Is there any solution to join advantages of both methods?

 

Also, as you can see I have to use include=all statement in my find methods and I don't know why. I've used Import Pages from CSV module. All codes (pages) are published and there is no access control on any of them.

Link to comment
Share on other sites

51 minutes ago, LostKobrakai said:

In your first version you can use ->find() on $prCodePages again to filter by single code in memory. This way you retrieve codes in one sweep from the db and filter them later to get results by single code.

I'll try that :)

It works!

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...