Jump to content


Photo

Better way to do this


  • Please log in to reply
5 replies to this topic

#1 onlyican

onlyican
  • Members
  • PipPipPip
  • Advanced Member
  • 921 posts
  • LocationHants - UK

Posted 28 August 2006 - 07:34 PM

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

<?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;
}

}

?>

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
Tell me the problem, I will try tell you the solution

#2 hitman6003

hitman6003
  • Members
  • PipPipPip
  • Advanced Member
  • 1,807 posts

Posted 28 August 2006 - 07:50 PM

$query = "SELECT status FROM locations WHERE location IN (" . implode(",", $locations_array) . ")";

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
$location_status_array[$location]
to get the status

#3 onlyican

onlyican
  • Members
  • PipPipPip
  • Advanced Member
  • 921 posts
  • LocationHants - UK

Posted 28 August 2006 - 08:00 PM

I was just thinking of doing that

Cheers hombre
Tell me the problem, I will try tell you the solution

#4 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,025 posts

Posted 28 August 2006 - 08:01 PM

1 query.

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
    }
    
}
?>

If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#5 hitman6003

hitman6003
  • Members
  • PipPipPip
  • Advanced Member
  • 1,807 posts

Posted 28 August 2006 - 08:41 PM

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.

#6 onlyican

onlyican
  • Members
  • PipPipPip
  • Advanced Member
  • 921 posts
  • LocationHants - UK

Posted 28 August 2006 - 10:06 PM

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.
Tell me the problem, I will try tell you the solution




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users