Jump to content

how to assume Null fields


nestorvaldez

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);
}
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.