Witam serdecznie,
Od razu zaznacze że jestem bardzo początkujący w PHP (IMG:
style_emoticons/default/smile.gif) Utknąłem przy przerabianiu skryptu który na celu ma wyswietlac post bedacy najblizej wyznaczonego adresu. Kazdy post ma oczywiscie swoj przypisany adres.
Chciałem to zrobić tak zeby wyswietlany byl tylko jeden, jesli spelniony jest warunek $miles<=10 a w przeciwnym wypadku "wypluwany" tekst. Niestety nie działa tutaj wrzucenie if-a w foreach przed wyswietleniem juz wyniku. Czy macie jakieś pomysły jak to ugryźć?
<?php
class AjaxFunctions{
function returnSearchResults(){
//echo var_export($_POST, true);
$options= get_option('aphs_FYN_options');
$Distance_Units=$options['Distance_Units'];
$Display_Results=$options['Display_Results'];
$lat1=$_POST['lat'];
$lng1=$_POST['lng'];
'post_type' => 'find_your_nearest',
'posts_per_page' => '-1'
);
$the_query = new WP_Query( $args );
while ( $the_query->have_posts() ) : $the_query->the_post();
$lng2=get_post_meta (get_the_ID(), '_aphs_FYN_longitude', TRUE );
$lat2=get_post_meta (get_the_ID(), '_aphs_FYN_latitude', TRUE );
//calculate distance from
$theta = $lng1 - $lng2;
$dist = sin(deg2rad($lat1)) * sin(deg2rad($lat2)) + cos(deg2rad($lat1)) * cos(deg2rad($lat2)) * cos(deg2rad($theta));
$dist = acos($dist);
$dist = rad2deg($dist);
if ($dist >= 1) {
$dist = 1;
}
if ($dist <= -1) {
$dist = -1;
}
$miles = $dist * 60 * 1.1515;
$content = get_the_content();
$content = nl2br($content); $title=get_the_title();
switch ($Distance_Units) {
case "miles":$results["$miles"][$title]="<h2>$title</h2><em>Twoja odleglosc od lokalu wynosi ".round($miles)." miles</em> <span class='FYN_viewmap' style='cursor: pointer' id='".get_the_ID
()."'>view map</span><p>".$content."</p>"; break;
case "kilometres":$results["$miles"][$title]="<h2>$title</h2><em>Twoja odleglosc od lokalu wynosi ".round($miles * 1.609344)."km</em> <span class='FYN_viewmap' style='cursor: pointer' id='".get_the_ID
()."'>view map</span><p>".$content."</p>"; break;
default:$results["$miles"][$title]="<h2>$title</h2> <span class='FYN_viewmap' style='cursor: pointer' id='".get_the_ID()."'>view map</span><p>".$content."</p>";
break;
}
}
endwhile;
wp_reset_postdata();
if ($Display_Results!=0 AND $Display_Results<count($results)) {
}
foreach ($results as $distance=>$content) {
foreach ($content as $item) {
}
}
}
else {
return "Your Search Criteria Returned No Results";
}
}
}
Napisze tez od razu co probowalem, mianowicie cos takiego :
foreach ($results as $distance=>$content) {
foreach ($content as $item) {
if($miles < 20)
{
return;
}
else
{
return "Your Search Criteria Returned No Results";
}
}
Jednakże wciąz nie działa poprawnie skrypt, gdzie popelnilem blad?