Old 25-02-2008, 17:49   #1 (permalink)
datahound
Spare Parts
 
datahound's Avatar
 
Join Date: Jan 2005
Location: Bracknell Forest
Posts: 4,532
help with multidimensional array

Can nayone help please?

Why does this not work?

// create UKPostCodes array

$result = mysql_query("SELECT * FROM UKPostCodes",$db);

if ($myrow = mysql_fetch_array($result)) {

do {

$UKPostCodes = array ('$myrow[PostCode]' => array('$myrow[Lat]','$myrow[Long]'));


} while ($myrow = mysql_fetch_array($result));

}


// get from array lat1 and long1

$lat1 = $UKPostCodes['$_POST[Fpostcode]'][1];
$long1 = $UKPostCodes['$Fpostcode'][2];


I am assuming that '$UKPostCodes = array' starts a new empty array everytime so I end up with just one row? Is that right? If so how do I get around it?

Thanks.
__________________
  Reply With Quote
Old 25-02-2008, 18:39   #2 (permalink)
datahound
Spare Parts
 
datahound's Avatar
 
Join Date: Jan 2005
Location: Bracknell Forest
Posts: 4,532
To clarify,

I want to get the entries from a db of postcodes with latitude and longtitude values into an array.

Then pull out a value $lat1 and $long1 based on the postcode posted by a form $Fpostcode.
__________________
  Reply With Quote
Old 25-02-2008, 18:47   #3 (permalink)
Hunch
Grumpy old man
 
Hunch's Avatar
 
Join Date: Oct 2007
Location: North Japan
Posts: 1,376
Use a while loop to read through the result set a row at a time.

while($row = mysql_fetch_array($result)) {

// Dump $row into array

}
  Reply With Quote
Old 25-02-2008, 18:51   #4 (permalink)
datahound
Spare Parts
 
datahound's Avatar
 
Join Date: Jan 2005
Location: Bracknell Forest
Posts: 4,532
Thanks. I have the loop. It is this bit that does not work?

// Dump $row into array

$UKPostCodes = array ('$myrow[PostCode]' => array('$myrow[Lat]','$myrow[Long]'));
__________________
  Reply With Quote
Old 25-02-2008, 19:59   #5 (permalink)
datahound
Spare Parts
 
datahound's Avatar
 
Join Date: Jan 2005
Location: Bracknell Forest
Posts: 4,532
All sorted, had to go round, but got there.
__________________
  Reply With Quote
Old 25-02-2008, 20:08   #6 (permalink)
HaXXor's
Web Schizophrenic
 
HaXXor's's Avatar
 
Join Date: Feb 2008
Location: In your head
Posts: 34
You should use OOP if you are using PHP.
  Reply With Quote
Old 25-02-2008, 21:04   #7 (permalink)
Hunch
Grumpy old man
 
Hunch's Avatar
 
Join Date: Oct 2007
Location: North Japan
Posts: 1,376
Quote:
Originally Posted by HaXXor's
You should use OOP if you are using PHP.

I fucking hate it when people who don't have a clue just spout random useless shit.

Quote:
Originally Posted by datahound
Thanks. I have the loop. It is this bit that does not work?

$UKPostCodes = array ('$myrow[PostCode]' => array('$myrow[Lat]','$myrow[Long]'));

Remove all the single quotes from the line and it'll work.

$UKPostCodes = array($myrow[PostCode]=>array($myrow[Lat],$myrow[Long]));
  Reply With Quote
Old 26-02-2008, 03:45   #8 (permalink)
rob*
you want it to do what!?
 
rob*'s Avatar
 
Join Date: Jan 2008
Posts: 21
Quote:
Originally Posted by Hunch
I fucking hate it when people who don't have a clue just spout random useless shit.

I second that.
  Reply With Quote
Old 26-02-2008, 05:13   #9 (permalink)
datahound
Spare Parts
 
datahound's Avatar
 
Join Date: Jan 2005
Location: Bracknell Forest
Posts: 4,532
Thanks Hunch, I am pretty sure I tried that every which way. I don't think is possible to append to a mdim array in that way. I will play with it again later when I have a bit of time.

In the end I used a simple assoc array where the second half holds two values split by a recognised string. I could only do this because I know the data format is fixed.

$UKPostCodes[$myrow[PostCode]] = $myrow[Lat]."xxx".$myrow[Long];

Then split them out where needed.

$latlong1 = $UKPostCodes[$Fpostcode];
$latlong1 = explode("xxx",$latlong1);
$lat1 = $latlong1[0];
$long1 = $latlong1[1];
__________________
  Reply With Quote
Old 26-02-2008, 05:42   #10 (permalink)
Hunch
Grumpy old man
 
Hunch's Avatar
 
Join Date: Oct 2007
Location: North Japan
Posts: 1,376
Quote:
Originally Posted by datahound
Thanks Hunch, I am pretty sure I tried that every which way. I don't think is possible to append to a mdim array in that way.

By removing the quotes? I tested it to be sure. It's fine.
  Reply With Quote
Old 26-02-2008, 05:49   #11 (permalink)
datahound
Spare Parts
 
datahound's Avatar
 
Join Date: Jan 2005
Location: Bracknell Forest
Posts: 4,532
Thanks.

As you can probably guess I am working on system to compare a postcode to the contents of a database and return the nearest results.

I think I have to revisit the mdim array to sort the results nearest first.
__________________
  Reply With Quote
Old 26-02-2008, 05:55   #12 (permalink)
Hunch
Grumpy old man
 
Hunch's Avatar
 
Join Date: Oct 2007
Location: North Japan
Posts: 1,376
I'm sure you could do that all of that (or at least most of it) in the SQL query. One of a database's strengths is the ability to sort data.
  Reply With Quote
Old 26-02-2008, 09:08   #13 (permalink)
pgo
i'm done, son
 
Join Date: Jan 2005
Posts: 12,262
Quote:
Originally Posted by HaXXor's
You should use OOP if you are using PHP.
Better get yourself over to Drupal headquarters and inform them that their CMS is poorly coded.
  Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search


Contact Us - Web Design Forums - Archive - Top
Search Engine Optimization by vBSEO 3.0.0 RC8