Erro Warning: number_format() expects parameter 1 to be float, s Boa tarde, Alguem sabe me dizer onde estou errando? Eu adptei o código de um exemplo do tutor mas não funcionou, segue código e tela. ...
RF
Erro Warning: number_format() expects parameter 1 to be float, s  
Boa tarde,

Alguem sabe me dizer onde estou errando?
Eu adptei o código de um exemplo do tutor mas não funcionou, segue código e tela.

 
  1. <?php
  2. /**
  3. * ListaCadatroProdutos Form List
  4. * @author <your name here>
  5. */
  6. class ListaCadatroProdutos extends TPage
  7. {
  8. protected $form; // form
  9. protected $datagrid; // datagrid
  10. protected $pageNavigation;
  11. protected $loaded;
  12. /**
  13. * Form constructor
  14. * @param $param Request
  15. */
  16. public function __construct( $param )
  17. {
  18. parent::__construct();
  19. // creates the form
  20. $this->form = new TQuickForm('form_Produto');
  21. $this->form->class = 'tform'; // change CSS class
  22. $this->form->style = 'display: table;width:100%'; // change style
  23. $this->form->setFormTitle('Produto');
  24. // create the form fields
  25. $id = new TEntry('id');
  26. $tipo = new TCombo('tipo');
  27. $bairro = new TEntry('bairro');
  28. $unidis = new TEntry('unidis');
  29. $vlmaior = new TEntry('vlmaior');
  30. $vlmenor = new TEntry('vlmenor');
  31. $empreendimento = new TEntry('empreendimento');
  32. // definição dos formatos dos campos
  33. $vlmaior -> setNumericMask(2,',', '.', true);
  34. $vlmenor -> setNumericMask(2,',', '.', true);
  35. $empreendimento ->style = "text-transform: uppercase";
  36. $bairro ->style = "text-transform: uppercase";
  37. $unidis ->style = "text-transform: uppercase";
  38. $id ->setEditable(FALSE);
  39. //Itens do estagio
  40. $itemEstagio= array();
  41. $itemEstagio['LANÇAMENTO'] = 'LANÇAMENTO';
  42. $itemEstagio['EM OBRAS'] = 'EM OBRAS';
  43. $itemEstagio['PRONTO PARA MORAR'] = 'PRONTO PARA MORAR';
  44. $tipo->setValue('PRONTO PARA MORAR');
  45. $tipo->addItems($itemEstagio);
  46. // add the fields
  47. $this->form->addQuickField('Código', $id, 200 );
  48. $this->form->addQuickField('Tipo', $tipo, 200 );
  49. $this->form->addQuickField('Bairro', $bairro, 200 );
  50. $this->form->addQuickField('Unidade disponível', $unidis, 450 );
  51. $this->form->addQuickField('Maior valor', $vlmaior, 200 );
  52. $this->form->addQuickField('Menor valor', $vlmenor, 200 );
  53. $this->form->addQuickField('Produto', $empreendimento, 200 );
  54. /** samples
  55. $this->form->addQuickFields('Date', array($date1, new TLabel('to'), $date2)); // side by side fields
  56. $fieldX->addValidation( 'Field X', new TRequiredValidator ); // add validation
  57. $fieldX->setSize( 100, 40 ); // set size
  58. **/
  59. // create the form actions
  60. $this->form->addQuickAction(_t('Save'), new TAction(array($this, 'onSave')), 'fa:floppy-o');
  61. $this->form->addQuickAction(_t('New'), new TAction(array($this, 'onClear')), 'bs:plus-sign green');
  62. // creates a Datagrid
  63. $this->datagrid = new TDataGrid;
  64. ##LIST_DECORATOR##
  65. $this->datagrid->style = 'width: 100%';
  66. $this->datagrid->setHeight(320);
  67. // $this->datagrid->datatable = 'true';
  68. // $this->datagrid->enablePopover('Popover', 'Hi <b> {name} </b>');
  69. // creates the datagrid columns
  70. $column_id = new TDataGridColumn('id', 'Código', 'left');
  71. $column_tipo = new TDataGridColumn('tipo', 'Tipo', 'left');
  72. $column_bairro = new TDataGridColumn('bairro', 'Bairro', 'left');
  73. $column_unidis = new TDataGridColumn('unidis', 'Unidade disponível', 'left');
  74. $column_vlmaior = new TDataGridColumn('vlmaior', 'Maior valor', 'left');
  75. $column_vlmenor = new TDataGridColumn('vlmenor', 'Menor valor', 'left');
  76. $column_empreendimento = new TDataGridColumn('empreendimento', 'Produto', 'left');
  77. $column_vlmaior->setTransformer(array($this, 'formatValor'));
  78. $column_vlmenor->setTransformer(array($this, 'formatValor'));
  79. // add the columns to the DataGrid
  80. $this->datagrid->addColumn($column_id);
  81. $this->datagrid->addColumn($column_tipo);
  82. $this->datagrid->addColumn($column_bairro);
  83. $this->datagrid->addColumn($column_unidis);
  84. $this->datagrid->addColumn($column_vlmaior);
  85. $this->datagrid->addColumn($column_vlmenor);
  86. $this->datagrid->addColumn($column_empreendimento);
  87. // creates two datagrid actions
  88. $action1 = new TDataGridAction(array($this, 'onEdit'));
  89. $action1->setUseButton(TRUE);
  90. $action1->setButtonClass('btn btn-default');
  91. //$action1->setLabel(_t('Edit'));
  92. $action1->setImage('fa:pencil-square-o blue fa-lg');
  93. $action1->setField('id');
  94. /*
  95. $action2 = new TDataGridAction(array($this, 'onDelete'));
  96. $action2->setUseButton(TRUE);
  97. $action2->setButtonClass('btn btn-default');
  98. $action2->setLabel(_t('Delete'));
  99. $action2->setImage('fa:trash-o red fa-lg');
  100. $action2->setField('id');
  101. */
  102. // add the actions to the datagrid
  103. $this->datagrid->addAction($action1);
  104. // $this->datagrid->addAction($action2);
  105. // create the datagrid model
  106. $this->datagrid->createModel();
  107. // creates the page navigation
  108. $this->pageNavigation = new TPageNavigation;
  109. $this->pageNavigation->setAction(new TAction(array($this, 'onReload')));
  110. $this->pageNavigation->setWidth($this->datagrid->getWidth());
  111. /* vertical box container
  112. $container = new TVBox;
  113. $container->style = 'width: 90%';
  114. // $container->add(new TXMLBreadCrumb('menu.xml', __CLASS__));
  115. $container->add($this->form);
  116. $container->add($this->datagrid);
  117. $container->add($this->pageNavigation);
  118. parent::add($container);
  119. */
  120. // Sub Menu Bonito
  121. $container = new TVBox;
  122. $container->style = 'width: 90%';
  123. $container->add(new TXMLBreadCrumb('menu.xml', __CLASS__));
  124. $container->add($this->form);
  125. $container->add(TPanelGroup::pack('', $this->datagrid));
  126. $container->add($this->pageNavigation);
  127. parent::add($container);
  128. }
  129. /**
  130. Classes ---------------------------------------------------
  131. */
  132. public function formatValor($param)
  133. {
  134. $param = number_format($param, 2, ',', '.');
  135. return $param;
  136. }
  137. /**
  138. * Load the datagrid with data
  139. */
  140. public function onReload($param = NULL)
  141. {
  142. try
  143. {
  144. // open a transaction with database 'sample'
  145. TTransaction::open('sample');
  146. // creates a repository for Produto
  147. $repository = new TRepository('Produto');
  148. $limit = 10;
  149. // creates a criteria
  150. $criteria = new TCriteria;
  151. // default order
  152. if (empty($param['order']))
  153. {
  154. $param['order'] = 'id';
  155. $param['direction'] = 'asc';
  156. }
  157. $criteria->setProperties($param); // order, offset
  158. $criteria->setProperty('limit', $limit);
  159. if (TSession::getValue('Produto_filter'))
  160. {
  161. // add the filter stored in the session to the criteria
  162. $criteria->add(TSession::getValue('Produto_filter'));
  163. }
  164. // load the objects according to criteria
  165. $objects = $repository->load($criteria, FALSE);
  166. $this->datagrid->clear();
  167. if ($objects)
  168. {
  169. // iterate the collection of active records
  170. foreach ($objects as $object)
  171. {
  172. // add the object inside the datagrid
  173. $this->datagrid->addItem($object);
  174. }
  175. }
  176. // reset the criteria for record count
  177. $criteria->resetProperties();
  178. $count= $repository->count($criteria);
  179. $this->pageNavigation->setCount($count); // count of records
  180. $this->pageNavigation->setProperties($param); // order, page
  181. $this->pageNavigation->setLimit($limit); // limit
  182. // close the transaction
  183. TTransaction::close();
  184. $this->loaded = true;
  185. }
  186. catch (Exception $e) // in case of exception
  187. {
  188. // shows the exception error message
  189. new TMessage('error', '<b>Error</b> ' . $e->getMessage());
  190. // undo all pending operations
  191. TTransaction::rollback();
  192. }
  193. }
  194. /**
  195. * Ask before deletion
  196. */
  197. public function onDelete($param)
  198. {
  199. // define the delete action
  200. $action = new TAction(array($this, 'Delete'));
  201. $action->setParameters($param); // pass the key parameter ahead
  202. // shows a dialog to the user
  203. new TQuestion(TAdiantiCoreTranslator::translate('Do you really want to delete ?'), $action);
  204. }
  205. /**
  206. * Delete a record
  207. */
  208. public function Delete($param)
  209. {
  210. try
  211. {
  212. $key=$param['key']; // get the parameter $key
  213. TTransaction::open('sample'); // open a transaction with database
  214. $object = new Produto($key, FALSE); // instantiates the Active Record
  215. $object->delete(); // deletes the object from the database
  216. TTransaction::close(); // close the transaction
  217. $this->onReload( $param ); // reload the listing
  218. new TMessage('info', TAdiantiCoreTranslator::translate('Record deleted')); // success message
  219. }
  220. catch (Exception $e) // in case of exception
  221. {
  222. new TMessage('error', '<b>Error</b> ' . $e->getMessage()); // shows the exception error message
  223. TTransaction::rollback(); // undo all pending operations
  224. }
  225. }
  226. /**
  227. * Save form data
  228. * @param $param Request
  229. */
  230. public function onSave( $param )
  231. {
  232. try
  233. {
  234. TTransaction::open('sample'); // open a transaction
  235. /**
  236. // Enable Debug logger for SQL operations inside the transaction
  237. TTransaction::setLogger(new TLoggerSTD); // standard output
  238. TTransaction::setLogger(new TLoggerTXT('log.txt')); // file
  239. **/
  240. $this->form->validate(); // validate form data
  241. $object = new Produto; // create an empty object
  242. $data = $this->form->getData(); // get form data as array
  243. $data ->bairro = strtoupper($data->bairro);
  244. $data ->empreendimento = strtoupper($data->empreendimento);
  245. $data ->unidis = strtoupper($data->unidis);
  246. $object->fromArray( (array) $data); // load the object with data
  247. $object->store(); // save the object
  248. // get the generated id
  249. $data->id = $object->id;
  250. $this->form->setData($data); // fill form data
  251. TTransaction::close(); // close the transaction
  252. new TMessage('info', TAdiantiCoreTranslator::translate('Record saved')); // success message
  253. $this->onReload(); // reload the listing
  254. }
  255. catch (Exception $e) // in case of exception
  256. {
  257. new TMessage('error', $e->getMessage()); // shows the exception error message
  258. $this->form->setData( $this->form->getData() ); // keep form data
  259. TTransaction::rollback(); // undo all pending operations
  260. }
  261. }
  262. /**
  263. * Clear form data
  264. * @param $param Request
  265. */
  266. public function onClear( $param )
  267. {
  268. $this->form->clear(TRUE);
  269. }
  270. /**
  271. * Load object to form data
  272. * @param $param Request
  273. */
  274. public function onEdit( $param )
  275. {
  276. try
  277. {
  278. if (isset($param['key']))
  279. {
  280. $key = $param['key']; // get the parameter $key
  281. TTransaction::open('sample'); // open a transaction
  282. $object = new Produto($key); // instantiates the Active Record
  283. $this->form->setData($object); // fill the form
  284. TTransaction::close(); // close the transaction
  285. }
  286. else
  287. {
  288. $this->form->clear(TRUE);
  289. }
  290. }
  291. catch (Exception $e) // in case of exception
  292. {
  293. new TMessage('error', $e->getMessage()); // shows the exception error message
  294. TTransaction::rollback(); // undo all pending operations
  295. }
  296. }
  297. /**
  298. * method show()
  299. * Shows the page
  300. */
  301. public function show()
  302. {
  303. // check if the datagrid is already loaded
  304. if (!$this->loaded AND (!isset($_GET['method']) OR $_GET['method'] !== 'onReload') )
  305. {
  306. $this->onReload( func_get_arg(0) );
  307. }
  308. parent::show();
  309. }
  310. }

Curso Dominando o Adianti Framework

O material mais completo de treinamento do Framework.
Curso em vídeo aulas + Livro completo + Códigos fontes do projeto ERPHouse.
Conteúdo Atualizado!


Dominando o Adianti Framework Quero me inscrever agora!

Comentários (4)


NR

Em alguma situação o php está interpretando a varíavel $param como string quando deveria ser float:

Algum dos valores dessas colunas está vindo vazio do banco?

Você está gravando os dados no banco com vírgula e ponto(413.400,00)?
RF

Boa tarde...

Na verdade eu só queria ver na DataGrid o valor dos campos Maior Valor e Menor Valor em formato númerico, se não for possível tudo bem eu vou sobreviver.
NR

Pra não aparecer o erro é só fazer o cast da variável para float:
 
  1. <?php
  2. $param = number_format((float)$param, 2, ',', '.');
  3. ?>
RF

Muito obrigado, era o que eu precisava. Tenho outras dúvidas mas depois eu posto, outro dia eu pergunto mais...