Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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.

share|improve this question
    
what do you use in php to encode? –  Danyal Sandeelo Mar 10 at 6:16
    
json_encode($fullSiteDetalis); –  Drew Mar 10 at 7:24

2 Answers 2

up vote 1 down vote accepted

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) ?>;
share|improve this answer
    
tried that..no luck...added the array output encoded as json to my edits so you can see...I used $row['Site'] as the associative index. –  Drew Mar 10 at 7:40
    
Can't see anything with associative keys. Can you put all php code with cycle and json_encode. It's should be very simple code and mistake must be very obvious. –  SeriousDron Mar 10 at 7:42
    
as for the keys not being quoted...the json validation keeps failing...also when I get this string into javascript..I cant reference it...jsonObj[0] returns { and jsonObj[Ajax] returns undefined... –  Drew Mar 10 at 7:43
    
added php db lookup to array –  Drew Mar 10 at 7:46
    
Ok. I think I know problem. If jsonObj[0] returns '{' for you then I think you get your result as a string, not object. If you using jquery to make ajax call set dataType parameter to 'json' to get result as object. If it's not your case please show us how do you use your php output and our js code –  SeriousDron Mar 10 at 7:46

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

share|improve this answer
    
json has been added in edit... –  Drew Mar 10 at 7:24

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.