The forums have permanently moved to This forum will be kept around in read-only mode for archival purposes. To learn how to continue using your existing account on the new forums, check out this thread.

Results 1 to 4 of 4

Thread: PHP Arrays... How do they work?

  1. #1

    PHP Arrays... How do they work?

    Alright... in my pursuit to convert my current site from Coldfusion to PHP, I ran into a problem with making CFC functions into PHP functions. Pretty much what I'm doing is recreating a proximity search. I think I've almost got it but I don't know how PHP handles arrays and how to call to them. Here is the code I have...

    PHP Code:
    //constant variables
    //pi divided by 180
    $piDivRad 0.0174;
    //number of miles per degree of latitude
    $latitudeMiles 69.1;
    //radius of the earth in miles
    $earthRadius 3956;

    //makes an array with coordinates reset to zero
    function newCoordinates() {
    $newCoor = array();
    $newCoor['latitude'] = 0;
    $newCoor['longitude'] = 0;
    $newCoor['rlatitude'] = 0;
    $newCoor['rlongitude'] = 0;

    //finds the coordinates from the list in the database by using the zipcode given
    function zipCoordinates($zip) {
    $zipCoor newCorordinates();
    $query "SELECT latitude, longitude, rlatitude, rlongitude FROM zipcodes WHERE zip = '$zip'";
    $result mysql_query($query);
        if (
    $zipcode mysql_fetch_array($result)) {
    $zipCoor['latitude'] = $zipcode['latitude'];
    $zipCoor['longitude'] = $zipcode['longitude'];
    $zipCoor['rlatitude'] = $zipcode['rlatitude'];
    $zipCoor['rlongitude'] = $zipcode['rlongitude'];
    //return the coordinates for the lat and long
    return $zipCoor;

    squareSearch($radius$zip) {
    $radius $radius//maybe not needed?
    $zip $zip//maybe not needed?
    $zipCoor zipCoordinates($zip);
    $formula $earthRadius * (ACOS((SIN($zipCoor['latitude']/57.2958) * SIN(latitude/57.2958)) + (COS($zipCoor['latitude']/57.2958) * COS(latitude/57.2958) * COS(longitude/57.2958 $zipCoor['longitude']/57.2958))))
    $query "SELECT zip, latitude, longitude, '$formula' FROM zipcodes WHERE '$formula' <= '$radius'";
    $result mysql_query($query);
    //return the results
    return $result;
    Alright... for the function zipCoordinates I have a variable ($zipCoor) calling the function newCorordinates().
    PHP Code:
    $zipCoor newCorordinates(); 
    Because the function creates an array, will the variable $zipCoor become an array?
    $zipCoor = [0, 0, 0, 0]?

    Also is my if statement correct in zipCoordinates()?

    Last, since squareSearch() will return multiple results will I need to make a while statement? or is the return $result at the end of the function sufficient?

  2. #2
    You forgot to return the array in newCoordinates();. After you fix this, $zipCoor will hold the array with all cells initialized to 0.

    The if statement is correct, but not error proof. If the query does not return any results, mysql_fetch_array() will give an error. Add 'mysql_num_rows($result) > 0 &&' to the beginning of the if statement.

    Yes, you can return a mysql resource which contains multiple rows. It is no different than returning a mysql resource with only 1 row.
    Last edited by NeoDreamer; September 6th, 2009 at 12:53 AM.

  3. #3
    so i got the function to work now i have to query the results of the function. i also changed squareSearch() to return $searchResult.

    In ColdFusion it was very simply to search the database with the results.

    SELECT * FROM users WHERE zipcode IN (#ListQualify(ValueList(,"'")#)
    What this did was list all of the rows in the column "zip". So the following query would be:

    SELECT * FROM users WHERE zipcode IN (11703, 11706, 11717)
    Is there such a thing in PHP as ValueList? Could I use a while loop or/with a for loop possibly?

  4. #4
    Quote Originally Posted by darkstarclone View Post
    SELECT * FROM users WHERE zipcode IN (11703, 11706, 11717)
    I'm not sure if this is helpful, but if you want a list of zips seperated by comma, that should be relatively easy in code.

    PHP Code:
    $zips "";
    $amount sizeof($searchResult)
    for (
    $i=0$i $amount$i++)
    $row mysql_fetch_array($searchResult);
    $zips $zips $row['zip'];
    $i != $amount 1)
    $zips $zips ", ";

    Hope it's helpful to you.
    Xbox 360 Gamertag: Maqrkk
    Current gamerscore: 20515

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Home About Meet the Moderators Advertise

 Link to Us


Copyright 1999 - 2012