Jump to content

PHP + MYSQL PROB, SELECTING EVERY OTHER ROW


shasto100

Recommended Posts

Hi guys, I'm try to work this code out & I'm getting nowhere.

 

I have a MYSQL Db with 1 table called 'Coordinates', it contains 3 columns, 'id', 'lat', 'long'.  I'm try to create SQL insert statements using the power of PHP, as my table has 92,000 rows in so far.

 

I have written some functions, which will work out the four points around these 2 coordinates, but I need to add/call the values from 1 row with the following row.  For example:

 


Coordinates table
-----------------
id | lat | long
-----------------

Sample Data
-----------------
1, 180.12345678, 33.12345678
2, 180.12345680, 33.12345680
3, 180.12345682, 33.12345682
4, 180.12345684, 33.12345684 
-----------------

 

PHP PAGE

<?
// sql 1 start
$k="INSERT INTO newTable (id, lat, long, point1, point2, point3, point4) values (";

// Select database connections
require_once('connections/DB_Connection.php');

// Make the query, test with just 50 rows
$query = "SELECT * FROM coordinates where row_ID <= 50";

// Run the query
$result = @mysql_query ($query);

if ($result) { // if it ran ok, then display the results

while ($my_row = mysql_fetch_array ($result, MYSQL_ASSOC)){

	// from the 1st row returned
                $lat1 = $my_row['lat'];
                $long1 = $my_row['lat'];
                
                // from the 2nd row returned
                $lat2 = $my_row['long'];
	$long2 = $my_row['long'];


             
	//echo "Lat 1 = ".$lat1."<br/>";
                //echo "Long 1 = ".$long1."<br/>";
	//echo "Lat 2 = ".$lat2."<br/>";
	//echo "Long 2 = ".$long2."<br/>";


               // use the variable called $k to output the beginning of the SQL statement
               // call the functions already created to calculate the points
               // the problem is I need to use 4 variables (lat1, long1, lat2, long2)
               // 2 from the 1st row, 2 from the 2nd row
               // then, start on row 2 with row 3

	}

	// Free up resources
mysql_free_result ($result); 

}
// if did not run properly
else 
{
echo "<p>". mysql_error(). "</p>";
}
?>

 

Hope you understand, its rather confusing!

 

Thanks for any help given, please ask any questions.

Link to comment
https://forums.phpfreaks.com/topic/68383-php-mysql-prob-selecting-every-other-row/
Share on other sites

For arguments sake lets say that:

point1 = $lat1+$lat2;

point2 = $lat2-$lat1;

point3 = $long1+$long2;

point4 = $long2-$long1;

 

The desired output is:

 

INSERT INTO newTable (id, lat, long, point1, point2, point3, point4) values (1, 81.497414, 120.697441, 162.994805, 0.000023, 241.372269, -0.022613};

INSERT INTO newTable (id, lat, long, point1, point2, point3, point4) values (1, 81.497391, 120.674828, $point1_value, $point2_value, $point3_value, $point4_value};

INSERT INTO newTable (id, lat, long, point1, point2, point3, point4) values (1, 81.497368, 120.652206, $point1_value, $point2_value, $point3_value, $point4_value};

 

 

Output now is:

 

INSERT INTO newTable (id, lat, long, point1, point2, point3, point4) values (81.497414, , 120.697441, }'.;

INSERT INTO newTable (id, lat, long, point1, point2, point3, point4) values (81.497391, , 120.674828, }'.;

INSERT INTO newTable (id, lat, long, point1, point2, point3, point4) values (81.497368, , 120.652206, }'.;

INSERT INTO newTable (id, lat, long, point1, point2, point3, point4) values (81.497345, , 120.629593, }'.;

 

 

-------------------------------

PLEASE CHANGE ORIGINAL CODE:

 

Sample data:

81.497414, 120.697441

81.497391, 120.674828

81.497368, 120.652206

81.497345, 120.629593

 

PHP page:

 

Around line 28:

echo $k.$lat1.", ".$lat2.", ".$long1.", ".$long2."}'.;<br/>";

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.