I'm stack in a php code that i want to find the total but i don't know what i did wrong and the total comes alway on "0".

My code like a order list. You check on what thing you need and how many do you want. You order it. It will apper how much the total cost

Here is my code:

The index code:

<form action="processorder.php" method="POST">
            Nafn: <input name="nafn" type="text" size ="10"><br>
            Kennitala: <input name="kennitala" type="text" size="10"><br>
            Heimilisfang: <input name="heimilis" type="text" size="15"><br>
            Netfang: <input name="netfang" type="text" size="10"><br>
            Sími: <input name="simi" type="text" size="8"><br>
            <table>
            Vörur: <input name="vorur" type="hidden">
                <tr>
                    <td></td><td>ID</td><td>Vörur</td><td>Verð</td><td>Magn/Verð</td><td>Magn</td>
                </tr>
            <?php
            //Opna vorulist.txt skrá
                $data = 'vorulist.txt';
                $fp = fopen('vorulist.txt','rb');
                $i=0;
            //Lesa eina linu í einu
                while(($line=fgets($fp)) !==false)
                {
                    $list=explode(' ',$line);
                echo '<tr><td>
                <input type="checkbox" name="mychkbox'.$i.'" /></td>
                <td><input type="text" name="myid'.$i.'" size="5" value="'.$list[0].'" readonly /></td>
                <td><input type="text" name="myproductname'.$i.'" size="30" value="'.$list[1].'" readonly /></td>
                <td><input type="text" name="myprice'.$i.'" size="5" value="'.$list[2].'" readonly /></td>
                <td><input type="text" name="magn/verd'.$i.'" size="5" value="'.$list[3].'" readonly /></td>
                <td><input type="text" name="magn'.$i.'" size="2" /></td>
                </tr>';
                $i++;
                }
                $size = $i;
                echo '<input type="hidden" name="size" />';
            ?>
            </table>
            <br>
            <input type="submit" value="Staðfest">
        </form>

Here is the code on php:

date_default_timezone_set('UTC');
$nafn = $_POST['nafn'];
$kennitala = $_POST['kennitala'];
$heimilis = $_POST['heimilis'];
$netfang =$_POST['netfang'];
$simi = $_POST['simi'];
$heildarverd = 0;
//Lesa inn vorur
$size = $_POST['size'];
for($i=0;$i<$size;$i++)
{
    $mychkbox =trim($_POST['mychkbox'.$i]);
    $myid = trim($_POST['myid'.$i]);
    $myproduckt = trim($_POST['myproductname'.$i]);
    $myprice = trim($_POST['myprice'.$i]);
    $mymagnverd = trim($_POST['magn/verd'.$i]);
    $mymagn = trim($_POST['magn'.$i]);
//Tékka á checkbox is on or off
    if ($mychkbox=='on')
    {
        $heildarverd = $heildarverd + ((int)$myprice)*((int)$mymagn);
        $totalorder = $totalorder.$myid.' '.$myproduckt.' '.$myprice.' '.$mymagnverd.' '.$mymagn.'<br>';
        
    }
}

//Sýna á vefinn

echo $totalorder;
echo 'Heildarverð: '.$heildarverd;

Here is some thing's list i did ( just for test)

201 Appelsínur 199 kr/kg
202 Epli(Grænn) 239 kr/kg
203 Epli(Rauð) 189 kr/kg
204 Kiwi 359 kr/kg
205 Mangó 529 kr/kg

Somebody help me plz. What did i do wrong?

GaBack

You have missed something that assigning the size values in to the hidden element.

echo '<input type="hidden" name="size" />';

to be

echo '<input type="hidden" name="size" value="'.$size.'" />';

and assign $totalorder=0 before starting the for loop.

thx alot

it's solved

GaBack

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.