Drew
Drew

Reputation: 836

PHP Array from MYSQL to Javascript Object using JSON

I am starting to wonder where my head is at because I should have this on smash...

Scenario:

MySQL DB Table  > 'Sites'

Columns > 'Siteid(INT)','Location(Varchar)','Address(varchar)','PostalCode(varchar)'...etc

Trying to get all rows from table into PHP array, encode to json.

I would like to access this data in javascript via sitesArray[Location].Address would give me 123 Street

sitesArray[Toronto] > 'Address' : '123 Street', 'PostalCode' : '12345'

sitesArray[Montreal] > 'Address' : '0987 Street', 'PostalCode' : '09876'

I can get all rows from db no problem...however I am stuck on this blasted array thing...I cant get it into an associative array...whenever I encode the straight array from mysql it is invalid json, further more there is no index either integer or literal...

PHP DB TO ARRAY TO JSON

$result = mysql_query("SELECT * from Recommendations.Satellite_Sites;");
while($row= mysql_fetch_array($result)){
        $fullSiteDetails[$row['Site']] = array("site" => $row["Site"],"city" => $row["Site_City"], "address" => $row["Site_Address"], "postalcode" => $row["Site_PostalCode"], "phone"=>$row["Site_Phone"],"fax"=>$row["Site_Fax"]);
    }

    die(json_encode($fullSiteDetails));

this is my array building in the while loop during db look up

$fullSiteDetails[] = array("site" => $row["Site"],"city" => $row["Site_City"], "address" => $row["Site_Address"], "postalcode" => $row["Site_PostalCode"], "phone"=>$row["Site_Phone"],"fax"=>$row["Site_Fax"]);

JSON json_encode($fullSiteDetails)....>

[
{
site: "Ajax",
city: "Ajax",
address: "xxx Salem Road, Unit xxx",
postalcode: "L1Z xxx",
phone: "(905)-xxx-xxxx",
fax: "(905)-xxx-xxxx"
},
{
site: "Cambridge",
city: "Cambridge",
address: "123 Street",
postalcode: "A4E xxx",
phone: "(519)-xxx-xxxx",
fax: "(519)-xxxx-xxxx"
},
{
site: "Mississauga",
city: "Mississauga",
address: "xxx Derry Road East, Suite xxx",
postalcode: "L5T xxx",
phone: "(905)-xxxx-xxxx",
fax: "(905)-xxx-xxxx"
},
{
site: "Ottawa",
city: "Ottawa",
address: "xxxxx Hunt Club Road, Unit xx",
postalcode: "xxx 0Y3",
phone: "(613)-xxx-xxxx",
fax: "(613)-xxx-xxxx"
},
{
site: "Sudbury",
city: "Sudbury",
address: "xxxx Elm Street, Suite xx",
postalcode: "xxx xxx",
phone: "(705)-xxx-xxxx",
fax: "(705)-xxx-xxxx"
},
{
site: "TWH",
city: "Toronto",
address: "xxxxxxxxxxxxxxxxxxxxxxxxx",
postalcode: "xxx xxx",
phone: "(416)-xxx-xxxx",
fax: "(416)-xxx-xxxx"
}
]

Not only are the keys unqouted but I also would like to get this into an associative array...

in javascript I am doing this:

var fullSiteDetails = {};
fullSiteDetails = '<?php echo json_encode($fullSiteDetails) ?>';

in the console:

fullSiteDetails[0]
"{"
fullSiteDetails[Ajax]
VM8497:2 Uncaught ReferenceError: Ajax is not defined
fullSiteDetails['Ajax']
undefined

What am I missing or not cluing into?

Thanks for your time.

Upvotes: 0

Views: 679

Answers (2)

SeriousDron
SeriousDron

Reputation: 1346

Can't see column location in your PHP code. But to make it associative array just user something like:

$fullSiteDetails[$row["Site_City"]] = array(...

And keys don't actually need to be quoted if there no reserved js words among it.

Use following string to get your php array to js object. Don't add ' to it as It's alredy js object and don't need any quoting

var fullSiteDetails = <?php echo json_encode($fullSiteDetails) ?>;

Upvotes: 2

Alexis Peters
Alexis Peters

Reputation: 1621

maybe there are ' or " in your data. If you're using json_encode try using JSON_HEX_APOS and/or JSON_HEX_QUOT as 2. parameter. If this doesn't work try posting your malformed json

Upvotes: 0

Related Questions