Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> Tworzenie warunku w jednej lini przy pomocy petli
rad11
post
Post #1





Grupa: Zarejestrowani
Postów: 1 270
Pomógł: 184
Dołączył: 7.10.2012
Skąd: Warszawa

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


Mając taka talice:

  1. array(5) {
  2. [0]=>
  3. array(3) {
  4. ["where"]=>
  5. string(7) "a_price"
  6. ["condition"]=>
  7. string(2) ">="
  8. ["match"]=>
  9. int(123213)
  10. }
  11. [1]=>
  12. array(3) {
  13. ["where"]=>
  14. string(7) "a_price"
  15. ["condition"]=>
  16. string(2) "<="
  17. ["match"]=>
  18. int(123)
  19. }
  20. [2]=>
  21. array(3) {
  22. ["where"]=>
  23. string(7) "a_price"
  24. ["condition"]=>
  25. string(1) ">"
  26. ["match"]=>
  27. int(5466)
  28. }
  29. [3]=>
  30. array(3) {
  31. ["where"]=>
  32. string(7) "a_price"
  33. ["condition"]=>
  34. string(1) "="
  35. ["match"]=>
  36. int(87697)
  37. }
  38. [4]=>
  39. array(3) {
  40. ["where"]=>
  41. string(7) "a_price"
  42. ["condition"]=>
  43. string(1) "<"
  44. ["match"]=>
  45. int(14134)
  46. }
  47. }
  48.  


Czy jest mozliwe utworzenie przy uzyciu pętli z tej tablicy warunku w jednej linit tzn:
  1. if(a_price < 14134 && a_price == 87697 && a_price > 5466 && a_price <= 123 && a_price >= 123213){
  2.  
  3. }


Ten post edytował rad11 4.12.2015, 08:07:48
Go to the top of the page
+Quote Post
 
Start new topic
Odpowiedzi (1 - 3)
Tomplus
post
Post #2





Grupa: Zarejestrowani
Postów: 1 879
Pomógł: 230
Dołączył: 20.03.2005
Skąd: Będzin

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


Warunek jest dobry. Ale będzie spełniony tylko dla jednego z elementów tablicy, jeżeli chcesz dla wszystkich to:
a) musisz znać ilość elementów i samemu stworzyć coś w stylu:

  1. if($arr[4][a_price] < 14134 && $arr[3][a_price] == 87697 && $arr[2][a_price] > 5466 && $arr[1][a_price]<= 123 && $arr[0][a_price]>= 123213){


lub stworzyć sobie funkcję zawierającą warunek:

  1. function a_price_tomore ($a_price) {
  2. if ($a_price > 0) return true;
  3. }
  4. function a_price_toless ($a_price) {
  5. if ($a_price < 0) return true;
  6. }
  7. function a_price_equal ($a_price) {
  8. if ($a_price == 0) return true;
  9. }
  10.  
  11. //potem
  12. $warunek = false;
  13. foreach ($arr as $k=>$key) {
  14. if ($key['condidion'] == '>') a_price_tomore ($key['a_price']) $warunek = true;
  15. elseif ($key['condidion'] == '<') a_price_toless ($key['a_price']) $warunek = true;
  16. elseif ($key['condidion'] == '=') a_price_equal ($key['a_price']) $warunek = true;
  17. else $warunek = false;
  18. }
  19. if ($warunek === true ) echo "Warunek spełniony";



Nie wiem czy czegoś nie spieprzyłem, ale m/w w podobny sposób, acz ja osobiście zrobiłbym jeszcze inaczej.
Go to the top of the page
+Quote Post
rad11
post
Post #3





Grupa: Zarejestrowani
Postów: 1 270
Pomógł: 184
Dołączył: 7.10.2012
Skąd: Warszawa

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


A jeżeli będę chciął w fuckji dodać opcje && lub || ?

Niestety musiałem sobie poradzić przy pomocy
  1. eval()


Jak by ktoś potrzebował:

  1. public function getWhere() {
  2. $this->condition = array();
  3. foreach ($this->where as $whereIndex => $whereValue) {
  4. if ($whereIndex == (count($this->where) - 1)) {
  5. unset($this->where[$whereIndex]["mode"]);
  6. }
  7. $this->mode = "";
  8. switch (strtolower($whereValue['condition'])):
  9. case "in":
  10. foreach ($whereValue['match'] as $matchIndex => $matchValue) {
  11. $this->conditionIn[] = "\$dataValue['{$whereValue['where']}'] == {$matchValue}";
  12. }
  13. if (isset($this->where[$whereIndex]['mode'])) {
  14. $this->mode = (trim(strtolower($this->where[$whereIndex]['mode'])) == "or") ? ' ||' : ' &&';
  15. }
  16. $this->condition[] = implode(' || ', $this->conditionIn) . " " . $this->mode;
  17.  
  18. break;
  19. case "like":
  20. $this->condition[] = $this->getLike($whereIndex);
  21. break;
  22. case "=":
  23. $this->condition[] = $this->getEqual($whereIndex);
  24. break;
  25. case "<":
  26. $this->condition[] = $this->getLower($whereIndex);
  27. break;
  28. case ">":
  29. $this->condition[] = $this->getGrather($whereIndex);
  30. break;
  31. case ">=":
  32. $this->condition[] = $this->getGratherEqual($whereIndex);
  33. break;
  34. case "<=":
  35. $this->condition[] = $this->getLowerEqual($whereIndex);
  36. break;
  37. case ">=<":
  38. $this->condition[] = $this->getBetweenEqual($whereIndex);
  39. break;
  40. case "><":
  41. $this->condition[] = $this->getBetweenWithOutEqual($whereIndex);
  42. break;
  43. endswitch;
  44. }
  45. $this->condition = implode(" ", $this->condition);
  46. foreach ($this->tData as $dataIndex => $dataValue) {
  47. eval("\$evalCondition = $this->condition;");
  48. if ($evalCondition) {
  49. $this->data[] = $dataValue;
  50. }
  51. }
  52. return $this;
  53. }


Przykładowa metoda z eval:
  1. public function getLike($index) {
  2.  
  3. if (isset($this->where[$index]['mode'])) {
  4. $this->mode = (trim(strtolower($this->where[$index]['mode'])) == "or") ? ' ||' : ' &&';
  5. }
  6.  
  7. return "strpos((string)\$dataValue['{$this->where[$index]['where']}'], (string){$this->where[$index]['match']})";
  8. }


Ten post edytował rad11 4.12.2015, 12:06:46
Go to the top of the page
+Quote Post
NickOver
post
Post #4





Grupa: Zarejestrowani
Postów: 332
Pomógł: 10
Dołączył: 13.03.2014
Skąd: Bydgoszcz

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


array(3) {
["where"]=>
string(7) "a_price"
["condition"]=>
string(2) ">="
["match"]=>
int(123213)
}
A nie łatwiej:
  1. $queryString;
  2. $i = 1;
  3. foreach ($xyz as $array){
  4. where
  5. if($i > 1){
  6. $queryString.= " && ".$key['$value']['where'].".&& $key['$value']['condition']." ".&& $key['$value']['match'];
  7. } else {
  8. $queryString.= .if($key['$value']['where'].".&& $key['$value']['condition']." ".&& $key['$value']['match'];
  9. }
  10. $i++;
  11. }
  12. $queryString.= "){";
  13. eval($queryString);
  14. //kod
  15. }

To powinno działać.
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 Aktualny czas: 19.08.2025 - 16:12