Using PHP MySQL query to set locations within Google maps - php

I am trying to set location markers. The code below is not looping through the results and displays only the first row. I tested the query and result outside of Google Maps and it work.
var addresses = [
<?php
while($row = mysql_fetch_array($results)) {
echo '"'.$row['company'].', '. $row['address'].', ' . $row['city'] . ', ' . $row['state'] . ', ' . $row['zip'] .'",';
}
?>
];

This is what you should do:
<?php
...
$array = array();
while ($row = mysql_fetch_assoc($results)) {
$array[] = array(
'company'=>$row['company'],
'address'=>$row['address'],
'lat'=>$row['latitude'],
'lng'=>$row['longitude'],
); // add what ever cels you need
}
$json_string = json_encode($array);
echo 'var addresses=' . $json_string . ';';
...
?>
If you keep having problems, please post the javascript part of the code; this is just the part where you print an object (or array) in a format that javascript can read.

Related

double quotes inside single quotes in MYSQL result?

I'm trying to do something very basic but I can't figure out how.
basically i'm trying to convert the mysql result ($row) into the following format (literal strings):
"0784562627828" => "James",
"0786636363663" => "David",
I have all the data stored in the database and I can get them echoed on my page like so:
$phone = $row['phone'];
$name = $row['name'];
$list .=''.$phone.'';
echo $list;
could someone please advise on this?
Thanks
Just assign them inside an array like you normally would:
$array = array();
while(your fetch here) {
$array[$row['phone']] = $row['name'];
}
To check its contents, you can use var_dump($array) or print_r($array)
Or just straight up show them, like the one you formatted:
while(your fetch here) {
echo '"' . $row['phone'] . '"' . ' => ' . '"' . $row['name'] . '"' . '<br/>';
}
you mean something like this?
$list = array();
$list[$phone] = $name;
Can you do something like
$list = [];
foreach($rows as $row) {
$list[$row['phone']] = $row['name'];
}

PHP foreach - parsed json from URL

I have a JSON file called from an URL. I've checked and I'm getting the the data from the URL.
I've tried a lot, but I can't get the loop foreach to work - what is wrong?
<?php
$url = 'http://banen.klintmx.dk/json/ba-simple-proxy.php?url=api.autoit.dk/car/GetCarsExtended/59efc61e-ceb2-463b-af39-80348d771999';
$json= file_get_contents($url);
$data = json_decode($json);
$rows = $data->{'contents'};
foreach($rows as $row) {
echo '<p>';
$FabrikatNavn = $row->{'contents'}->{'FabrikatNavn'};
$ModelNavn = $row->{'contents'}->{'ModelNavn'};
$PrisDetailDkk = $row->{'contents'}->{'PrisDetailDkk'};
echo $FabrikatNavn . $ModelNavn . ' Pris ' . $PrisDetailDkk;
echo '</p>';
}
?>
The actual problem is you trying to access content object again. Just change your foreach snippet with,
foreach ($rows as $row) {
echo '<p>';
$FabrikatNavn = $row->FabrikatNavn;
$ModelNavn = $row->ModelNavn;
$PrisDetailDkk = $row->PrisDetailDkk;
echo $FabrikatNavn . $ModelNavn . ' Pris ' . $PrisDetailDkk;
echo '</p>';
}
DEMO.
Use json_decode($data, true) so that it parses the JSON content into a PHP array. So it will be something like
$rows = $data['contents'];
foreach($rows as $row) {
echo '<p>';
$FabrikatNavn = $row['contents']['FabrikatNavn'];
$ModelNavn = $row['contents']['ModelNavn'];
$PrisDetailDkk = $row['contents']['PrisDetailDkk'];
echo $FabrikatNavn . $ModelNavn . ' Pris ' . $PrisDetailDkk;
echo '</p>';
}
Take a look at using json_decode($json, true) as this will convert the data to an associative array which seems to be the way you are approaching the solution.
Check the output by printing with var_dump() or print_r()
Try like this
$data = json_decode($json,true); //decode json result as array and thenloop it
print '<pre>';
print_r($data);
foreach($data as $row){
//do something here
}

Get element by index, [n]

I'm using PHPQuery to read some content from HTML, I'm unable to get the element by it's index using the square bracket notation.
See this simple example:
$html = '<div><table id="theTable"><tr><td>FIRST TD</td><td>SECOND TD</td><td>THIRD TD</td></tr></table></div>';
$pq = phpQuery::newDocumentHTML($html);
$table = $pq->find('#theTable');
$tds = $table->find('td');
echo "GETTING BY INDEX:\n\n";
echo '$tds[1] = ' . $tds[1];
echo "\n\n\n";
echo "GETTING IN FOREACH:\n\n";
foreach($tds as $key => $td){
echo '$tds[' . $key . '] = ' . pq($td) . "\n";
}
The output of this is:
GETTING BY INDEX:
$tds[1] =
GETTING IN FOREACH:
$tds[0] = FIRST TD
$tds[1] = SECOND TD
$tds[2] = THIRD TD
I would have expected that I can get the contents of $tds[1] using square brackets, but seems not. How can I get it by index?
Try a var_dump($tds), it'll tell you whats exactly inside the tds. Maybe those keys are actually strings and you should use:
echo "GETTING BY INDEX:\n\n";
echo '$tds['1'] = ' . $tds['1'];
Edit: Also, on your foreach you're using pq(), maybe you should use this
echo "GETTING BY INDEX:\n\n";
echo '$tds[1] = ' . pq($tds[1]);
Found the answer just after posting the question. Instead of square brackets you need to use eq(n):
echo '$tds[1] = ' . $tds->eq(1);
Try the following:
echo '$tds[1] = ' . $tds['1'];

Using PHP to get Facebook friends statuses?

That's my first post here on SO.
I'm using PHP to get Facebook friends statuses. In particular, I've tried to retrieve all public statuses from one of my facebook friends, but it happens only the first 100 statuses. I need to get all the statuses and to write them in a text file. this is the code I'm using, patched up from many answers I read here on SO.
$i=0;
$result = $facebook->api('/my_friend_ID/statuses/',
array('access_token' => $facebook->access_token,'limit'=>100,));
//'offset'=>50,used before limit, it push the limits forward by 50, it doesn't go beyond it
//'since'=>2010, I read on SO there was even this field, but I can't make it work.
foreach($result['data'] as $post)
{
echo $i . '<br>';
echo $post['id'] . '<br>';
echo $post['from']['name'] . '<br>';
echo $post['from']['id'] . '<br>';
echo $post['name'] . '<br>';
echo $post['message'] . '<br>';
echo '*---------------------------------------------------*' . '<br>';
$i++;
$write_file = fopen("esempio.txt","a");
$message = $post['message'] . '<br>';
fwrite($write_file,$message);
fclose($write_file);
}
so, to be clearer, how to get all friends statuses (old and new) in a text file?
You need to use paging https://developers.facebook.com/docs/reference/api/pagination/
$the_statuses = array();
$your_statuses = $facebook->api("/my_friend_ID/statuses/");
while ($your_statuses['data'])
{
$the_statuses = array_merge( $the_statuses, $your_statuses['data'] );
$paging = $your_statuses['paging'];
$next = $paging['next'];
$query = parse_url($next, PHP_URL_QUERY);
parse_str($query, $par);
$your_statuses = $facebook->api(
"/my_friend_ID/statuses/", 'GET', array(
'limit' => $par['limit'],
'until' => $par['until'] ));
}
Then you can do the loop through all statuses
foreach($the_statuses['data'] as $post)
{
echo $i . '<br>';
echo $post['id'] . '<br>';
echo $post['from']['name'] . '<br>';
echo $post['from']['id'] . '<br>';
echo $post['name'] . '<br>';
echo $post['message'] . '<br>';
echo '*---------------------------------------------------*' . '<br>';
$i++;
$write_file = fopen("esempio.txt","a");
$message = $post['message'] . '<br>';
fwrite($write_file,$message);
fclose($write_file);
}

mssql_fetch_array only displays one row if columns are put into variables

I'm still a PHP noob, so I apologize if this is something simple.
I am creating a fairly basic search facility for a website using PHP and mySQL. I have connected to the database, selected the database, queried the table and have fetched the table columns;
$k = htmlspecialchars($_GET['k']); // Get search query
$select = mssql_query("SELECT * FROM search WHERE Title Like '%" . $k . "%'");
if( mssql_num_rows($select) < 1) {
$noResults = 'No results found for <b>' . $k . '</b>, <label for="k">Please try again.</label>';
} else {
while ($results = mssql_fetch_array($select)) {
$title = $results['Title'];
$link = $results['Link'];
$description = $results['Description'];
}
}
When I put the $results[''] columns into variables and then try to echo out each variable like so;
if( isset($noResults)) {
echo $noResults;
} else {
echo '<li>' . '<h2>' . '' . $title . '' . '</h2>' . '<p>' . $link . '</p>' . '<p>' . $description . '</p>' . '</li>';
}
it only echo's out one row matching that query however, If I was to just simple echo out the columns like so;
echo $results['Title'];
echo $results['Link'];
echo $results['Description'];
all rows matching the query will be displayed..
I'm not sure why this is happening. If someone could help me out that would be great!
You need to use a loop:
$k = mysql_real_escape_string($_GET['k']); // Get search query
$select = mssql_query("SELECT * FROM search WHERE Title Like '%" . $k . "%'");
if( mssql_num_rows($select) < 1) {
$noResults = 'No results found for <b>' . $k . '</b>, <label for="k">Please try again.</label>';
} else {
$results= array();
while ($result = mssql_fetch_array($select)) {
$results[]= $result;
}
}
if( isset($noResults)) {
echo $noResults;
} else {
echo "<ul>";
foreach($results as $result){
echo '<li>' . '<h2>' . '' . $result['title'] . '' . '</h2>' . '<p>' . $result['link'] . '</p>' . '<p>' . $result['description'] . '</p>' . '</li>';
}
echo "</ul>";
}
Do you execute the output in the while-loop?
If you execute the while-loop and call the echo after that, each resultset will overwrite the previous, and the echo will output the last resultset which was fetched.
If you call the echo in the Loop, every result set will generate "his own" output line.
If you want to hold every resultset in a variable you can use an array, which is declared in front of the loop and gets filled in the loop.
a few things are not clear from your question, but i am assuming that you are echo'ing the variables outside the loop since you are checking isset($noResults). that means you are reassigning the variables with new values in each loop of while. so ultimately you get the last one assigned to the variables. you have to either use an array to hold the values or echo it with in the loop.

Categories