Hey there,
I am building a store finder in Evolution and have created a custom table that contains the latitudes and longitudes of all the stores. I have created a modx document for each store (so that template variables for each one can be edited easily).
Each one of my lat/long rows also contains a corresponding docid for the corresponding store.
Now I want to find the 10 stores nearest me, so I search the lat/long table which spits out the top 10 docids. The question is, how can I use this to output the data in the modx documents.
Can I use the getDocuments API? What’s the best option?
Any help would be mightily appreciated.
Thanks
The easy way is to use Ditto with the ditto-parameter
&documents=`[[getDocidsOfNearestStores_commaseperated]]`
or use
$params['documents']=$DocidsOfNearestStores_commaseperated;
$output=$modx->runSnippet('Ditto',$params);
in your snippet.
of course you can also use
$docs = $modx->getDocuments($DocidsOfNearestStores_asArray, 1, 0,$fields);
in case you need tvs you can get them with
$modx->getTemplateVarOutput($idnames= array (), $docid= "", $published= 1)
in a loop.
I think its better to collect first all datas into an array, then run ditto once after your loop:
<?php
$docids=array();
foreach($stores AS $store) {
$modx->setPlaceholder($store['docid'].'.distance',number_format($store['distance'],2));
$docids[]=$store['docid'];
}
$params=array();
$params['documents']=implode(',',$docids);
$params['tpl']='listtpl';
return $modx->runSnippet('Ditto',$params);
?>
in your template:
ditto-sorting is normally createdon. If you want your sorting as in &documents you need my docsort-extender.
See here:
http://modxcms.com/forums/index.php/topic,38711.msg233570.html#msg233570
Well I was hoping to bypass Ditto sorting altogether by using the SQL query above.
I want to order them by distance, with the nearest one at the top.
Another problem has arisen. It seems that the first distance calculation is skipped by the loop. Any ideas what this might be?
Thanks
Well I was hoping to bypass Ditto sorting altogether by using the SQL query above.
with my extender you cannot bypass dittos own sorting, but it resorts the documents as they came from parameter &documents.
It seems that the first distance calculation is skipped by the loop. Any ideas what this might be?
in your or in my solution? You can try to change the order of your codelines and set the placeholder before calling ditto:
foreach($stores AS $store) {
$modx->setPlaceholder('distance',number_format($store['distance'],2));
$params['documents']=$store['docid'].',';
$params['tpl']='listtpl';
$output=$modx->runSnippet('Ditto',$params);
echo $output;
Changing the order of the code lines worked (I was trying my solution). Thanks.
However, I would much prefer to try your solution, as I tested them against each other:
Mine - 186 queries
Yours - 9 queries
No contest really.
Applied your customsort extender and everything seems to work great! Vielen dank für deine Hilfe.