Jump to content

Archived

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

onlyican

Better way to do this

Recommended Posts

Hey

Let me try and explain

I have a map, and I am using the GD LIB functions to complete the map
On the Map it self, there is 100 boxes.

I have the Top Left corner location of each box in an array
This location will be the Grid location
(longitude / Latitude)

Now I have a MySQL table, which will contain what is in each box. (There status)
Then with this status, I will add an image depending on the status that is in the table
(Then that Location in the box will have another image in it, using GD LIB)

How I am working this out is like the following

[code]
<?php
//This is the possible locations, as it is a square the X and Y will have the same locations
$locations_array = array("10","70","130","190","250","310","370","430","490","500");

//This is so If I add another location, i dont have to change all of the code
$num_locations = count($locations_array);

//setting a var to check everything is working
$num_results = 0;

//Run a for loop to get each location from the array for the X axis
for($a = 0; $a < $num_locations; $a ++){

//Now to get the Y axis, for each X axis
for($b = 0; $b <$num_locations; $b ++){

//Create the X and Y location into one
$location = $locations_array[$a].".".$locations_array[$b]

//Now just a Visual Test for me
echo $location."<br />\n";

//And for the testing to make sure they are all coming out
$num_results = $num_results + 1;
}

}

?>
[/code]

This SHOULD show 100 results in a list
and $num_results will be 100

That may help explain how I am getting all of the locations (saves having an array of 100)

Now, I want to use $location to check what the status is for its location

If I run a query in the middle of the for loops, then that means 100 queries in that page alone, which will kill my server

Can anyone think of a better way to retrieve the status of all locations, for me to compare them to $location

Share this post


Link to post
Share on other sites
[code]$query = "SELECT status FROM locations WHERE location IN (" . implode(",", $locations_array) . ")";[/code]

Then you would loop through to put them into an array using the location as the key (assuming all locations occur only once).  Then to get the status, just use [code]$location_status_array[$location][/code] to get the status

Share this post


Link to post
Share on other sites
1 query.
[code]

table
----------
long
lat
status

<?php
$sql = "SELECT long, lat, status FROM mytable";
$res = mysql_query ($sql) or die (mysql_error());
while (list($long, $lat, $status) = mysql_fetch_row($res)) {
    $status_array[$long][$lat] = $status;
}

//Run a for loop to get each location from the array for the X axis
for($a = 0; $a < $num_locations; $a ++){
    //Now to get the Y axis, for each X axis
    for($b = 0; $b <$num_locations; $b ++){
       
        $status = $status_array[$a][$b];
        // draw map square using status value
    }
   
}
?>[/code]

Share this post


Link to post
Share on other sites
PHP is weakly typed, so it will convert the keys to whatever format you request them in.  So long as the two are equivalent, either string, int, or float, then they will equate to eachother.

Share this post


Link to post
Share on other sites
its ok, I found an error,

I done if(is_string($var){
and it was coming up as string, so I knew it wasn't that
when I was testing I run a query
Then I ran the query again cos I forgot I run the first query
so they were overwritting each other, causing the mess up.
Thanks anyway

Its looking nice
a 610px by 610px image, with 100 boxes, being filled with color from the table.

Share this post


Link to post
Share on other sites

×

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.