Warning: Cannot use a scalar value as an array in /home/admin/public_html/forum/include/fm.class.php on line 757

Warning: Invalid argument supplied for foreach() in /home/admin/public_html/forum/include/fm.class.php on line 770

Warning: Invalid argument supplied for foreach() in /home/admin/public_html/forum/topic.php on line 737
Форумы портала PHP.SU :: Метод для подсчёта тарифа

 PHP.SU

Программирование на PHP, MySQL и другие веб-технологии
PHP.SU Портал     На главную страницу форума Главная     Помощь Помощь     Поиск Поиск     Поиск Яндекс Поиск Яндекс     Вакансии  Пользователи Пользователи


 Страниц (1): [1]   

> Описание: ООП
pantela
Отправлено: 17 Мая, 2016 - 17:25:08
Post Id



Частый посетитель


Покинул форум
Сообщений всего: 765
Дата рег-ции: Авг. 2010  


Помог: 1 раз(а)




Имею метод "get_all()" каторый выводит список объектов

CODE (htmlphp):
скопировать код в буфер обмена
  1. public function get_all()
  2. {
  3.     $res = DB::select('id', 'price_discount','district_key')
  4.         ->from('object')
  5.         ->order_by('id', 'DESC')
  6.         ->where('status', '=', 1)
  7.         ->as_array();
  8.  
  9.     $res = $this->addDistrictPricesByIDItems($res);
  10.  
  11.     if (!empty($res)) return $res;
  12.     return null;
  13. }


Передаём ключь для вывода массива "district_price"
CODE (htmlphp):
скопировать код в буфер обмена
  1. public function addDistrictPricesByIDItems($items)
  2. {
  3.     foreach($items as &$i)
  4.    {
  5.        $i['district_price'] = $this->getDistrictPricesByID($i['district_key']);
  6.     }
  7.     return $items;
  8. }



Выводим массив "district_price" через "$district_key"
CODE (htmlphp):
скопировать код в буфер обмена
  1. public function getDistrictPricesByID($district_key)
  2. {
  3.     $res =
  4.         DB::select('price_full_day','price_first_half','price_second_half')
  5.         ->from($this->_table_district)
  6.         ->where('id','=',$district_key)
  7.         ->execute()
  8.         ->as_array();
  9.     if (!empty($res)) return $res[0];
  10.     return null;
  11. }



Результат
CODE (htmlphp):
скопировать код в буфер обмена
  1. (
  2.     [0] => Array
  3.         (
  4.             [id] => 30
  5.             [price_discount] => 5
  6.             [district_price] => Array
  7.                 (
  8.                     [price_full_day] => 70
  9.                     [price_first_half] => 30
  10.                     [price_second_half] => 50
  11.                 )
  12.         )
  13.     [1] => Array
  14.         (
  15.             [price_discount] => 3
  16.             [district_price] => Array
  17.                 (
  18.                     [price_full_day] => 70
  19.                     [price_first_half] => 30
  20.                     [price_second_half] => 50
  21.                 )
  22.         )
  23.     [2] => Array
  24.         (
  25.             [price_discount] => 0
  26.             [district_price] => Array
  27.                 (
  28.                     [price_full_day] => 70
  29.                     [price_first_half] => 30
  30.                     [price_second_half] => 50
  31.                 )
  32.         )
  33.     [3] => Array
  34.         (
  35.             [price_discount] => 10
  36.             [district_price] => Array
  37.                 (
  38.                     [price_full_day] => 70
  39.                     [price_first_half] => 30
  40.                     [price_second_half] => 50
  41.                 )
  42.         )
  43. )



Теперь мне надо создать метод "priceCalc()" и вызывать его в методе "get_all()" для того что-бы передавать каждой записи ['district_price'] и ['price_discount'], а уже в методе "priceCalc()" считать значения по формуле:
['district_price'][price_full_day] * [price_discount] / 100
['district_price'][price_first_half] * [price_discount] / 100
['district_price'][price_second_half] * [price_discount] / 100



Витоге массив будет ~ виде:
CODE (htmlphp):
скопировать код в буфер обмена
  1. (
  2.     [0] => Array
  3.         (
  4.             [id] => 30
  5.             [price_discount] => 5
  6.             [district_price] => Array
  7.                 (
  8.                     [price_full_day] => 70
  9.                     [price_first_half] => 30
  10.                     [price_second_half] => 50
  11.                 )
  12.             [price_calc] => Array
  13.                 (
  14.                     [price_full_day] => 3.5
  15.                     [price_first_half] => 1.5
  16.                     [price_second_half] => 2.5
  17.                 )
  18.         )
  19.     [1] => Array
  20.         (
  21.             [price_discount] => 3
  22.             [district_price] => Array
  23.                 (
  24.                     [price_full_day] => 70
  25.                     [price_first_half] => 30
  26.                     [price_second_half] => 50
  27.                 )
  28.             [price_calc] => Array
  29.                 (
  30.                     [price_full_day] => 1.4
  31.                     [price_first_half] => 0.9
  32.                     [price_second_half] => 1.5
  33.                 )
  34.         )
  35.     [2] => Array
  36.         (
  37.             [price_discount] => 0
  38.             [district_price] => Array
  39.                 (
  40.                     [price_full_day] => 70
  41.                     [price_first_half] => 30
  42.                     [price_second_half] => 50
  43.                 )
  44.             [price_calc] => Array
  45.                 (
  46.                     [price_full_day] => 0
  47.                     [price_first_half] => 0
  48.                     [price_second_half] => 0
  49.                 )
  50.         )
  51.     [3] => Array
  52.         (
  53.             [price_discount] => 10
  54.             [district_price] => Array
  55.                 (
  56.                     [price_full_day] => 70
  57.                     [price_first_half] => 30
  58.                     [price_second_half] => 50
  59.                 )
  60.             [price_calc] => Array
  61.                 (
  62.                     [price_full_day] => 7
  63.                     [price_first_half] => 3
  64.                     [price_second_half] => 5
  65.                 )
  66.         )
  67. )


спс.Ув. Закатив глазки
 
 Top
pantela
Отправлено: 18 Мая, 2016 - 09:49:04
Post Id



Частый посетитель


Покинул форум
Сообщений всего: 765
Дата рег-ции: Авг. 2010  


Помог: 1 раз(а)




Растерялся не догоняю как бы сделать... теоритический хотяб дайте совет...
 
 Top
pantela
Отправлено: 25 Мая, 2016 - 02:29:28
Post Id



Частый посетитель


Покинул форум
Сообщений всего: 765
Дата рег-ции: Авг. 2010  


Помог: 1 раз(а)




Кое как сделал, может тупо, но работает...
Оба метода в одном классе, что бы тупо 2 метода не держать, как их объединить? Растерялся


PHP:
скопировать код в буфер обмена
  1.     public function addDistrictCalcPricesByIDItem($item)
  2.     {
  3.         $item['district_calc_price'] = $this->addDistrictCalcPricesByIDItemCalc($item);
  4.         return $item;
  5.     }
  6.  
  7.     public function addDistrictCalcPricesByIDItemCalc($item)
  8.     {
  9.         $_model_map = new Model_Map();
  10.  
  11.         $district_price = $_model_map->getDistrictPricesByID($item['district_key']);
  12.  
  13.         $price_discount = $item['price_discount'];
  14.  
  15.         foreach($district_price as &$i)
  16.         {
  17.             if ($price_discount) {
  18.                 $i = ceil($i-$i*$price_discount/100);
  19.             }
  20.         }
  21.         return $district_price;
  22.     }
 
 Top
Страниц (1): [1]
Сейчас эту тему просматривают: 0 (гостей: 0, зарегистрированных: 0)
« Напишите за меня, пожалуйста »


Все гости форума могут просматривать этот раздел.
Только зарегистрированные пользователи могут создавать новые темы в этом разделе.
Только зарегистрированные пользователи могут отвечать на сообщения в этом разделе.
 



Powered by PHP  Powered By MySQL  Powered by Nginx  Valid CSS  RSS

 
Powered by ExBB FM 1.0 RC1. InvisionExBB