Re: creating an array from a mySQL query
- From: "huckphin" <chad.rhyner@xxxxxxxxx>
- Date: 27 Aug 2006 22:17:33 -0700
To take your $result, and put it into an array, you need to feed each
row of
an array through a loop. The best way that I like is to use a while
loop, and put
it into an array. Although it does get complicated attempting to do
multi-dimensional
associative arrays, I would suggest using the constant MYSQL_BOTH as
the type of
array to put it into. In your case, to fetch the array and put it into
an array, it would
look like this.
// Grab all coordinates and put them in an array
while ( $row = mysql_fetch_array( $result, MYSQL_BOTH ) {
$coordinates[] = $row;
}
Using both associative and a numerical array, you can then use a for
loop to cycle through
all coordinates like this. Using the associtive array, you can use
this to define the fields inside
the second dimension of the array.
My apologies, for I forgot to add something. You need to specify
that you are in the $i array when you are doing this. So, the correct
way to cycle through it would be this.
for ($i = 0; $i < count($coordinates); $i++) {
print "var longitude = $coordinates[$i]['long']";
print "var namelong = $coordinates[$i]['name_long']";
print "var address = $coordinates[$i]['addr_num']";
print "var stname = $coordinates[$i]['st_namefull']";
// Do whatever processing is required to map the coordinate and
then continue to the next.
}
Hope this helps.
Respectfully Submitted,
Huck Finn
.
- References:
- creating an array from a mySQL query
- From: ericv
- Re: creating an array from a mySQL query
- From: huckphin
- creating an array from a mySQL query
- Prev by Date: Re: creating an array from a mySQL query
- Next by Date: Re: Wiki software for PHP4
- Previous by thread: Re: creating an array from a mySQL query
- Next by thread: Problem with $_GET
- Index(es):
Relevant Pages
|