#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2010
    Posts
    4
    Rep Power
    0

    Question How to apply formula on posted array values?


    Page1:

    PHP Code:


    echo"<table border=1>";
        
    echo
    "<form method='post' name='data_form' id='form1' action='data_validate.php'>";


    for(
    $i=1$i<=2$i++)

    {

    for(
    $j=1$j<=3$j++)

    {
        
     echo 
    "<th> <input type='text' name='x[]' size='1'> </th>";
     
    }

    }


    echo
    "</tr> </form> </table>"


    Page2 (data_validate.php):

    PHP Code:


    $x 
    $_POST['x'];
        


    $counter=0;    
        
        
    echo
    "<table border=1>";


    for(
    $i=1$i<=2$i++)

    {

    for(
    $j=1$j<=3$j++)

    {
     
        
    $result=( ($x[$counter])  / (???????????) );
        
    echo
    "<th>".$result."</th>";
        
    ++
    $counter;
     
    }

    }


    echo
    "</tr> </table>"


    I posted the values of a form in an array named x[] from page1 to page2
    where i want to apply a certain formula on them.
    the formula is as following: $result=$x[$counter] / (max($x[$counter]));

    that is, it takes the value of each element in array x and divides it by the largest x (in the inner loop iterations)

    To clarify more lets imagine that we posted x[0]=2, x[1]=4, x[2]=5

    Now the formula will be: 2/5, 4/5, and 5/5 (that was while $i was equal to 1 and it looped through $j=1,2,3)

    But when $i=2 here i want to divide by the largest value from the second iteration and so on..

    To explain lets resume from where we stopped: x[3]=1, x[4]=3, x[5]=2. here the formula is supposed to be:

    1/3, 3/3, and 2/3 and not 1/5, 3/5 etc.. Did i explain my idea clear? i placed question marks in the code

    where i need help.

    Your cooperation is appreciated indeed..
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jul 2003
    Posts
    4,290
    Rep Power
    630
    If I understand correctly, I think this is what you want.
    PHP Code:
    $x $_POST['x'];    
        
        
    echo
    "<table border=1>";


    for(
    $i=1$i<=2$i++)

    {
    $divide=$x[$i];

    for(
    $j=1$j<=3$j++)

    {
     
        
    $result=( ($x[$j])  / $divide );
        
    echo
    "<th>".$result."</th>";
        
    ++
    $counter;
     
    }

    }


    echo
    "</tr> </table>"
    Note that you may need to check for $divide=0.
    Last edited by gw1500se; July 2nd, 2017 at 07:42 AM.
    There are 10 kinds of people in the world. Those that understand binary and those that don't.
  4. #3
  5. Wiser? Not exactly.
    Devshed God 2nd Plane (6000 - 6499 posts)

    Join Date
    May 2001
    Location
    Bonita Springs, FL
    Posts
    6,038
    Rep Power
    4101
    First, split your array into chunks of equal size, then you can get the max of each chunk and loop through the values to do your calculation.

    For example:
    PHP Code:
    <?php


    $chunks 
    3;
    $chunkSize 3;


    if (isset(
    $_POST['x'])){
        
    $result = [];
        
    $chunkedX array_chunk($_POST['x'], $chunkSize);
        foreach (
    $chunkedX as $x){
            
    $max max($x);
            foreach (
    $x as $value){
                
    $result[] = $value $max;
            }
        }
    }
    ?>
    <!DOCTYPE html>
    <html>
    <head><title>Array chunking</title></head>
    <body>
        <form method='post' name='data_form' id='form1' action=''>
        <table border="1">
        <tr>
        <?php for ($i=0$i $chunks $chunkSize$i++): ?>
            <th><input type='text' name='x[]' size='1'></th>
        <?php endfor; ?>
        </tr>
        <?php if (isset($result)): ?>
            <tr>
            <?php foreach ($result as $value): ?>
                <td><?=$value?></td>
            <?php endforeach; ?>
            </tr>
        <?php endif; ?>
        </table>
        <button type="submit">Submit</button>
        </form>  
    </body>
    </html>
    Recycle your old CD's



    If I helped you out, show some love with some reputation, or tip with Bitcoins to 1N645HfYf63UbcvxajLKiSKpYHAq2Zxud
  6. #4
  7. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2016
    Location
    Lakewood, WA
    Posts
    192
    Rep Power
    17
    This looks really interesting, and I'm curious what the purpose / application of this process is? Can you give me an example of your use (or an abstraction if you don't want to do that)?
    Last edited by Arty Zifferelli; July 2nd, 2017 at 01:54 PM.

IMN logo majestic logo threadwatch logo seochat tools logo