Jump to content


Photo

how to assume Null fields


  • Please log in to reply
3 replies to this topic

#1 nestorvaldez

nestorvaldez
  • Members
  • PipPip
  • Member
  • 12 posts

Posted 28 October 2006 - 01:55 PM

Im working with a MySql Database with PHP, but, right now I need to make some calculations in the client area, I mean the system must give the results before to send to the database, I've got it using Javascript. This is just the explanation what Im doing...
My problem is that I have some decimal(3.2) fields in MySql with NULL. I want Java script in the calculation take the null value not like a 0. This is because I'm making some avg calculations, and I have two Fields to make the avg calculation, if the fields are >=0 it will promediate considering this field if some of the fields is not 0 (NULL.  it take the value from DB) it will not considerate it to promediate.

EX1. REG_PRA1 =80
    REG_PRA2 =90
    In this case it must calc (80+90) /2 = 85

EX2. REG_PRA1 =80
    REG_PRA2 =0
    In this case it must calc (80+0) /2 = 40

EX3. REG_PRA1 =80
    REG_PRA2 =NULL
    In this case it must calc (80+90) /1 = 80

Im using this code: the code is working well, the problem is takin the value (0 or null) from the DB) it take both (0 and null) like 0.0
I need to solve it to make the correct average.

var reg_pra1 = document.Actualizar.reg_pra1.value;
var reg_pra2 = document.Actualizar.reg_pra2.value;

var count=0;
var sum=0;

if(reg_pra1>=0){
count=count+1;
sum = reg_pra1 * 1;
}

if(reg_pra2>=0){
count=count+1;
sum = (sum * 1) + (reg_pra2 * 1);
}

if(sum>0){
var prompra = sum/count;
prompra.toFixed(1)
document.Actualizar.reg_promp.value = prompra.toFixed(1);
}


#2 fenway

fenway
  • Staff Alumni
  • MySQL Si-Fu / PHP Resident Alien
  • 16,199 posts
  • LocationToronto, ON

Posted 28 October 2006 - 04:24 PM

The question is how you get back NULL vs zero from your DB to begin with.
Seriously... if people don't start reading this before posting, I'm going to consider not answering at all.

#3 nestorvaldez

nestorvaldez
  • Members
  • PipPip
  • Member
  • 12 posts

Posted 28 October 2006 - 04:32 PM

so, how is it?

#4 fenway

fenway
  • Staff Alumni
  • MySQL Si-Fu / PHP Resident Alien
  • 16,199 posts
  • LocationToronto, ON

Posted 28 October 2006 - 04:47 PM

I'm confused... .value in JS for an INPUT will never be NULL.
Seriously... if people don't start reading this before posting, I'm going to consider not answering at all.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users