Jump to content


Photo

Erro Em Max() E Min() No Php 5.2.6


  • Faça o login para participar
4 replies to this topic

#1 djalma_bina

djalma_bina

    Novato no fórum

  • Usuários
  • 5 posts
  • Sexo:Não informado

Posted 26/02/2010, 20:32


Ola pessoal !
Sou novato em desenvolvimento com PHP 5 e MySQL , estou fazendo uma classe para pegar dados de arquivos XML e GPS. Meu codigo-fonte este :

<?php

  class Route {
	var $points; //array with points
	
	var $lats; //array with latitudes
	var $maxlat; 
	var $minlat;

	var $lngs; //array with longitudes
	var $maxlng; 
	var $minlng;
	
	var $alts; //array with elevations 
	var $maxalt;
	var $minalt;
	 
	var $n; 
	var $dist; //total distance in metres
	var $drop; //total drop in metres
	var $time; //time the route spent in secconds
	var $hours;
	var $minutes;

	function fromPoints($points) {   
	    
	
		$this->n = count($points); 
                 
		
		for($i = 0; $i < $this->n; $i++) {
			$this->lats[$i] = $points[$i]->lat;
			$this->lngs[$i] = $points[$i]->lng;
			$this->alts[$i] = $points[$i]->alt;
		}
		
                  //max and min 		
                  $this->maxlat = max($this->lats); 	  	 
                  $this->maxlng = max($this->lngs); 		
                  $this->maxalt = max($this->alts); 		
                  $this->minlat = min($this->lats); 		
                  $this->minlng = min($this->lngs); 		
                  $this->minalt = min($this->alts); 	

		
		$this->drop = $this->getDrop($points);
		$this->dist = $this->getTotalDistance($this->lats,$this->lngs,$this->alts);
		$this->time = $points[($this->n-1)]->time - $points[0]->time;
		$this->hours = floor($this->time / 3600);
		$this->minutes = floor(($this->time - 3600 * $this->hours) / 60);
	}

	     
	function getDrop($points) // returns total drop in 
	{
		$n = count($points);
		$drop = 0;
	 	for($i = 1; $i < $n; $i++)
	 	{
			if($points[$i]->alt > $points[($i-1)]->alt) 
				$drop += round($points[$i]->alt - $points[($i-1)]->alt);
		}
		return $drop;
	}
	function distAB($lat1,$lng1,$alt1,$lat2,$lng2,$alt2) { //returns the distance in metres between two points
		$distKM = 6371000 * acos(cos(deg2rad($lat1))*cos(deg2rad($lat2))*cos(deg2rad($lng2)-deg2rad($lng1))+sin(deg2rad($lat1))*sin(deg2rad($lat2)));
		$dist = sqrt(pow($distKM,2)+pow(($alt1-$alt2),2));
		return $dist; 
	}
	function getTotalDistance($lats,$lngs,$alts) { //returns total distance in meters
		$step = 1;
	 	$total = count($lats);
	
		$a = $b = $c = 0;
		
		//this algorithm takes the max and the min difference distances, to calculate a pitagoric distance at the end
		for($i = $step; $i < $total; $i += $step) {
			$latn = abs($lats[$i]-$lats[$i-$step]);
			$lngn = abs($lngs[$i]-$lngs[$i-$step]);
			$a += max($latn,$lngn);
			$b += min($latn,$lngn);
			$c += abs($alts[$i]-$alts[$i-$step]);
		}
		return $this->distAB(0,0,0,$a,$b,$c);
	}
}
?>

A página que utiliza esta classe roda neste url --
http://trekhotel.com.../index.php?r=46
No entanto o php retorna estes warnings :

Warning: Wrong parameter count for max() in /home/trekhote/public_html/bina/core/georoute.class.php  on line 41

Warning: Wrong parameter count for max() in /home/trekhote/public_html/bina/core/georoute.class.php on line 42

Warning: Wrong parameter count for max() in /home/trekhote/public_html/bina/core/georoute.class.php on line 43

Warning: Wrong parameter count for min() in /home/trekhote/public_html/bina/core/georoute.class.php on line 44

Warning: Wrong parameter count for min() in /home/trekhote/public_html/bina/core/georoute.class.php on line 45

Warning: Wrong parameter count for min() in /home/trekhote/public_html/bina/core/georoute.class.php on line 46

As funções max() e min() não aceitam os arrays , por que ?
Obrigado por qualquer sugestão .
Djalma Bina.

#2 Felipe Pena

Felipe Pena

    O temor do Senhor é o princípio da sabedoria

  • Ex-Admins
  • 6441 posts
  • Sexo:Masculino

Posted 26/02/2010, 21:39

O problema parece ser na parte:

                  //max and min                 
                  $this->maxlat = max($this->lats);              
                  $this->maxlng = max($this->lngs);             
                  $this->maxalt = max($this->alts);             
                  $this->minlat = min($this->lats);             
                  $this->minlng = min($this->lngs);             
                  $this->minalt = min($this->alts);

Dependendo do valor de $this->n, você poderia estar passando um valor incorreto ao chamar a max()/min() com as propriedades $this->lats, $this->lngs e $this->alts.
Felipe Pena
[...] ó terra, terra, terra; ouve a palavra do Senhor. — Jeremias 22:29

#3 djalma_bina

djalma_bina

    Novato no fórum

  • Usuários
  • 5 posts
  • Sexo:Não informado

Posted 27/02/2010, 23:24

Desculpe insistir, eu não entendi o que voce quer dizer, que valor seria este?
Segundo o Manual oficial do PHP, http://php.net as funções max() e min() pedem arrays como parametros.
Obrigado.

#4 Paulo Freitas

Paulo Freitas

    ××××××× LRU #456504 ××××××× ××××××× LRM #364686 ×××××××

  • Ex-Admins
  • 5612 posts
  • Sexo:Masculino
  • Localidade:Campinas - SP

Posted 28/02/2010, 00:23

Desculpe insistir, eu não entendi o que voce quer dizer, que valor seria este?
Segundo o Manual oficial do PHP, http://php.net as funções max() e min() pedem arrays como parametros.
Obrigado.

Muito simples: as propriedades $this->lats, $this->lngs e $this->alts não foram inicializadas como array (e portanto são tudo null) e $this->n (quem vem de count($points)) é igual a zero, o que faz o loop de construção destes futuros arrays não executar.

O problema, então, está no parâmetro $points que você passa na função, que muito provavelmente está sendo passado como null, pois de outra forma as funções max() e min() retornariam zero (0). Simples assim.

Reproduzindo o problema:

<?php

$a = null;
$p = null;
$c = count($p);

for ($i = 0; $i < $c; $i++) {
    $a[] = $i;
}

var_dump(max($a));
[]’sAté mais

#5 djalma_bina

djalma_bina

    Novato no fórum

  • Usuários
  • 5 posts
  • Sexo:Não informado

Posted 01/03/2010, 16:02


No debug da página o Firefox retorna estes warnings explicando o
que falta :

<b>Warning</b>: max() [<a href='function.max'>function.max</a>]: Array must contain at least one element in <b>/home/trekhote/public_html/bina/get/georoute.class.php</b> on line <b>39</b><br />
<br />
<b>Warning</b>: max() [<a href='function.max'>function.max</a>]: Array must contain at least one element in <b>/home/trekhote/public_html/bina/get/georoute.class.php</b> on line <b>40</b><br />

<br />
<b>Warning</b>: max() [<a href='function.max'>function.max</a>]: Array must contain at least one element in <b>/home/trekhote/public_html/bina/get/georoute.class.php</b> on line <b>41</b><br />
<br />
<b>Warning</b>: min() [<a href='function.min'>function.min</a>]: Array must contain at least one element in <b>/home/trekhote/public_html/bina/get/georoute.class.php</b> on line <b>42</b><br />

<br />
<b>Warning</b>: min() [<a href='function.min'>function.min</a>]: Array must contain at least one element in <b>/home/trekhote/public_html/bina/get/georoute.class.php</b> on line <b>43</b><br />
<br />
<b>Warning</b>: min() [<a href='function.min'>function.min</a>]: Array must contain at least one element in <b>/home/trekhote/public_html/bina/get/georoute.class.php</b> on line <b>44</b><br />

GMaps.marker(52.7293,-117.644,'spot','');
GMaps.marker(52.7286,-117.644,'spot','');
GMaps.marker(52.7285,-117.645,'spot','');
GMaps.marker(52.728,-117.645,'spot','');
GMaps.marker(52.7279,-117.646,'spot','');
GMaps.marker(52.728,-117.646,'spot','');
GMaps.marker(52.7277,-117.645,'spot','');
E T C .....................

Os valores são passados para função GMaps.marker em
http://trekhotel.com.../gmaps.php?r=46
Obrigado !






1 user(s) are reading this topic

0 membro(s), 1 visitante(s) e 0 membros anônimo(s)

IPB Skin By Virteq