calculation for dynamically added rows - Multiple

Discussion in 'PHP' started by radagast, Oct 27, 2011.

  1. #1
    Hi

    I am hoping that some one could please help as I have been struggling with this for a very long time.
    1. I have a dynamic form where customers can add rows so that they can input dimension and weights of a parcel/s.
      They then request a price on request I do the following:

      <?php
      for($i=0; $i<count($height); $i++){
      echo "<tr><td>$i</td> <td>$selRow0[$i]</td> <td>$height[$i] cm x $length[$i] cm x $width[$i] cm</td> <td>$mass[$i]</td>";
      $volume = $height[$i] * $length[$i] * $width[$i];
      echo "<td>$volume</td></tr>";

      $totalv = $volume + $volume[$i];
      $totalm = $mass + $mass;
      echo "$totalv , $totalm";

      }
      ?>

      The code in green is working fine.
      But the red code is what I am having trouble with.

      What I would like it to do is add all the $volume (per row) together to get $totalv and add all the $mass[$i] together to get $totalm


      example $volume_mass = 50(dimm1) *50 (dimm2) * 50 (dimm3);
      so that means $volume_mass = 125000
      I then take the total of the weight - $actual_mass = 20(weight)

      I am doing the following query to see what service are available

      $data = "SELECT * FROM rates WHERE hub_from = 'nxd' AND hub_to = 'auk'";
      $result = mysql_query($data)
      or die ("Error in query: $data. " . mysql_error());

      if (mysql_num_rows($result) > 0)
      {

      while($row = mysql_fetch_object($result))



      }
      ?>

      So I get multiple results and I need to now work out the cost per result.

      Example of results from rates table

      $service $volume $base $min_kg $per_kg
      AIR 5000 70.00 5 4
      ECO 4000 80.00 10 4
      LOX 5000 45.00 1 35
      LSE 5000 45.00 1 35
      OVN 5000 70.00 10 4

      Know I need to do the following per result (per service)
      • need to the volume factor from the service - &totalv = $volume_mass / $volume (from the rates table)
      • Check if which is now greater between $actual_mass and $totalv - $charge_mass = $actual_mass <> $totalv
      • then need to calculate the costs - if $charge_mass <= $min_kg then $total_cost = $base echo "$total_cost" else if $charge_mass > $min_kg then $total_cost = ($charge_mass - $min_kg) *$per_kg + $base echo "$total_cost"


      Any help would be greatly appreciated
     
    radagast, Oct 27, 2011 IP
  2. mfscripts

    mfscripts Banned

    Messages:
    319
    Likes Received:
    4
    Best Answers:
    8
    Trophy Points:
    90
    Digital Goods:
    3
    #2
    Hi,

    I'm not sure I followed all of your detail but this definately looks wrong...

    First you set this:

    $volume = $height[$i] * $length[$i] * $width[$i];

    Then $totalv to this:

    $totalv = $volume + $volume[$i];

    I'm guessing you don't mean the $volume[$i]? Is $totalv an overall volume total? So more like $totalv = $totalv + $volume.
     
    mfscripts, Oct 28, 2011 IP
  3. radagast

    radagast Peon

    Messages:
    7
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Hi mfscripts

    First form for user is the quote page


    <hmtl>
    <head>
    <script>
    function addRowToTable()
    {
    var tbl = document.getElementById('quote_details');
    var lastRow = tbl.rows.length;
    // if there's no header row in the table, then iteration = lastRow + 1
    var iteration = lastRow;
    var row = tbl.insertRow(lastRow);

    // Parcel ID
    var parcel_id = row.insertCell(0);
    var textNode = document.createTextNode(iteration);
    parcel_id.appendChild(textNode);


    // Goods Discription
    var discription = row.insertCell(1);
    var sel = document.createElement('select');
    sel.name = 'selRow0[]' + iteration;
    sel.options[0] = new Option('Box', 'Box');
    sel.options[1] = new Option('Flyer', 'Flyer');
    sel.options[2] = new Option('Something big', 'Something big');
    discription.appendChild(sel);


    // Height
    var height = row.insertCell(2);
    var el1 = document.createElement('input');
    el1.type = 'text';
    el1.name = 'height[]' + iteration;
    el1.id = 'vol' + iteration;
    el1.size = 5;


    height.appendChild(el1);

    // Length
    var length = row.insertCell(3);
    var el2 = document.createElement('input');
    el2.type = 'text';
    el2.name = 'length[]' + iteration;
    el2.id = 'vol' + iteration;
    el2.size = 5;


    length.appendChild(el2);

    // Width
    var width = row.insertCell(4);
    var el3 = document.createElement('input');
    el3.type = 'text';
    el3.name = 'width[]' + iteration;
    el3.id = 'vol' + iteration;
    el3.size = 5;


    width.appendChild(el3);

    // Mass
    var mass = row.insertCell(5);
    var el4 = document.createElement('input');
    el4.type = 'text';
    el4.name = 'mass[]' + iteration;
    el4.id = 'mass' + iteration;
    el4.size = 5;


    mass.appendChild(el4);




    }


    function removeRowFromTable()
    {
    var tbl = document.getElementById('quote_details');
    var lastRow = tbl.rows.length;
    if (lastRow > 2) tbl.deleteRow(lastRow - 1);
    }
    </script>


    </head>


    <body>


    <!-- frame -->
    <table class = "frame" border = "0" cellpadding = "0" cellspacing = "0">
    <tr>
    <td>


    <!-- container -->
    <table class = "container" align = "center" border = "0" cellpadding = "0" cellspacing = "0">
    <tr>
    <td>


    <!-- site -->
    <!-- Header -->
    <table border = "0" width = "740px" cellpadding = "0" cellspacing = "0">
    <tr>
    <td width = "294px" height = "45px">


    <table border = "0" cellpadding = "0" cellspacing = "0">
    <tr>
    </tr>
    </table>

    </td>
    </tr>

    </table>
    <!-- End Header -->


    <!-- Content Table -->
    <table class = "contentTable" border = "0" width = "740px" cellpadding = "0" cellspacing = "0">
    <tr>
    <td width = "740px">
    <p></p>


    <!-- ## CONTENT ## -->
    <table border = "0" width = "740px" cellpadding = "0" cellspacing = "0">
    <tr>
    <td valign = "top" align = "center">


    <!-- @ @ CONTENT BOX @ @ -->
    <table border = "0" width = "716px" cellpadding = "0" cellspacing = "0">
    <tr>
    <td valign = "top">



    <form name="quote_form" id="quote_form" action="quote2.php" method="get">

    <table border = "1" width = "100%" cellpadding = "0" cellspacing = "0">
    <p></p>
    <tr>

    <td colspan = "3" class = "featureBox">

    <h2>Online Quote</h2>

    <table border = "0" width = "100%" class = "plainCopy" cellpadding = "2" cellspacing = "2">

    <tr>

    <td>



    <fieldset>
    <legend><b>Shipping From/To</b></legend>
    <table border = "0" width = "100%" cellpadding = "2" cellspacing = "2" class="quoteCopy">
    <tr>
    <td>
    <td>From:</td>
    <td><?php
    $query = "SELECT * FROM places";
    $result = mysql_query($query)
    or die ("Couldn’t execute query.");
    /* create form containing selection list */
    echo "<select name='fromhub'><option value = ''>Select one...</option>\n";
    while ($row = mysql_fetch_array($result))
    {
    extract($row);
    echo "<option value = '$Town,$Area'>$Town,$Area\n";
    }
    echo "</select>\n";
    ?>

    <td>To:</td>
    <td><?php
    $query = "SELECT * FROM places";
    $result = mysql_query($query)
    or die ("Couldn’t execute query.");
    /* create form containing selection list */
    echo "<select name='tohub'><option value = ''>Select one...</option>\n";
    while ($row = mysql_fetch_array($result))
    {
    extract($row);
    echo "<option value = '$Town,$Area'>$Town,$Area\n";
    }
    echo "</select>\n";
    ?></td></td>
    </tr>
    </table>
    <p></p>



    <p>
    </p>
    <table border="0" id="quote_details" class="plainCopy">

    <tr>
    <td>Item No.</td>
    <td>Discription</td>
    <td>Height(cm)</td>
    <td>Length(cm)</td>
    <td>Width(cm)</td>
    <td>Weight(kg)</td>
    </tr>
    <tr>
    <td>1</td>

    <td>
    <select name="selRow0[]">
    <option value="Box">Box</option>
    <option value="Flyer">Flyer</option>
    <option value="Something big">something big</option>
    </select>
    </td>

    <td><input type="text" name="height[]" id="height" size="5"/></td>
    <td><input type="text" name="length[]" id="length" size="5"/></td>
    <td><input type="text" name="width[]" id="width" size="5"/></td>
    <td><input type="text" name="mass[]" id="mass" size="5"/></td>

    </tr>

    </table>

    </table>

    </fieldset>
    <p></p>
    <input type="button" class = "cmdInput" value="Add item" onclick="addRowToTable();" />
    <input type="button" class = "cmdInput" value="Remove last" onclick="removeRowFromTable();" />


    <p></p>


    <input type = "submit" class = "cmdInput" id = "cmd_Submit" name = "cmd_Submit" value = "Get Quote">


    <p></p>

    </table>
    </form>
    </td>
    </tr>
    </table>

    </td>
    </tr>
    </table>
    <!-- @ @ END CONTENT BOX @ @ -->


    </td>
    </tr>
    </table>
    <!-- ## END Contact Us Content ## -->


    </td>
    </tr>
    </table>
    <!-- END Content Table -->


    <!-- Footer -->
    <table border = "0" width = "740px" cellpadding = "0" cellspacing = "0">

    </table>
    <!-- End Footer -->


    <!-- END site -->


    </td>
    </tr>
    </table>
    <!-- END container -->




    </body>


    </html>


    Then on submit it goes to the quote2 page

    <hmtl>
    <head>



    </head>


    <body>


    <!-- frame -->
    <table class = "frame" border = "0" cellpadding = "0" cellspacing = "0">
    <tr>
    <td>


    <!-- container -->
    <table class = "container" align = "center" border = "0" cellpadding = "0" cellspacing = "0">
    <tr>
    <td>


    <!-- site -->
    <!-- Header -->
    <table border = "0" width = "740px" cellpadding = "0" cellspacing = "0">
    <tr>
    <td width = "294px" height = "45px">


    <table border = "0" cellpadding = "0" cellspacing = "0">
    <tr>
    </tr>
    </table>

    </td>
    </tr>

    </table>
    <!-- End Header -->


    <!-- Content Table -->
    <table class = "contentTable" border = "0" width = "740px" cellpadding = "0" cellspacing = "0">
    <tr>
    <td width = "740px">
    <p></p>


    <!-- ## CONTENT ## -->
    <table border = "0" width = "740px" cellpadding = "0" cellspacing = "0">
    <tr>
    <td valign = "top" align = "center">


    <!-- @ @ CONTENT BOX @ @ -->
    <table border = "0" width = "716px" cellpadding = "0" cellspacing = "0">
    <tr>
    <td valign = "top">



    <form name="quote_form" id="quote_form">


    <table border = "1" width = "100%" cellpadding = "0" cellspacing = "0">

    <tr>

    <td colspan = "3" class = "featureBox">

    <h1>Online Quote</h1>

    <table border = "0" width = "100%" class = "plainCopy" cellpadding = "2" cellspacing = "2">

    <tr>

    <td>



    <fieldset>
    <legend><b>Quotation Information</b></legend>
    <table border = "0" width = "100%" cellpadding = "2" cellspacing = "2" class="quoteCopy">
    <tr>
    <td>From</td>
    <td><span name="collect_from" id="collect_from"<font color="#FF6600"><?php echo $_GET["fromhub"]; ?></span></td>
    <td>To</td>
    <td><span name="deliver_to" id="deliver_to"<font color="#FF6600"><?php echo $_GET["tohub"]; ?></span></td>


    <table border = "0" width = "100%" class = "plainCopy" cellpadding = "2" cellspacing = "2">

    <tr>
    <td>Item No</td>
    <td>Description</td>
    <td>Dimensions (cm) (H x L x D)</td>
    <td>Weight (kg) </td>
    </tr>

    </td>
    <?php
    $height=$_GET['height'];
    $length=$_GET['length'];
    $width=$_GET['width'];
    $mass=$_GET['mass'];
    $selRow0=$_GET['selRow0'];





    echo "<td>";

    for($i=0; $i<count($height); $i++){
    echo "<tr><td>$i</td> <td>$selRow0[$i]</td> <td>$height[$i] cm x $length[$i] cm x $width[$i] cm</td> <td>$mass[$i]</td>";
    $volume = $height[$i] * $length[$i] * $width[$i];
    echo "<td>$volume</td></tr>";
    $totalv = "?"; //// Need to add all volume row results
    $totalm = "?"; //// Need to add all mass row results
    }

    echo "</td>";





    echo "</tr>";

    echo "</table>";

    echo "</table>";

    echo "</fieldset>";

    echo "$totalv , $totalm";
    ?>
    <div id= "editbutton" align="right" style="display: ;">
    <a href = "onlinegetquote.php"><input type = "submit" class = "cmdInput" id = "cmd_Submit" name = "cmd_Submit" value = "New Quote"> </a>
    </div>
    <p></p>
    <fieldset>
    <legend><b>Cost & Services Offered</b></legend>
    <table border = "0" width = "100%" class = "plainCopy" cellpadding = "2" cellspacing = "2">

    <tr>
    <td><b>Service<b></td>
    <td><b>Transit Times<b></td>
    <td><b>Cost<b></td>
    </tr>

    <?php

    //split Town & Area
    $fromhub = $_GET["fromhub"];
    list ($ftown,$fhub)= explode(",", $fromhub);

    $tohub = $_GET["tohub"];
    list ($ttown,$thub)= explode(",", $tohub);



    // Connects to your Database
    mysql_connect("localhost", "test", "test") or die(mysql_error());
    mysql_select_db("online") or die(mysql_error());
    $data = "SELECT *, ($totalv/`volume`) as volume_mass
    FROM rates
    WHERE hub_from = 'nxd'
    AND hub_to = 'auk'"
    ;
    $result = mysql_query($data)
    or die (
    "Error in query: $data. " . mysql_error());

    while(
    $row = mysql_fetch_object($result))
    {
    $charge_mass = max($actual_mass, $row['volume_mass']);
    $overage = max($charge_mass - $row['min_kg'], 0);
    $total_cost = $row['base'] + ($overage * $row['per_kg']);
    echo
    "{$row['service']}: {$total_cost}<br>\n";
    }
    ?>

    //////Above code not working????????

    </table>

    </fieldset>

    </table>
    </form>


    Example of results from rates table
    • $service $volume $base $min_kg $per_kg
      AIR 5000 70.00 5 4
      ECO 4000 80.00 10 4
      LOX 5000 45.00 1 35
      LSE 5000 45.00 1 35
      OVN 5000 70.00 10 4



      Hope This helps
     
    radagast, Oct 28, 2011 IP
  4. radagast

    radagast Peon

    Messages:
    7
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Hi

    Managed to solve my problem

    $totalv += $volume[$i];

    Thanks
     
    radagast, Nov 5, 2011 IP