0

I am defining the subtraction variable like so:

    $query = "
    SELECT * FROM around WHERE `long` != '' ORDER BY id DESC"; 
    $result = mysql_query( $query );

    $select_maxlong = mysql_query("SELECT long FROM around WHERE
    `id`='$max_id'");
    $row3 = mysql_fetch_row($select_maxlong);
    $max_long = $row3[0];

Then I need to subtract $max_long from the array value below:

   while ( $row = mysql_fetch_assoc( $result ) ) {

   echo $row['long'];}

I have tried:

   foreach($row as $row) {
   $car = $max_long;
   echo "u";
   echo $car - $row['long'] ; }

But the output is not correct.

If there are 10 numbers in an array, $row['long'], I want to subtract $max_long from each of them, and echo the result.

20
  • 1
    And what is the output + what output do you expect? Commented Feb 12, 2015 at 20:23
  • 2
    one number of WHAT? You're fetching multiple rows of data, and subtracting a value from each of those rows. you'd have to explain what this "one number" should be, because right now your code is working perfectly well as-written. Commented Feb 12, 2015 at 20:25
  • 1
    foreach($row as $row) Whoa! Commented Feb 12, 2015 at 20:27
  • 1
    How do you think if initially your $row is an array and you're doing foreach, then on first iteration $row will become a first item of $row. And what's next? Commented Feb 12, 2015 at 20:30
  • 1
    There is no more support for mysql_* functions, they are officially deprecated, no longer maintained and will be removed in the future. You should update your code with PDO or MySQLi to ensure the functionality of your project in the future. Commented Feb 12, 2015 at 20:42

1 Answer 1

1

This code should work for you

$select_maxlong = mysql_query("SELECT long FROM around WHERE `id`='$max_id'");
$row3 = mysql_fetch_row($select_maxlong);
$max_long = $row3[0];

$myarray=array();
while ( $row = mysql_fetch_assoc( $result ) ) {
   $myarray[]=$row['long'];
}

function showvalue($element, $key) {
  echo $max_long-$element;
}

array_walk($myarray,'showvalue');

But this way, you are substracting the element from $max_long, and not otherwise as you said (that's what your code seems to try). If you want to really substract $max_long from each element of the array and output the result, just invert the variable order on the echo line. Hope this helps.

Sign up to request clarification or add additional context in comments.

11 Comments

Can the output be placed inside ( $row = mysql_fetch_assoc( $result ) ) { } I am trying to use JSON.
The calculation worked here, but it only calculates it once. If there are 100 posts, I need it to run 100 times.
Then you don't need to create a new array. Just do echo $max_long-$row['long']; inside the while() {...} block
I don't think I understand... this function will echo one result per element of the original array. If there are 100, it will output 100 values.
The result is 0 when I do that.
|

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.