Jump to content

sort as queried


martind
 Share

Recommended Posts

hi.

i have a selector like this:

$result = wire('pages')->find("template=xx, field_x=439|417|456|402");

now i need the $result sorted exactly like the values are commited in field_x. How to do this?

thanks, martin

Link to comment
Share on other sites

Hm, tricky question...

maybe like this?

$result = wire('pages')->find("template=xx, field_x=439");
$result = $result->import(wire('pages')->find("template=xx, field_x=417"));
$result = $result->import(wire('pages')->find("template=xx, field_x=456"));
$result = $result->import(wire('pages')->find("template=xx, field_x=402"));
Link to comment
Share on other sites

thank you, seems promising. Now i`m struggling with dynamically build the query. this won`t work as one might wish:

$q = "wire('pages')->find";
$count = 0;
foreach($v_array AS $v)
{
    if($count === 0) $q .= '("template=xx, field_x='.$v.'")';  
    if($count > 0) $q .= '.append("template=xx, field_x='.$v.'")';
    $count=$count+1;            
}
$result = $q; //unfortunately empty result while $q as string seems ok
Link to comment
Share on other sites

thank you both, i could solve it with diogos advice, i think my attempt above wrecked more on my poor php understanding. this way it works perfect:

$count = 0;
foreach($v_array AS $v)
{
    if($count === 0) $result = wire('pages')->find("template=xx, field_x=$v");  
    if($count > 0) $result = $result->import(wire('pages')->find("template=xx, film_id=$v"));
    $count=$count+1;            
}
Link to comment
Share on other sites

You can simplify it like this

$result = new PageArray();

foreach($v_array AS $v)
{
    $result->import(wire('pages')->find("template=xx, film_id=$v"));      
}
Link to comment
Share on other sites

The disadvantage of this solution is that it requires 4 DB queries. As long as there is only a small amount of data, this should be fine. How about this:

$ids = array(439,417,456,402);
$result = new PageArray();
$p = wire('pages')->find("template=xx, field_x=439|417|456|402");
foreach ($ids as $id) {
  $result->append($p->get("field_x=$id"));
}

Needs only one query :)

  • Like 2
Link to comment
Share on other sites

Yep, good one Wanze. But for what I understood there are more than one page for each id. In that case append should still be import, and get should be find.

Link to comment
Share on other sites

  • 2 years later...

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