Avatar billede webzone Nybegynder
08. juni 2009 - 10:16 Der er 10 kommentarer og
2 løsninger

gennemsnit af antal udfyldte felter

Hej eksperter.
Jeg er ved at prøve at lave et scrip,som skal beregne summen, samt gennemsnittet af ti felter. men den skal kun beregne gennemsnittet ud fra de felter der er udfyldt. Det vil sige dem der er over 0
Jeg har prøvet mig lidt frem og er nu kommet frem til dette. Det udregner godt nok totalen men gennemsnittet driller.
Kan i hjælpe mig på rette vej?
her er koden:

    if (isset($_POST['update_shooter'])) {
   

$tal1 = $_POST['res01'];
$tal2 = $_POST['res02'];
$tal3 = $_POST['res03'];
$tal4 = $_POST['res04'];
$tal5 = $_POST['res05'];
$tal6 = $_POST['res06'];
$tal7 = $_POST['res07'];
$tal8 = $_POST['res08'];
$tal9 = $_POST['res09'];
$tal10 = $_POST['res10'];
$tal012 = $_POST['res012'];
$tal022 = $_POST['res022'];
$tal032 = $_POST['res032'];
$tal042 = $_POST['res042'];
$tal052 = $_POST['res052'];
$tal062 = $_POST['res062'];
$tal072 = $_POST['res072'];
$tal082 = $_POST['res082'];
$tal092 = $_POST['res092'];
$tal102 = $_POST['res102'];
$tal11 = $tal1 + $tal2 + $tal3 + $tal4 + $tal5 + $tal6 + $tal7 + $tal8 + $tal9 + $tal10 + $tal012 + $tal022 + $tal032 + $tal042 + $tal052 + $tal062 + $tal072 + $tal082 + $tal092 + $tal102;

$count=0; {
if ($tal1 + $tal012 > 0){$count=$count+1;}
if ($tal2 + $tal022 > 0){$count=$count+1;}
if ($tal3 + $tal032 > 0){$count=$count+1;}
if ($tal4 + $tal042 > 0){$count=$count+1;}
if ($tal5 + $tal052 > 0){$count=$count+1;}
if ($tal6 + $tal062 > 0){$count=$count+1;}
if ($tal7 + $tal072 > 0){$count=$count+1;}
if ($tal8 + $tal082 > 0){$count=$count+1;}
if ($tal9 + $tal092 > 0){$count=$count+1;}
if ($tal10 + $tal102 > 0){$count=$count+1;}
if ($check['$count']) {
$tal12 = $tal1 + $tal2 + $tal3 + $tal4 + $tal5 + $tal6 + $tal7 + $tal8 + $tal9 + $tal10 + $tal012 + $tal022 + $tal032 + $tal042 + $tal052 + $tal062 + $tal072 + $tal082 + $tal092 + $tal102 / $count;  }
else if ($check['$count'] = 0){
$tal12 = $tal1 + $tal2 + $tal3 + $tal4 + $tal5 + $tal6 + $tal7 + $tal8 + $tal9 + $tal10 + $tal012 + $tal022 + $tal032 + $tal042 + $tal052 + $tal062 + $tal072 + $tal082 + $tal092 + $tal102 / 1;
}}


        dbquery("UPDATE ".DB_STAT1." SET name='".$_POST['name']."', `pos`='".$_POST['pos']."', dob='".$_POST['dob']."', height='".$_POST['height']."', weight='".$_POST['weight']."', hand='".$_POST['hand']."', res01='".$_POST['res01']."'+'".$_POST['res012']."', res02='".$_POST['res02']."'+'".$_POST['res022']."', res03='".$_POST['res03']."'+'".$_POST['res032']."', res04='".$_POST['res04']."'+'".$_POST['res042']."', res05='".$_POST['res05']."'+'".$_POST['res052']."', res06='".$_POST['res06']."'+'".$_POST['res062']."', res07='".$_POST['res07']."'+'".$_POST['res072']."', res08='".$_POST['res08']."'+'".$_POST['res082']."', res09='".$_POST['res09']."'+'".$_POST['res092']."', res10='".$_POST['res10']."'+'".$_POST['res102']."', total=".$tal11.", snit=".$tal12.", info='".$_POST['info']."', pic='".$_POST['pic']."', active='".$_POST['active']."', info_breaks='".$_POST['info_breaks']."' WHERE id='".$id."'");
        redirect (FUSION_SELF.$aidlink, "script");
    }
Avatar billede CCodam Nybegynder
08. juni 2009 - 11:17 #1
Prøv at ændre dine if sætninger til noget lignende:
if (($tal1 + $tal012) > 0){ $count++; }

Derudover så ændre din $tal12 udregning til:

if ($count > 0) { $tal12 = $tal11 / $count; }
else { $tal12 = 0 }


Derudover så synes jeg du burde ændre tal11 og tal12 til noget mere sigende...
Avatar billede webzone Nybegynder
08. juni 2009 - 13:22 #2
Efter lidt nørklerier med dine yderst nyttige informationer har jeg nu fået tilpasset det så det virker.

men ved du også hvor jeg skal putte afrunding til 2 decimaler ind henne for at den laver 2decimaler i gennemsnitsudregningen?
Den laver nemlig slet ingen decimaler men skriver det kun i hele tal.
Jeg vil tro at det er noget lignende dette jeg skal indsætte
$snit = round($snit, 2);


her er den nye kode jeg kom frem til:

    if (isset($_POST['update_shooter'])) {
   

$tal01 = $_POST['res01'];
$tal02 = $_POST['res02'];
$tal03 = $_POST['res03'];
$tal04 = $_POST['res04'];
$tal05 = $_POST['res05'];
$tal06 = $_POST['res06'];
$tal07 = $_POST['res07'];
$tal08 = $_POST['res08'];
$tal09 = $_POST['res09'];
$tal10 = $_POST['res10'];
$tal012 = $_POST['res012'];
$tal022 = $_POST['res022'];
$tal032 = $_POST['res032'];
$tal042 = $_POST['res042'];
$tal052 = $_POST['res052'];
$tal062 = $_POST['res062'];
$tal072 = $_POST['res072'];
$tal082 = $_POST['res082'];
$tal092 = $_POST['res092'];
$tal102 = $_POST['res102'];
$total = $tal01 + $tal02 + $tal03 + $tal04 + $tal05 + $tal06 + $tal07 + $tal08 + $tal09 + $tal10 + $tal012 + $tal022 + $tal032 + $tal042 + $tal052 + $tal062 + $tal072 + $tal082 + $tal092 + $tal102;


if (($tal01 + $tal012) > 0){$count++;}
if (($tal02 + $tal022) > 0){$count++;}
if (($tal03 + $tal032) > 0){$count++;}
if (($tal04 + $tal042) > 0){$count++;}
if (($tal05 + $tal052) > 0){$count++;}
if (($tal06 + $tal062) > 0){$count++;}
if (($tal07 + $tal072) > 0){$count++;}
if (($tal08 + $tal082) > 0){$count++;}
if (($tal09 + $tal092) > 0){$count++;}
if (($tal10 + $tal102) > 0){$count++;}

if ($count > 0) { $snit = $total / $count; }
else {$snit = 0; }



        dbquery("UPDATE ".DB_STAT1." SET name='".$_POST['name']."', `pos`='".$_POST['pos']."', dob='".$_POST['dob']."', height='".$_POST['height']."', weight='".$_POST['weight']."', hand='".$_POST['hand']."', res01='".$_POST['res01']."'+'".$_POST['res012']."', res02='".$_POST['res02']."'+'".$_POST['res022']."', res03='".$_POST['res03']."'+'".$_POST['res032']."', res04='".$_POST['res04']."'+'".$_POST['res042']."', res05='".$_POST['res05']."'+'".$_POST['res052']."', res06='".$_POST['res06']."'+'".$_POST['res062']."', res07='".$_POST['res07']."'+'".$_POST['res072']."', res08='".$_POST['res08']."'+'".$_POST['res082']."', res09='".$_POST['res09']."'+'".$_POST['res092']."', res10='".$_POST['res10']."'+'".$_POST['res102']."', total=".$total.", snit=".$snit.", info='".$_POST['info']."', pic='".$_POST['pic']."', active='".$_POST['active']."', info_breaks='".$_POST['info_breaks']."' WHERE id='".$id."'");
        redirect (FUSION_SELF.$aidlink, "script");
}
Avatar billede CCodam Nybegynder
08. juni 2009 - 14:30 #3
Mon ikke dette burde virke:
if ($count > 0) { $snit = round(($total / $count), 2); }
else {$snit = 0; }
Avatar billede webzone Nybegynder
08. juni 2009 - 14:55 #4
Nej det gjorde det ikke, den runder stadig ned til nærmeste hele tal jeg ved ikke helt om det er et andet sted i scriptet den så er gal. men jeg prøver lige at vedhæfte hele det aktuelle script



opentable($locale['srl_001']);
echo "<table width='100%' align='center'>
<tr>
    <td align='center' width='25%'><a href='admin_new_shooter.php'><img src='images/player.png'><br />".$locale['srl_002']."</a></td>
    <td align='center' width='25%'><a href='admin_update_shooter.php'><img src='images/update.png'><br />".$locale['srl_003']."</a></td>
    <td align='center' width='25%'><a href='admin_active_shooter.php'><img src='images/status.png'><br />".$locale['srl_004']."</a></td>
    <td align='center' width='25%'><a href='admin_upload_picture.php'><img src='images/playerpic.png'><br />".$locale['srl_005']."</a></td>
</tr>
</table>";
closetable();

tablebreak();

$axn = isset($_REQUEST['axn']) ? $_REQUEST['axn'] : "";
$id = (isset($_REQUEST['id']) AND isnum($_REQUEST['id'])) ? $_REQUEST['id'] : "";


$cat_files = makefilelist(CAT_DIR, ".|..|index.php", true);
$cat_list = makefileopts($cat_files);
$cat_image = "nopic.jpg";

opentable($locale['srl_001']);
switch ($axn) {
    default:
    $result = dbquery("SELECT * FROM ".DB_STAT1." WHERE active='1' ORDER BY name ASC");
    $rows = dbrows($result);
   
    if ($rows > 0) {
echo "<table width='780' align='center' cellpadding='0' cellspacing='0'>
<tr>
    <td height='20' align='right' class='tbl3'><strong>".$locale['srl_050b']."</strong></td>
    <td height='20' align='left' class='tbl3'><strong>".$locale['srl_051']."</strong></td>
    <td height='20' align='center' class='tbl3'><strong>".$locale['srl_052']."</strong></td>
    <td height='20' align='center' class='tbl3'><img src='images/skive.gif' title='".$locale['srl_024a']."' alt='".$locale['srl_024a']."'></td>
    <td height='20' align='center' class='tbl3'><img src='images/skive.gif' title='".$locale['srl_024b']."' alt='".$locale['srl_024b']."'></td>
    <td height='20' align='center' class='tbl3'><img src='images/skive.gif' title='".$locale['srl_024c']."' alt='".$locale['srl_024c']."'></td>
    <td height='20' align='center' class='tbl3'><img src='images/skive.gif' title='".$locale['srl_024d']."' alt='".$locale['srl_024d']."'></td>
    <td height='20' align='center' class='tbl3'><img src='images/skive.gif' title='".$locale['srl_024e']."' alt='".$locale['srl_024e']."'></td>
    <td height='20' align='center' class='tbl3'><img src='images/skive.gif' title='".$locale['srl_024f']."' alt='".$locale['srl_024f']."'></td>
    <td height='20' align='center' class='tbl3'><img src='images/skive.gif' title='".$locale['srl_024g']."' alt='".$locale['srl_024g']."'></td>
    <td height='20' align='center' class='tbl3'><img src='images/skive.gif' title='".$locale['srl_024h']."' alt='".$locale['srl_024h']."'></td>
    <td height='20' align='center' class='tbl3'><img src='images/skive.gif' title='".$locale['srl_024i']."' alt='".$locale['srl_024i']."'></td>
    <td height='20' align='center' class='tbl3'><img src='images/skive.gif' title='".$locale['srl_024j']."' alt='".$locale['srl_024j']."'></td>
    <td height='20' align='center' class='tbl3'><img src='images/win.gif' title='".$locale['srl_025']."' alt='".$locale['srl_025']."'></td>
    <td height='20' align='center' class='tbl3'><img src='images/win.gif' title='".$locale['srl_025b']."' alt='".$locale['srl_025b']."'></td>
    <td colspan='2' height='20' align='center' class='tbl3'>".$locale['srl_025c']."</td>
    </tr>";
        while ($data = dbarray($result)) {

    if ($data['pos'] == 1) $data['pos2'] = "".$locale['srl_014a']."";
    if ($data['pos'] == 2) $data['pos2'] = "".$locale['srl_014b']."";
    if ($data['pos'] == 3) $data['pos2'] = "".$locale['srl_014c']."";
    if ($data['pos'] == 4) $data['pos2'] = "".$locale['srl_014d']."";
   
        $pos2 = $data['pos2'];

            echo '<tr>
    <td height="20" align="right" class="tbl3">'.$data['id'].'</td>
    <td height="20" align="left" class="tbl2">'.$data['name'].'</td>
    <td height="20" align="center" class="tbl2">'.$data['pos2'].'</td>
    <td height="20" align="center" class="tbl1">'.$data['res01'].'</td>
    <td height="20" align="center" class="tbl">'.$data['res02'].'</td>
    <td height="20" align="center" class="tbl1">'.$data['res03'].'</td>
    <td height="20" align="center" class="tbl">'.$data['res04'].'</td>
    <td height="20" align="center" class="tbl1">'.$data['res05'].'</td>
    <td height="20" align="center" class="tbl">'.$data['res06'].'</td>
    <td height="20" align="center" class="tbl1">'.$data['res07'].'</td>
    <td height="20" align="center" class="tbl">'.$data['res08'].'</td>
    <td height="20" align="center" class="tbl1">'.$data['res09'].'</td>
    <td height="20" align="center" class="tbl">'.$data['res10'].'</td>
    <td height="20" align="center" class="tbl2">'.$data['total'].'</td>
    <td height="20" align="center" class="tbl2">'.$data['snit'].'</td>';
    echo "<td height='20' align='center' class='tbl3'><a href='".FUSION_SELF.$aidlink."&axn=update&id=".$data['id']."'>".$locale['srl_017']."</a></a></td>";
    echo "<td height='20' align='center' class='tbl3'><a href='".FUSION_SELF.$aidlink."&axn=reset&id=".$data['id']."'>".$locale['srl_018']."</a></a></td>";
        }
        echo "<tr><td colspan='11' align='center'><br></td></tr>\n";
        echo "<tr><td colspan='11' align='center'></td></tr></table>\n";
    }
    else {
        echo "<div style='text-align:center'><p style='margin-top:2em'><span style='color:#cc0000; weight:bold;font-size:1.5em;'>".$locale['srl_020']."</span></p>\n";

    }
    break;
    case 'update':
    if (isset($_POST['update_shooter'])) {
   

$tal01 = $_POST['res01'];
$tal02 = $_POST['res02'];
$tal03 = $_POST['res03'];
$tal04 = $_POST['res04'];
$tal05 = $_POST['res05'];
$tal06 = $_POST['res06'];
$tal07 = $_POST['res07'];
$tal08 = $_POST['res08'];
$tal09 = $_POST['res09'];
$tal10 = $_POST['res10'];
$tal012 = $_POST['res012'];
$tal022 = $_POST['res022'];
$tal032 = $_POST['res032'];
$tal042 = $_POST['res042'];
$tal052 = $_POST['res052'];
$tal062 = $_POST['res062'];
$tal072 = $_POST['res072'];
$tal082 = $_POST['res082'];
$tal092 = $_POST['res092'];
$tal102 = $_POST['res102'];
$total = $tal01 + $tal02 + $tal03 + $tal04 + $tal05 + $tal06 + $tal07 + $tal08 + $tal09 + $tal10 + $tal012 + $tal022 + $tal032 + $tal042 + $tal052 + $tal062 + $tal072 + $tal082 + $tal092 + $tal102;


if (($tal01 + $tal012) > 0){$count++;}
if (($tal02 + $tal022) > 0){$count++;}
if (($tal03 + $tal032) > 0){$count++;}
if (($tal04 + $tal042) > 0){$count++;}
if (($tal05 + $tal052) > 0){$count++;}
if (($tal06 + $tal062) > 0){$count++;}
if (($tal07 + $tal072) > 0){$count++;}
if (($tal08 + $tal082) > 0){$count++;}
if (($tal09 + $tal092) > 0){$count++;}
if (($tal10 + $tal102) > 0){$count++;}

if ($count > 0) { $snit = round(($total / $count), 2); }
else {$snit = 0; }




        dbquery("UPDATE ".DB_STAT1." SET name='".$_POST['name']."', `pos`='".$_POST['pos']."', dob='".$_POST['dob']."', height='".$_POST['height']."', weight='".$_POST['weight']."', hand='".$_POST['hand']."', res01='".$_POST['res01']."'+'".$_POST['res012']."', res02='".$_POST['res02']."'+'".$_POST['res022']."', res03='".$_POST['res03']."'+'".$_POST['res032']."', res04='".$_POST['res04']."'+'".$_POST['res042']."', res05='".$_POST['res05']."'+'".$_POST['res052']."', res06='".$_POST['res06']."'+'".$_POST['res062']."', res07='".$_POST['res07']."'+'".$_POST['res072']."', res08='".$_POST['res08']."'+'".$_POST['res082']."', res09='".$_POST['res09']."'+'".$_POST['res092']."', res10='".$_POST['res10']."'+'".$_POST['res102']."', total=".$total.", snit=".$snit.", info='".$_POST['info']."', pic='".$_POST['pic']."', active='".$_POST['active']."', info_breaks='".$_POST['info_breaks']."' WHERE id='".$id."'");
        redirect (FUSION_SELF.$aidlink, "script");
}
    else {
        $result = dbquery("SELECT * FROM ".DB_STAT1." WHERE id=".$id."");
        $data = dbarray($result);
       
echo'<form name="addcat" action='.FUSION_SELF.$aidlink.'&axn=update method="post" style="margin:2em;">
<table align="center" width="780" cellspacing="0" cellpadding="0" border="0">
<input type="hidden" name="id" value="'.$data['id'].'">
<input type="hidden" name="name" value="'.$data['name'].'">
<input type="hidden" name="pos" value="'.$data['pos'].'">
<input type="hidden" name="dob" value="'.$data['dob'].'">
<input type="hidden" name="height" value="'.$data['height'].'">
<input type="hidden" name="weight" value="'.$data['weight'].'">
<input type="hidden" name="hand" value="'.$data['hand'].'">
<input type="hidden" name="res01" value="'.$data['res01'].'">
<input type="hidden" name="res02" value="'.$data['res02'].'">
<input type="hidden" name="res03" value="'.$data['res03'].'">
<input type="hidden" name="res04" value="'.$data['res04'].'">
<input type="hidden" name="res05" value="'.$data['res05'].'">
<input type="hidden" name="res06" value="'.$data['res06'].'">
<input type="hidden" name="res07" value="'.$data['res07'].'">
<input type="hidden" name="res08" value="'.$data['res08'].'">
<input type="hidden" name="res09" value="'.$data['res09'].'">
<input type="hidden" name="res10" value="'.$data['res10'].'">
<input type="hidden" name="res10" value="'.$data['total'].'">
<input type="hidden" name="res10" value="'.$data['snit'].'">
<input type="hidden" name="info" value="'.$data['info'].'">
<input type="hidden" name="pic" value="'.$data['pic'].'">
<input type="hidden" name="active" value="'.$data['active'].'">
<input type="hidden" name="info_breaks" value="'.$data['info_breaks'].'">
<tr>
    <td colspan="10" align="center" class="tbl2">'.$data["name"].'</td>
</tr>
<tr>
    <td colspan="10" align="center"><br></td>
</tr>
<tr>
    <td align="center" class="tbl2"><img src="images/skive.gif"></td>
    <td align="center" class="tbl2"><img src="images/skive.gif"></td>
    <td align="center" class="tbl2"><img src="images/skive.gif"></td>
    <td align="center" class="tbl2"><img src="images/skive.gif"></td>
    <td align="center" class="tbl2"><img src="images/skive.gif"></td>
    <td align="center" class="tbl2"><img src="images/skive.gif"></td>
    <td align="center" class="tbl2"><img src="images/skive.gif"></td>
    <td align="center" class="tbl2"><img src="images/skive.gif"></td>
    <td align="center" class="tbl2"><img src="images/skive.gif"></td>
    <td align="center" class="tbl2"><img src="images/skive.gif"></td>
</tr>
<tr>
    <td align="center">'.$data["res01"].'</td>
    <td align="center">'.$data["res02"].'</td>
    <td align="center">'.$data["res03"].'</td>
    <td align="center">'.$data["res04"].'</td>
    <td align="center">'.$data["res05"].'</td>
    <td align="center">'.$data["res06"].'</td>
    <td align="center">'.$data["res07"].'</td>
    <td align="center">'.$data["res08"].'</td>
    <td align="center">'.$data["res09"].'</td>
    <td align="center">'.$data["res10"].'</td>

</tr>
<tr>
    <td align="center">+</td>
    <td align="center">+</td>
    <td align="center">+</td>
    <td align="center">+</td>
    <td align="center">+</td>
    <td align="center">+</td>
    <td align="center">+</td>
    <td align="center">+</td>
    <td align="center">+</td>
    <td align="center">+</td>
</tr>
<tr>
    <td align="center"><input type="text" class="textbox" name="res012" id="res012" size="5" maxlength="4" value="0"></td>
    <td align="center"><input type="text" class="textbox" name="res022" id="res022" size="5" maxlength="4" value="0"></td>
    <td align="center"><input type="text" class="textbox" name="res032" id="res032" size="5" maxlength="4" value="0"></td>
    <td align="center"><input type="text" class="textbox" name="res042" id="res042" size="5" maxlength="4" value="0"></td>
    <td align="center"><input type="text" class="textbox" name="res052" id="res052" size="5" maxlength="4" value="0"></td>
    <td align="center"><input type="text" class="textbox" name="res062" id="res062" size="5" maxlength="4" value="0"></td>
    <td align="center"><input type="text" class="textbox" name="res072" id="res072" size="5" maxlength="4" value="0"></td>
    <td align="center"><input type="text" class="textbox" name="res082" id="res082" size="5" maxlength="4" value="0"></td>
    <td align="center"><input type="text" class="textbox" name="res092" id="res092" size="5" maxlength="4" value="0"></td>
    <td align="center"><input type="text" class="textbox" name="res102" id="res102" size="5" maxlength="4" value="0"></td>
</tr>
<tr>
    <td colspan="10" align="center"><br></td>
</tr>
<tr>
    <td colspan="10" align="center"><input type="submit" name="update_shooter" value='.$locale['srl_028'].' class="button"/></td>
</tr>
</table></form>';

       
    }
    break;
    case 'reset':
    if (isset($_POST['reset_shooter'])) {
   
$tal01 = $_POST['res01'];
$tal02 = $_POST['res02'];
$tal03 = $_POST['res03'];
$tal04 = $_POST['res04'];
$tal05 = $_POST['res05'];
$tal06 = $_POST['res06'];
$tal07 = $_POST['res07'];
$tal08 = $_POST['res08'];
$tal09 = $_POST['res09'];
$tal10 = $_POST['res10'];
$total = $tal01 + $tal02 + $tal03 + $tal04 + $tal05 + $tal06 + $tal07 + $tal08 + $tal09 + $tal10;

if (($tal01 + $tal012) > 0){$count++;}
if (($tal02 + $tal022) > 0){$count++;}
if (($tal03 + $tal032) > 0){$count++;}
if (($tal04 + $tal042) > 0){$count++;}
if (($tal05 + $tal052) > 0){$count++;}
if (($tal06 + $tal062) > 0){$count++;}
if (($tal07 + $tal072) > 0){$count++;}
if (($tal08 + $tal082) > 0){$count++;}
if (($tal09 + $tal092) > 0){$count++;}
if (($tal10 + $tal102) > 0){$count++;}

if ($count > 0) { $snit = round(($total / $count), 2); }
else {$snit = 0; }

        dbquery("UPDATE ".DB_STAT1." SET name='".$_POST['name']."', `pos`='".$_POST['pos']."', dob='".$_POST['dob']."', height='".$_POST['height']."', weight='".$_POST['weight']."', hand='".$_POST['hand']."', res01='".$_POST['res01']."'+'".$_POST['res012']."', res02='".$_POST['res02']."'+'".$_POST['res022']."', res03='".$_POST['res03']."'+'".$_POST['res032']."', res04='".$_POST['res04']."'+'".$_POST['res042']."', res05='".$_POST['res05']."'+'".$_POST['res052']."', res06='".$_POST['res06']."'+'".$_POST['res062']."', res07='".$_POST['res07']."'+'".$_POST['res072']."', res08='".$_POST['res08']."'+'".$_POST['res082']."', res09='".$_POST['res09']."'+'".$_POST['res092']."', res10='".$_POST['res10']."'+'".$_POST['res102']."', total=".$total."-".$total.", snit=".$snit."-".$snit.", info='".$_POST['info']."', pic='".$_POST['pic']."', active='".$_POST['active']."', info_breaks='".$_POST['info_breaks']."' WHERE id='".$id."'");
        redirect (FUSION_SELF.$aidlink, "script");
    }
    else {
        $result = dbquery("SELECT * FROM ".DB_STAT1." WHERE id=".$id."");
        $data = dbarray($result);
       
echo'<form name="addcat" action='.FUSION_SELF.$aidlink.'&axn=reset method="post" style="margin:2em;">
<table align="center" width="468" cellspacing="0" cellpadding="0" border="0">
<input type="hidden" name="id" value="'.$data['id'].'">
<input type="hidden" name="name" value="'.$data['name'].'">
<input type="hidden" name="pos" value="'.$data['pos'].'">
<input type="hidden" name="dob" value="'.$data['dob'].'">
<input type="hidden" name="height" value="'.$data['height'].'">
<input type="hidden" name="weight" value="'.$data['weight'].'">
<input type="hidden" name="hand" value="'.$data['hand'].'">
<input type="hidden" name="res01" value="'.$data['res01'].'">
<input type="hidden" name="res02" value="'.$data['res02'].'">
<input type="hidden" name="res03" value="'.$data['res03'].'">
<input type="hidden" name="res04" value="'.$data['res04'].'">
<input type="hidden" name="res05" value="'.$data['res05'].'">
<input type="hidden" name="res06" value="'.$data['res06'].'">
<input type="hidden" name="res07" value="'.$data['res07'].'">
<input type="hidden" name="res08" value="'.$data['res08'].'">
<input type="hidden" name="res09" value="'.$data['res09'].'">
<input type="hidden" name="res10" value="'.$data['res10'].'">
<input type="hidden" name="total" value="'.$data['total'].'">
<input type="hidden" name="snit" value="'.$data['snit'].'">
<input type="hidden" name="info" value="'.$data['info'].'">
<input type="hidden" name="pic" value="'.$data['pic'].'">
<input type="hidden" name="active" value="'.$data['active'].'">
<input type="hidden" name="info_breaks" value="'.$data['info_breaks'].'">
<tr>
    <td colspan="12" align="center" class="tbl2">'.$data["name"].'</td>
</tr>
<tr>
    <td colspan="12" align="center"><br></td>
</tr>
<tr>
    <td align="center" class="tbl2"><img src="images/skive.gif"></td>
    <td align="center" class="tbl2"><img src="images/skive.gif"></td>
    <td align="center" class="tbl2"><img src="images/skive.gif"></td>
    <td align="center" class="tbl2"><img src="images/skive.gif"></td>
    <td align="center" class="tbl2"><img src="images/skive.gif"></td>
    <td align="center" class="tbl2"><img src="images/skive.gif"></td>
    <td align="center" class="tbl2"><img src="images/skive.gif"></td>
    <td align="center" class="tbl2"><img src="images/skive.gif"></td>
    <td align="center" class="tbl2"><img src="images/skive.gif"></td>
    <td align="center" class="tbl2"><img src="images/skive.gif"></td>
    <td align="center" class="tbl2"><img src="images/win.gif"></td>
    <td align="center" class="tbl2"><img src="images/win.gif"></td>
</tr>
<tr>
    <td height="5" align="center" colspan="12"></td>
</tr>
<tr>
    <td align="center">'.$data["res01"].'</td>
    <td align="center">'.$data["res02"].'</td>
    <td align="center">'.$data["res03"].'</td>
    <td align="center">'.$data["res04"].'</td>
    <td align="center">'.$data["res05"].'</td>
    <td align="center">'.$data["res06"].'</td>
    <td align="center">'.$data["res07"].'</td>
    <td align="center">'.$data["res08"].'</td>
    <td align="center">'.$data["res09"].'</td>
    <td align="center">'.$data["res10"].'</td>
    <td align="center">'.$data["total"].'</td>
    <td align="center">'.$data["snit"].'</td>
</tr>
<tr>
    <td height="5" align="center" colspan="12"></td>
</tr>
<tr>
    <td align="center"><input type="text" class="textbox" name="res012" id="res012" size="5" maxlength="4" value="-'.$data["res01"].'"></td>
    <td align="center"><input type="text" class="textbox" name="res022" id="res022" size="5" maxlength="4" value="-'.$data["res02"].'"></td>
    <td align="center"><input type="text" class="textbox" name="res032" id="res032" size="5" maxlength="4" value="-'.$data["res03"].'"></td>
    <td align="center"><input type="text" class="textbox" name="res042" id="res042" size="5" maxlength="4" value="-'.$data["res04"].'"></td>
    <td align="center"><input type="text" class="textbox" name="res052" id="res052" size="5" maxlength="4" value="-'.$data["res05"].'"></td>
    <td align="center"><input type="text" class="textbox" name="res062" id="res062" size="5" maxlength="4" value="-'.$data["res06"].'"></td>
    <td align="center"><input type="text" class="textbox" name="res072" id="res072" size="5" maxlength="4" value="-'.$data["res07"].'"></td>
    <td align="center"><input type="text" class="textbox" name="res082" id="res082" size="5" maxlength="4" value="-'.$data["res08"].'"></td>
    <td align="center"><input type="text" class="textbox" name="res092" id="res092" size="5" maxlength="4" value="-'.$data["res09"].'"></td>
    <td align="center"><input type="text" class="textbox" name="res102" id="res102" size="5" maxlength="4" value="-'.$data["res10"].'"></td>
    <td align="center"><input type="text" class="textbox" name="total" id="res092" size="5" maxlength="4" value="-'.$data["total"].'"></td>
    <td align="center"><input type="text" class="textbox" name="snit" id="res102" size="5" maxlength="4" value="-'.$data["snit"].'"></td>

</tr>
<tr>
    <td colspan="10" align="center"><br></td>
</tr>
<tr>
    <td colspan="10" align="center"><input type="submit" name="reset_shooter" value='.$locale['srl_018'].' class="button"/></td>
</tr>
</table></form>';

       
    }
    break;
}
closetable();

require_once THEMES."templates/footer.php";
Avatar billede acore Ekspert
08. juni 2009 - 16:55 #5
NB: Svar på din første post:

Jeg kan da se, at $check ikke er defineret (hvis du ellers har posted hele din kode) og at du vist mangler en parentes rundt om summer før du dividere med $count for at beregne gennemsnittet ($tal12).

Jeg vil dog foreslå en anden og mere gennemskuelig struktur på din kode, hvor på laver en liste over hvilke tal-felter, du har, og så løber den igennem og beregner sum og gennemsnit:

if (isset($_POST['update_shooter']))
{
            $num_flds = array(
                        'res01', 'res02', 'res03', 'res04', 'res05', 'res06', 'res07', 'res08', 'res09', 'res10',
                        'res012', 'res022', 'res032', 'res042', 'res052', 'res062', 'res072', 'res082', 'res092', 'res112'
            );

            $total = 0;
            $count = 0;
            foreach($num_flds as $fld)
            {
                        if (isset($_POST[$fld]) && $_POST[$fld] > 0)
                        {
                                    $total += $_POST[$fld];
                                    $count++;
                        }
            }

            $snit = ($count > 0? $total/$count: 0);

        dbquery("UPDATE ".DB_STAT1." SET name='".$_POST['name']."', `pos`='".$_POST['pos']."', dob='".$_POST['dob']."', height='".$_POST['height']."', weight='".$_POST['weight']."', hand='".$_POST['hand']."', res01='".$_POST['res01']."'+'".$_POST['res012']."', res02='".$_POST['res02']."'+'".$_POST['res022']."', res03='".$_POST['res03']."'+'".$_POST['res032']."', res04='".$_POST['res04']."'+'".$_POST['res042']."', res05='".$_POST['res05']."'+'".$_POST['res052']."', res06='".$_POST['res06']."'+'".$_POST['res062']."', res07='".$_POST['res07']."'+'".$_POST['res072']."', res08='".$_POST['res08']."'+'".$_POST['res082']."', res09='".$_POST['res09']."'+'".$_POST['res092']."', res10='".$_POST['res10']."'+'".$_POST['res102']."', total=".$total.", snit=".$snit.", info='".$_POST['info']."', pic='".$_POST['pic']."', active='".$_POST['active']."', info_breaks='".$_POST['info_breaks']."' WHERE id='".$id."'");

        redirect (FUSION_SELF.$aidlink, "script");
}

Jeg har ikke testet koden, så der kan være slåfejl, men dem kan du nok fange og rette - ellers må du vende tilbage her eller på www.php-fusion.dk, som jeg kan se, du bruger som cms.
Avatar billede webzone Nybegynder
08. juni 2009 - 17:41 #6
Det ser ud til også at virke efter hensigten og er da også en kortere kode. Men jeg kan stadig ikke få den til at skrive gennemsnittet med 2 decimaler
Avatar billede acore Ekspert
08. juni 2009 - 18:01 #7
Ja, og nem at rette med nye felter.

Skriv $snit med 2 decimaler:

echo(numberformat($snit, 2));
Avatar billede webzone Nybegynder
08. juni 2009 - 18:29 #8
hvor skal jeg indsætte den?
Jeg kan ikke rigtig få den til at virke i mine echo linier og har også prøvet at sætte den ind under den kode som du lagde på. lige under
          $snit = ($count > 0? $total/$count: 0);
men den vil ikke rigtig som jeg vil
Avatar billede acore Ekspert
08. juni 2009 - 19:39 #9
Der hvor du nu udskriver $snit står der sikkert

echo("....".$snit."....);

hvor prikkerne kan være alt muligt.

Det skal nu være

echo("....".numberformat($snit, 2)."....);
Avatar billede webzone Nybegynder
08. juni 2009 - 21:43 #10
Jeg har ikke nogle direkte resultatlinier med echo. det hele skrives til en database og hentes så derfra. det kan da godt være det ikke er den rigtige måde at gøre det på..
Jeg prøver lige at poste hele update funktionen den indeholder også echo delen. er der ikke en venlig sjæl der vil putte den ind det rigtige sted. for jeg kan vist ikke rigtig få det til at virke.
Den regner fint nok men den mangler de 2 decimaler i .$data['snit'].


    case 'update':
if (isset($_POST['update_shooter']))
{
            $num_flds = array(
                        'res01', 'res02', 'res03', 'res04', 'res05', 'res06', 'res07', 'res08', 'res09', 'res10',
                        'res012', 'res022', 'res032', 'res042', 'res052', 'res062', 'res072', 'res082', 'res092', 'res102'
            );

            $total = 0;
            $count = 0;
            foreach($num_flds as $fld)
            {
                        if (isset($_POST[$fld]) && $_POST[$fld] > 0)
                        {
                                    $total += $_POST[$fld];
                                    $count++;
                        }
            }

            $snit = ($count > 0? $total/$count: 0);



        dbquery("UPDATE ".DB_STAT1." SET name='".$_POST['name']."', `pos`='".$_POST['pos']."', dob='".$_POST['dob']."', height='".$_POST['height']."', weight='".$_POST['weight']."', hand='".$_POST['hand']."', res01='".$_POST['res01']."'+'".$_POST['res012']."', res02='".$_POST['res02']."'+'".$_POST['res022']."', res03='".$_POST['res03']."'+'".$_POST['res032']."', res04='".$_POST['res04']."'+'".$_POST['res042']."', res05='".$_POST['res05']."'+'".$_POST['res052']."', res06='".$_POST['res06']."'+'".$_POST['res062']."', res07='".$_POST['res07']."'+'".$_POST['res072']."', res08='".$_POST['res08']."'+'".$_POST['res082']."', res09='".$_POST['res09']."'+'".$_POST['res092']."', res10='".$_POST['res10']."'+'".$_POST['res102']."', total=".$total.", snit=".$snit.", info='".$_POST['info']."', pic='".$_POST['pic']."', active='".$_POST['active']."', info_breaks='".$_POST['info_breaks']."' WHERE id='".$id."'");
        redirect (FUSION_SELF.$aidlink, "script");
}
    else {
        $result = dbquery("SELECT * FROM ".DB_STAT1." WHERE id=".$id."");
        $data = dbarray($result);

echo'<form name="addcat" action='.FUSION_SELF.$aidlink.'&axn=update method="post" style="margin:2em;">
<table align="center" width="780" cellspacing="0" cellpadding="0" border="0">
<input type="hidden" name="id" value="'.$data['id'].'">
<input type="hidden" name="name" value="'.$data['name'].'">
<input type="hidden" name="pos" value="'.$data['pos'].'">
<input type="hidden" name="dob" value="'.$data['dob'].'">
<input type="hidden" name="height" value="'.$data['height'].'">
<input type="hidden" name="weight" value="'.$data['weight'].'">
<input type="hidden" name="hand" value="'.$data['hand'].'">
<input type="hidden" name="res01" value="'.$data['res01'].'">
<input type="hidden" name="res02" value="'.$data['res02'].'">
<input type="hidden" name="res03" value="'.$data['res03'].'">
<input type="hidden" name="res04" value="'.$data['res04'].'">
<input type="hidden" name="res05" value="'.$data['res05'].'">
<input type="hidden" name="res06" value="'.$data['res06'].'">
<input type="hidden" name="res07" value="'.$data['res07'].'">
<input type="hidden" name="res08" value="'.$data['res08'].'">
<input type="hidden" name="res09" value="'.$data['res09'].'">
<input type="hidden" name="res10" value="'.$data['res10'].'">
<input type="hidden" name="res10" value="'.$data['total'].'">
<input type="hidden" name="res10" value="'.$data['snit'].'">
<input type="hidden" name="info" value="'.$data['info'].'">
<input type="hidden" name="pic" value="'.$data['pic'].'">
<input type="hidden" name="active" value="'.$data['active'].'">
<input type="hidden" name="info_breaks" value="'.$data['info_breaks'].'">
<tr>
    <td colspan="10" align="center" class="tbl2">'.$data["name"].'</td>
</tr>
<tr>
    <td colspan="10" align="center"><br></td>
</tr>
<tr>
    <td align="center" class="tbl2"><img src="images/skive.gif"></td>
    <td align="center" class="tbl2"><img src="images/skive.gif"></td>
    <td align="center" class="tbl2"><img src="images/skive.gif"></td>
    <td align="center" class="tbl2"><img src="images/skive.gif"></td>
    <td align="center" class="tbl2"><img src="images/skive.gif"></td>
    <td align="center" class="tbl2"><img src="images/skive.gif"></td>
    <td align="center" class="tbl2"><img src="images/skive.gif"></td>
    <td align="center" class="tbl2"><img src="images/skive.gif"></td>
    <td align="center" class="tbl2"><img src="images/skive.gif"></td>
    <td align="center" class="tbl2"><img src="images/skive.gif"></td>
    <td align="center" class="tbl2"><img src="images/win.gif"></td>
    <td align="center" class="tbl2"><img src="images/win.gif"></td>
</tr>
<tr>
    <td align="center">'.$data["res01"].'</td>
    <td align="center">'.$data["res02"].'</td>
    <td align="center">'.$data["res03"].'</td>
    <td align="center">'.$data["res04"].'</td>
    <td align="center">'.$data["res05"].'</td>
    <td align="center">'.$data["res06"].'</td>
    <td align="center">'.$data["res07"].'</td>
    <td align="center">'.$data["res08"].'</td>
    <td align="center">'.$data["res09"].'</td>
    <td align="center">'.$data["res10"].'</td>
    <td align="center">'.$data["total"].'</td>
    <td align="center">'.$data["snit"].'</td>
</tr>
<tr>
    <td align="center">+</td>
    <td align="center">+</td>
    <td align="center">+</td>
    <td align="center">+</td>
    <td align="center">+</td>
    <td align="center">+</td>
    <td align="center">+</td>
    <td align="center">+</td>
    <td align="center">+</td>
    <td align="center">+</td>
</tr>
<tr>
    <td align="center"><input type="text" class="textbox" name="res012" id="res012" size="5" maxlength="4" value="0"></td>
    <td align="center"><input type="text" class="textbox" name="res022" id="res022" size="5" maxlength="4" value="0"></td>
    <td align="center"><input type="text" class="textbox" name="res032" id="res032" size="5" maxlength="4" value="0"></td>
    <td align="center"><input type="text" class="textbox" name="res042" id="res042" size="5" maxlength="4" value="0"></td>
    <td align="center"><input type="text" class="textbox" name="res052" id="res052" size="5" maxlength="4" value="0"></td>
    <td align="center"><input type="text" class="textbox" name="res062" id="res062" size="5" maxlength="4" value="0"></td>
    <td align="center"><input type="text" class="textbox" name="res072" id="res072" size="5" maxlength="4" value="0"></td>
    <td align="center"><input type="text" class="textbox" name="res082" id="res082" size="5" maxlength="4" value="0"></td>
    <td align="center"><input type="text" class="textbox" name="res092" id="res092" size="5" maxlength="4" value="0"></td>
    <td align="center"><input type="text" class="textbox" name="res102" id="res102" size="5" maxlength="4" value="0"></td>
</tr>
<tr>
    <td colspan="10" align="center"><br></td>
</tr>
<tr>
    <td colspan="10" align="center"><input type="submit" name="update_shooter" value='.$locale['srl_028'].' class="button"/></td>
</tr>
</table></form>';

       
    }
    break;
Avatar billede acore Ekspert
08. juni 2009 - 23:44 #11
Hvis der er en decimal-del af $snit, som ikke er 0, så kommer den også med, hvis snit i databasen er defineret som et decimal-tal. Og så behøver du ikke gøre noget.

Hvis der er en decimal-del af $snit, der ikke er 0, men som ikke lagres i tabellen, så hjælper ovenstående ikke.

Er det fordi du vil afrunde $snit til 2 decimaler før du gemmer, og i så fald hvorfor?

Hvilken datatype er snit i tabellen?
Avatar billede webzone Nybegynder
08. juni 2009 - 23:57 #12
hehe ups jeg havde ikke lige set at deci malen var sat til 4,0 så er det jo klart at det ikke virkede.
Jeg har nu ændret i databasen og det ser ud til at virke.
jeg tildeler point og siger mange tak for en super god support
Avatar billede Ny bruger Nybegynder

Din løsning...

Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.

Loading billede Opret Preview
Kategori
Vi tilbyder markedets bedste kurser inden for webudvikling

Log ind eller opret profil

Hov!

For at kunne deltage på Computerworld Eksperten skal du være logget ind.

Det er heldigvis nemt at oprette en bruger: Det tager to minutter og du kan vælge at bruge enten e-mail, Facebook eller Google som login.

Du kan også logge ind via nedenstående tjenester