Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

 
Reply to this topicStart new topic
> [PHP] problem ze wstawieniem if-a
lagotek
post 7.05.2017, 16:28:03
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 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?


--------------------
konkursy
gry mmo
Go to the top of the page
+Quote Post
vokiel
post 7.05.2017, 16:32:45
Post #2





Grupa: Zarejestrowani
Postów: 2 592
Pomógł: 445
Dołączył: 12.03.2007

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


return nie przerywa pętli, robi to break


--------------------
Go to the top of the page
+Quote Post
lagotek
post 7.05.2017, 16:46:16
Post #3





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

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


Ah faktycznie my bad, dziękuję

Jednakże nie zmienia to faktu że ustawiając $miles < 5, gdzie odleglosc pomiedzy dwoma punktami to maksymalnie 1 mila wypluwa cały czas "Your Search Criteria Returned No Results"

Ktos mi powie dlaczego ?


--------------------
konkursy
gry mmo
Go to the top of the page
+Quote Post
vokiel
post 8.05.2017, 08:11:09
Post #4





Grupa: Zarejestrowani
Postów: 2 592
Pomógł: 445
Dołączył: 12.03.2007

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


$miles (linia 36) masz w pętli while (linia 19), później sprawdzasz te $miles w pętli foreach porównując do stałej liczby, co nie ma sensu, bo $miles będzie ostatnią wartością z pętli while.

Więc chyba zamiast $miles powinieneś sprawdzać $distance, albo coś z $item


--------------------
Go to the top of the page
+Quote Post

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

 



RSS Wersja Lo-Fi Aktualny czas: 26.04.2024 - 04:07