Author Topic: [SOLVED] Odd result from mysql query into an array  (Read 653 times)

Offline Emicarn

  • Level 5
  • *
  • Posts: 19
  • Reputation: +0/-0
    • View Profile
[SOLVED] Odd result from mysql query into an array
« on: December 19, 2010, 08:00:21 AM »
Good Morning.

I've been slowly working on a game I've had in mind for some time.  Figured I better get on it before senility sets in.. anyway....

What I've run into is an mysql query to return all players, I then compute the distance based on [x,y] coordinates, and dump into an array.  

The Code:

Quote

$currentid = $_COOKIE['spacewarid'];
   $getfleet = 'select * from players';
   $dofleet = mysql_query($getfleet, $dbconnect);

      echo '<div id="status-screen-sensors">';


      $i = 0;


      while ($ships = mysql_fetch_array($dofleet))
      {
         if ($ships['id'] != $currentid)
            {
               $currentdistance = round(sqrt( pow(($ships['pos_x'] - $row['pos_x']),2) + pow(($ships['pos_y'] - $row['pos_y']),2)),2);
         
               $currentships = array ( $i => array("Ship Name" => $ships['ship_name'], "Distance" => $currentdistance),
         
               );
         
             }
         
             $i++;
            print_r($currentships);          
      
      }
   
      echo '</div>';






The print_r output to check the array data (prettied up for ease of reading):

Quote

Array (
  • => Array ( [Ship Name] => Corn Cob [Distance] => 5.1 ) )

Array ( [1] => Array ( [Ship Name] => Golden Kronar [Distance] => 4.47 ) )
Array ( [1] => Array ( [Ship Name] => Golden Kronar [Distance] => 4.47 ) )
Array ( [3] => Array ( [Ship Name] => Money [Distance] => 5.66 ) )



I have 4 records in the database.   It is pulling one of them twice for some reason and dumping it into an array.  I put in echos to separate out and the info for Golden Kronar dumps twice and the other two do not.  

I'm pretty sure I'm missing something obvious here but after two days of staring at it I'm getting cross eyed.  

Thanks for any help!


Rich

« Last Edit: December 19, 2010, 02:45:12 PM by emicarn »
Thanks!

~ Rich

http://www.sectorbattles.com [not ready for prime time]

Offline Chris

  • Game Owner
  • Level 35
  • *
  • Posts: 2,217
  • Reputation: +28/-1
    • View Profile
Re: Odd result from mysql query into an array
« Reply #1 on: December 19, 2010, 09:10:12 AM »
If you have problems, simplify.
Code: [Select]
     // where is the query to get $row?
     $row['pos_x']=5;
     $row['pos_y']=5;

      echo '<div id="status-screen-sensors">';
      $warid = (int)$_COOKIE['spacewarid']; // (int) for sql injection protection
      $resultdofleet = mysql_query("SELECT * FROM players WHERE id<>$warid");
      while ($ships = mysql_fetch_array($resultdofleet))
      {
               $currentdistance = round(sqrt( pow(($ships['pos_x'] - $row['pos_x']),2) + pow(($ships['pos_y'] - $row['pos_y']),2)),2);
               echo("Ship Name".$ships['ship_name']. "Distance".$currentdistance.'<br>');
      }
      echo '</div>';

Offline JGadrow

  • Level 35
  • **
  • Posts: 1,133
  • Reputation: +23/-2
    • View Profile
Re: Odd result from mysql query into an array
« Reply #2 on: December 19, 2010, 09:37:28 AM »
In your loop, your print_r() statement is outside of the conditional. So, when $i = 2 it's failing the conditional (because the current player is the owner of that row's ship). But the previous value of the $currentships variable is set from when the conditional passed on $i = 1. Thus, you're printing that row's data twice.

*Edit - Changed "you're" to "your."
Idiocy - Never underestimate the power of stupid people in large groups.


Offline Emicarn

  • Level 5
  • *
  • Posts: 19
  • Reputation: +0/-0
    • View Profile
Re: Odd result from mysql query into an array
« Reply #3 on: December 19, 2010, 02:36:18 PM »
If you have problems, simplify.


Good suggestion! :)


Code: [Select]
     // where is the query to get $row?
     $row['pos_x']=5;
     $row['pos_y']=5;


It was here:

Code: [Select]
while ($row = mysql_fetch_array($dovessel))

It was a piece further up the line.  Sorry should have included that.


 I'll give this a try.

Rich

Thanks!

~ Rich

http://www.sectorbattles.com [not ready for prime time]

Offline Emicarn

  • Level 5
  • *
  • Posts: 19
  • Reputation: +0/-0
    • View Profile
Re: Odd result from mysql query into an array
« Reply #4 on: December 19, 2010, 02:44:53 PM »
In your loop, your print_r() statement is outside of the conditional. So, when $i = 2 it's failing the conditional (because the current player is the owner of that row's ship). But the previous value of the $currentships variable is set from when the conditional passed on $i = 1. Thus, you're printing that row's data twice.

*Edit - Changed "you're" to "your."


That fixed it.

Now I can start figuring out how to sort this array down to output nearest to farthest.

Thanks guys!
Thanks!

~ Rich

http://www.sectorbattles.com [not ready for prime time]

Offline 133794m3r

  • Level 22
  • *
  • Posts: 265
  • Reputation: +2/-0
    • View Profile
Re: Odd result from mysql query into an array
« Reply #5 on: December 19, 2010, 11:28:25 PM »
/* snipped */

That fixed it.

Now I can start figuring out how to sort this array down to output nearest to farthest.

Thanks guys!
http://php.net/manual/en/function.sort.php

even though i'm late to this party, i suggest that you look at that page since i believe it should do exactly what you're wanting to do and thus will save you a great deal of time.

 


SimplePortal 2.3.3 © 2008-2010, SimplePortal