Jump to content


Photo

Arrays store data sa strings, but what if I don't want that?


  • Please log in to reply
2 replies to this topic

#1 taps128

taps128
  • Members
  • PipPip
  • Member
  • 14 posts

Posted 21 April 2006 - 08:32 AM

I fill an array with data collected from a webpage form it looks like this:

while ($a<$redovi) {d
$pk="proizvedeno_komada".$a;
$sp="sifra_proizvoda".$a;
$proizvedeno_komada[]=$_POST[$pk];
$sifra_proizvoda[]=$_POST[$sp];
$a++;
}

And since only integers can be stored inside $proizvedeno_komada, I want to produce a "You can't do that" message if the user entered anything else besides an integer.

When I entered only integers the gettype() function echoed string for all of them my checking function goes like this :
function kontrola_niza($niz)
{
$a=array_merge($niz);
foreach ($a as $value){

if (is_integer($value)==FALSE) {$r=0;return $r; break;}


}
$r=1;
return $r;
}

I appriciate all the help you can give me.

Nikola

#2 sonjay

sonjay
  • New Members
  • Pip
  • Newbie
  • 4 posts
  • LocationThe Hurricane State

Posted 21 April 2006 - 01:25 PM

The "numbers" that come in through your $_POST values are always POSTed as strings, even if they're numbers.

You need to use PHP's is_numeric() function instead of is_integer(). You can also cast your POSTed values as integers before doing anything with them:
$val = intval($val);


#3 taps128

taps128
  • Members
  • PipPip
  • Member
  • 14 posts

Posted 21 April 2006 - 05:01 PM

thanx




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users