Jump to content

Fill multiple arrays from mysql and then loop together.


beckjoh

Recommended Posts

I need to select multiple rows (x,y data0 from two seperate tables and then store the values in two seperate array's.  One will be pts the other polygon, I think I have done that. However, I then need to use all values in each array in a seperate function that is outside of the while loop for each query. The function only returns one value, instead of all of the values that should be stored in the array. Here is some of the code:

 

<?php
$link = mysql_connect("localhost", "root", ""); 
mysql_select_db("database", $link); 

$bndresult = mysql_query("SELECT * FROM polygon 
", $link); 
$num_rows = mysql_num_rows($bndresult); 
if($num_rows == 0) { 
echo "No rows!"; 
} 
else { 
// get each row 
while($myrow = mysql_fetch_array($bndresult, MYSQL_BOTH)) 
{ 
$x = $myrow[Lon]; 
$y = $myrow[Lat]; 
$myPolygon = array("x," => $x, "y" => $y); 

} 

$result = mysql_query("SELECT * FROM point", $link); 
$num_rows2 = mysql_num_rows($result); 
if($num_rows2 == 0) { 
echo "No rows!"; 
} 
else { 
// get each row 

while($myrow2 = mysql_fetch_assoc($result)) 
{ 
$xpoint = $myrow2[Lon]; 
$ypoint = $myrow2[Lat]; 
$point = array("x" => $xpoint, "y" => $ypoint); 
} 

foreach($point as $key => $point) { 
echo "$key ($point) is " . $pointLocation->pointInPolygon($point, $myPolygon) . "<br>"; 
} 
?>

 

This loop does not work correctly because it only loops for one of the arrays how do I loop for both?beckjoh

New php-forum User

 

 

Posts: 1

Joined: Sat Feb 23, 2008 9:17 am

Private message

Here is the Table structure for table `point`

--

 

CREATE TABLE IF NOT EXISTS `point` (

  `PointID` int(11) NOT NULL,

  `Lon` decimal(10,6) NOT NULL,

  `Lat` decimal(10,6) NOT NULL,

  `Elevation` decimal(5,1) NOT NULL,

  `FixType` decimal(4,0) NOT NULL,

  `UTCTime` decimal(7,1) NOT NULL,

  `Speed` decimal(3,2) NOT NULL,

  `Course` decimal(4,1) NOT NULL,

  `SF1` int(11) NOT NULL,

  `SF2` decimal(7,0) NOT NULL,

  `SF3` decimal(7,0) NOT NULL,

  `SF4` decimal(4,3) NOT NULL,

  `SF5` decimal(10,4) NOT NULL,

  `SF6` decimal(10,4) NOT NULL

) ENGINE=InnoDB DEFAULT CHARSET=latin1;

 

and

 

 

-- Table structure for table `polygon`

--

 

CREATE TABLE IF NOT EXISTS `polygon` (

  `PolygonID` int(11) NOT NULL,

  `PointNum` int(11) NOT NULL,

  `Lon` decimal(10,6) NOT NULL,

  `Lat` decimal(10,6) NOT NULL

) ENGINE=InnoDB DEFAULT CHARSET=latin1;

 

I thought that would help but I'm still confused.

 

No keys. How do the table relate, if at all?

Is pointNum the pointID in the points table or just a sequence number?

 

Why are lat, long held twice, or are they the keys to join the tables?

If so, aren't both your arrays identical?

That solution looks great, but I still have the same problem because the author of that script physically added the point and polygon values.  I need the script to pull all of the values from a mysql table.  The only way I know how to fill the array is through two seperate loops, but to run the function, I would need to be able for each array to loop through all the points.  Therefore my first question remains, can you first fill two arrays and then loop them together. Or do you use the explode function to fix the problem?

If you want to create an array to create a polygon then

 

while($myrow = mysql_fetch_array($bndresult, MYSQL_BOTH)) 
{ 
    $x = $myrow['Lon']; 
    $y = $myrow['Lat']; 
    $myPolygon[] = $x ;
    $myPolygon[] = $y;

} 

Thanks, I think I have it working.  I was going to try your function on http://www.phpfreaks.com/forums/index.php/topic,108445.msg436434.html#msg436434  to see if it works.  Do I need the imagecreate function or is it within php?  Thanks again.

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.