1

I'm trying to create some PHP that will perform a calculation from two sequences of input text.

If the user enters 9,8,2 in one input, and 5,2,3 in other it will perform a calculation.

Anyway, I've gotten very close but the calculation is showing commas and I'm not sure how to get rid of these commas from the array. Any help would be greatly appreciated.

$first = $_POST['first'];
$second= $_POST['second'];

$maths = $first[0] * $second[0] + $first[0] * $second[1] + $first[0] * $second[2] + $first[0] * $second[3] + $first[0] * $second[4] + $first[0] * $second[5] + $first[1] * $second[0] + $first[1] * $second[1] + $first[1] * $second[2] + $first[1] * $second[3] + $first[1] * $second[4] + $first[1] * $second[5] + $first[2] * $second[0] + $first[2] * $second[1] + $first[2] * $second[2] + $first[2] * $second[3] + $first[2] * $second[4] + $first[2] * $second[5];
$result = "$first[0] * $second[0] + $first[0] * $second[1] + $first[0] * $second[2] + $first[0] * $second[3] + $first[0] * $second[4] + $first[0] * $second[5] + $first[1] * $second[0] + $first[1] * $second[1] + $first[1] * $second[2] + $first[1] * $second[3] + $first[1] * $second[4] + $first[1] * $second[5] + $first[2] * $second[0] + $first[2] * $second[1] + $first[2] * $second[2] + $first[2] * $second[3] + $first[2] * $second[4] + $first[2] * $second[5]";

echo "</br>";
echo "the question is $result";
echo "</br>";
echo  "the result is $maths ";
4
  • 3
    Are you in fact needing to explode your POST params first? Commented Nov 2, 2015 at 16:04
  • @JonStirling I am quite new to PHP, I'm unsure of what explode is. Would that ignore the commas for the input data? Commented Nov 2, 2015 at 16:07
  • Read the link and that will explain. Whether new or old, the PHP documentation is your friend :) Commented Nov 2, 2015 at 16:07
  • @JonStirling Yeah, I've been reading up as much as I can. Sometimes the terminology can be overwhelming and I get buried in detail. I've been working on PHP for a few weeks, usually I can eventually find a fix but this time I got really stuck. Thanks for the help. :) Commented Nov 2, 2015 at 16:12

2 Answers 2

1
$first = explode(',', $_POST['first']);
$second= explode(',', $_POST['second']);

Now you got two real arrays with the data like:

[1,2,3,4]
Sign up to request clarification or add additional context in comments.

Comments

0

I'm not sure that the example stated above actually returns the correct value, though as has been proved before I could well be wrong. Using the numbers above and the way in which you have shown the calculation that makes 135.. rather than 60.

    $sum=0;
    $question=array();

    $first=explode(',',$_POST['first']);
    $second=explode(',',$_POST['second']);

    for( $i=0; $i < count( $first ); $i++ ){

        $row=array();
        $tmp=array();

        for( $j=0; $j < count( $second ); $j++ ){
            $tmp[]='( '.$first[ $i ]. ' x '. $second[ $j ].' )';
            $row[]=$first[ $i ] * $second[ $j ];
        }
        $sum+=array_sum( $row );
        $question[]=implode( ' + ', $tmp );
    }
    echo 'The question is: [ ' . implode( ' ] + [ ', $question ).' ]<br />';
    echo 'Total: '.$sum;

1 Comment

Your code works much better, actually. The example above worked for me after I made a few other changes, however I was unable to add extra numbers. It did however fix my comma issue. I did get the correct answer, but I couldn't add more numbers as it just wouldnt do anything with those numbers.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.