Changing the format of a PHP array to allow looping in a smarty tempate
First post - Have been researching and struggling for days but I am unabl to resolve the follow开发者_JAVA技巧ing issue where I have added a second Select query to do some distance calculations from my starting point to destinations within a certain range I am getting the output ok but not in a fromat i can use - I have an existing script that outputs an array and passes this to a smarty template - this works fine so i have used this to debug whats going wrong in my first attemp.
I am using a printf("<pre>%s</pre>", print_r($myarrayname, true));
to debug and have the following output
Array
(
[firm] => company name 1
[loc1] => Inverness
[distance] => 0.800791320271485
)
Array
(
[firm] => company name 2
[loc1] => Inverness
[distance] => 0.972151789782665
)
Array
(
[firm] => company name 3
[loc1] => Inverness
[distance] => 1.04042401681383
)
etc etc however my known working output produces the follow with one index and combines the [firm] and [loc1] together (but ommiting the index name)
Array
(
[html] => company name 1 Inverness
)
Array
(
[html] => company name 2 Inverness
)
Array
(
[html] => company name 3 Inverness
)
Can anyone suggest how I can convert this array to the format shown. Hope there is enough info here - did not want to splater ther page with irrelevent stuff
Here is the Code
// Start of Nearby query
$lat = trim($_REQUEST['lat']);
$lng = trim($_REQUEST['lng']);
$lat = (!empty($lat)? $lat : 57.45666); // starting point Latitude
$lng = (!empty($lng)? $lng : -4.22137);// starting point Longitude
$HelpQuery = sprintf("SELECT firm, loc1, ( 3959 * acos( cos( radians( %s ) ) * cos( radians( lat ) ) * cos( radians( longit ) - radians( %s ) ) + sin( radians( %s ) ) * sin( radians( lat ) ) ) ) AS distance FROM $my_table WHERE state='apr' HAVING distance > 0.01 ORDER BY distance LIMIT 1,5",$lat,$lng,$lat);
$HelpResult = mysql_query($HelpQuery);
$result = array();
$result['html'] = "";
if($HelpResult)
{
while($Helprow = mysql_fetch_assoc($HelpResult))
printf("<pre>%s</pre>", print_r($Helprow, true)); // Prints on 3 lines each one having its own index- needs combiing to one index
{
$Helprow['distance'] = round($Helprow['distance'],2);
$result['html'][] = $Helprow;
}
}
else
{
$result['html'] = "Error";
}
// End of listing nearby MOD
Following the suggestions and pointer I have now resolved this issue with the new code pasted below. The output to display records within a given distance from the source can found here towards the bottom of the page. The source latitude and longitude are pulled onto each page as a variable.
// Start of Nearby Accommodation query
$lat = $vs_current_listing['lat'];
$lng = $vs_current_listing['longit'];
$firm_loop = sprintf("SELECT firm, loc1, id, ROUND( 3959 * acos( cos( radians( %s ) ) * cos( radians( lat ) ) * cos( radians( longit ) - radians( %s ) ) + sin( radians( %s ) ) * sin( radians( lat ) ) ) ,2) AS distance FROM $my_table WHERE state='apr' AND record_set='xxxx' HAVING distance BETWEEN 0.01 and 20 ORDER BY distance LIMIT 1,20",$lat,$lng,$lat);
$firm_loop = mysql_query($firm_loop);
for($x=0;$x<mysql_num_rows($firm_loop);$x++){
$f_img = mysql_fetch_assoc($firm_loop);
$f_firm[mod_firm] = str_replace(" ","_",$f_img[firm]);
$vs_listfirm[$x][html] = "<a href=\"./$f_firm[mod_firm]-$f_img[id].html\" title='$f_img [firm]'>" .$f_img[firm]." - ".$f_img[loc1]." - ".$f_img[distance]." miles </a>";
}
$tpl-> assign('vs_listfirm',$vs_listfirm);
// End of listing nearby MOD
The section of the smarty template displaying the code is shown below, color fonts and formating have been ommited to show basic code.
{html_table_adv loop=$vs_listfirm cols=2 inner=cols table_attr='width=90% border=0 cellpadding=0 cellspacing=1'} [[html]] {/html_table_adv}
精彩评论