Jump to content

Better way to do this


onlyican

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
Link to comment
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
Link to comment
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]
Link to comment
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.
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.