Jump to content

Archived

This topic is now archived and is closed to further replies.

nestorvaldez

how to assume Null fields

Recommended Posts

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);
}

Share this post


Link to post
Share on other sites
The question is how you get back NULL vs zero from your DB to begin with.

Share this post


Link to post
Share on other sites
I'm confused... .value in JS for an INPUT will never be NULL.

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.