Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> [PHP] problem ze wstawieniem if-a
lagotek
post
Post #1





Grupa: Zarejestrowani
Postów: 14
Pomógł: 0
Dołączył: 5.03.2010

Ostrzeżenie: (0%)
-----


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źć?


  1. <?php
  2. class AjaxFunctions{
  3.  
  4. function returnSearchResults(){
  5. //echo var_export($_POST, true);
  6. $options= get_option('aphs_FYN_options');
  7. $Distance_Units=$options['Distance_Units'];
  8. $Display_Results=$options['Display_Results'];
  9. $lat1=$_POST['lat'];
  10. $lng1=$_POST['lng'];
  11. $args=array(
  12. 'post_type' => 'find_your_nearest',
  13. 'posts_per_page' => '-1'
  14. );
  15.  
  16. $the_query = new WP_Query( $args );
  17.  
  18. $results=array();
  19. while ( $the_query->have_posts() ) : $the_query->the_post();
  20. $lng2=get_post_meta (get_the_ID(), '_aphs_FYN_longitude', TRUE );
  21. $lat2=get_post_meta (get_the_ID(), '_aphs_FYN_latitude', TRUE );
  22. //calculate distance from
  23. if (is_numeric($lng2) && is_numeric($lat2)) {
  24. $theta = $lng1 - $lng2;
  25. $dist = sin(deg2rad($lat1)) * sin(deg2rad($lat2)) + cos(deg2rad($lat1)) * cos(deg2rad($lat2)) * cos(deg2rad($theta));
  26. $dist = acos($dist);
  27. $dist = rad2deg($dist);
  28. if ($dist >= 1) {
  29. $dist = 1;
  30. }
  31. if ($dist <= -1) {
  32. $dist = -1;
  33. }
  34.  
  35.  
  36. $miles = $dist * 60 * 1.1515;
  37. $content = get_the_content();
  38. $content = nl2br($content);
  39. $content = str_replace('><br />', '>', $content);
  40. $title=get_the_title();
  41. switch ($Distance_Units) {
  42. 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>";
  43. break;
  44. 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>";
  45. break;
  46. default:$results["$miles"][$title]="<h2>$title</h2> <span class='FYN_viewmap' style='cursor: pointer' id='".get_the_ID()."'>view map</span><p>".$content."</p>";
  47. break;
  48. }
  49. }
  50. endwhile;
  51.  
  52. wp_reset_postdata();
  53. ksort($results);
  54. if (count($results)>0) {
  55. if ($Display_Results!=0 AND $Display_Results<count($results)) {
  56. $results = array_slice($results, 0, $Display_Results);
  57. }
  58. foreach ($results as $distance=>$content) {
  59.  
  60. ksort($content);
  61. foreach ($content as $item) {
  62. echo $item;
  63. }
  64. }
  65. }
  66. else {
  67. return "Your Search Criteria Returned No Results";
  68. }
  69. die();
  70. }
  71.  
  72.  
  73. }



Napisze tez od razu co probowalem, mianowicie cos takiego :


  1. foreach ($results as $distance=>$content) {
  2. ksort($content);
  3. foreach ($content as $item) {
  4. if($miles < 20)
  5. {
  6. echo $item;
  7. return;
  8. }
  9. else
  10. {
  11. return "Your Search Criteria Returned No Results";
  12. }
  13. }


Jednakże wciąz nie działa poprawnie skrypt, gdzie popelnilem blad?
Go to the top of the page
+Quote Post

Posty w temacie


Reply to this topicStart new topic
2 Użytkowników czyta ten temat (2 Gości i 0 Anonimowych użytkowników)
0 Zarejestrowanych:

 



RSS Aktualny czas: 8.08.2026 - 08:04