I have emails and street addresses stored in a MySQL database. I want to use PHP to run a proximity/geospatial search and send an email to all the results within a given radius.
I can get the Email/Addresses in a foreach loop, but now I don't know how to send the email to all the results within the query result.
My code is like this:
foreach ($Ergebnis as $Eintrag) {
echo '<li><strong>' . htmlentities($Eintrag->Name) . '</strong><br />' . "\n";
echo htmlentities($Eintrag->Email) . '<br />' . "\n";
echo htmlentities($Eintrag->Strasse) . '<br />' . "\n";
echo htmlentities($Eintrag->PLZ) . ' ' . htmlentities($Eintrag->Ort) . '<br />' . "\n";
if (!empty($Eintrag->Website)) {
echo 'Website: ' . htmlentities($Eintrag->Website) . '<br />' . "\n";
}
Thank you very much!
Related
I'm working on a script that will retrieve API information of the 'near earth objects' from NASA's website. User selects date, api grabs the information and displays it. How do I fix the foreach in this script? would appreciate some help with this.
$jsonAsteroids = file_get_contents("https://api.nasa.gov/neo/rest/v1/feed?start_date=2018-08-01&end_date=2018-08-04&api_key=NNKOjkoul8n1CH18TWA9gwngW1s1SmjESPjNoUFo");
$data = json_decode($jsonAsteroids, true);
echo "<h4>Retrieving the first element (i.e. \"links\") of the JSON structure</h4>";
var_dump( $data["links"]);
echo "<h4>Retrieving the first element (i.e. \"next\") inside the \"links\" element</h4>";
echo( $data["links"]["next"]);
You were very close. Your main issue was that you used json_decode(..., true); which gives you an array, but then used the object->property syntax instead of object['property']. My suggestion is to use json_decode without the 2nd argument in this case.
Finally, your 2nd foreach was malformed.
<?php
$result = file_get_contents("https://api.nasa.gov/neo/rest/v1/feed?start_date=2018-08-01&end_date=2018-08-04&api_key=NNKOjkoul8n1CH18TWA9gwngW1s1SmjESPjNoUFo");
$data = json_decode($result);
foreach ($data->near_earth_objects as $date => $objects) {
echo "<p>" . count($objects) . " objects detected on $date</p>";
echo "<ol>";
foreach ($objects as $object) {
echo "<li>" . $object->name . " <a href='" . $object->nasa_jpl_url . "'>" . $object->nasa_jpl_url . "</a><br>";
echo "Diameter of the object: " . $object->estimated_diameter->meters->estimated_diameter_min . "-" . $object->estimated_diameter->meters->estimated_diameter_max . " metres<br>";
echo "<ul>";
foreach ($object->close_approach_data as $close_approach) {
echo "<li>Close approach: " . $close_approach->close_approach_date . " traveling at a velocity of " . $close_approach->relative_velocity->kilometers_per_hour . " km/h " . "missing " . $close_approach->orbiting_body . " by " . $close_approach->miss_distance->kilometers . " km</li> ";
}
echo "</ul>";
}
echo "</ol>";
}
I need some help with echoing a <img> tag with attributes like title, rel, and class.
I have made it this far, when I'm echoing a filename from a db to search in a catalogue to find it. But I'm not sure how to write some attributes to it since I'm going to display it with Pirobox.
This is what i got working:
echo '<a href="uploads/'.$row['bildnamn'].'">';
echo '<img src="uploads/'.$row['thumb_bildnamn'].'">';
echo '</a>';
But I also need these attributes for the <A> tag which makes the image large.
rel="gallery" class="pirobox_gall" title="$row['uploaded']" . " " . "$row['user']";
What I don't get to work is how to get that line together with:
echo '<a href="uploads/'.$row['bildnamn'].'">';
You should be able to concatenate everything in the <a> tag like so:
echo '<a href="uploads/' . $row['bildnamn'] . '" rel="gallery" class="pirobox_gall" title="' . $row['uploaded'] . ' ' . $row['user'] . '">';
echo '<img src="uploads/' . $row['thumb_bildnamn'] . '">';
echo '</a>';
I inserted spaces to help emphasize where PHP does concatenation. In your case, a single quote starts/ends the string for PHP; a double quote is ignored and goes into the HTML. So this part:
title="' . $row['uploaded'] . ' ' . $row['user'] . '"
will make the title be the value of the uploaded column, then a space, then the value of the user column. Then just end the a tag with a >.
you could continue to concatenate the string
echo '<a href="uploads/'.$row['bildnamn'].'"'. 'rel="gallery" class="pirobox_gall" title="'.$row['uploaded'].' '.$row['user'].'">';
Try this:
echo '<a href="uploads/' . $row['bildnamn'] . '" rel="gallery" class="pirobox_gall" title="' . $row['uploaded'] . ' ' . $row['user'] . '">';
echo '<img src="uploads/' . $row['thumb_bildnamn'] . '">';
echo '</a>';
You can do this using string concatenation, like this:
$anchor = '<a href="uploads/'.$row['bildnamn'].'"';
$anchor .= 'rel="gallery" class="pirobox_gall" title="' . $row['uploaded'] . ' ' . $row['user'] . '">';
$anchor .= '<img src="uploads/'.$row['thumb_bildnamn'].'"></a>';
echo $anchor;
$refPointer = $site_to_be_extracted . $a->href;
echo $refPointer . '<br>';
generates in output (Chrome browser):
http://www.hackingwithphp.com/1/1/0/is-this-book-for-you
You would expect the following line to generate a correct <a> tag. That is a correct link
print '<a href=' . $refPointer . '>' . $a . '</a>';
Instead it generates in output:
localhost/1/1/0/is-this-book-for-you
So both the protocol (HTTP) and the $site_to_be_extracted (www.hackingwithphp.com) are lost. And replaced by "localhost"
Why? What could be the problem
This is the function where it's all happening. Now it works fine
include('simple_html_dom.php');
.
.
function createChapterContent ($site_to_be_extracted, $chapter_to_be_extracted) {
$html = file_get_html($chapter_to_be_extracted);
$refPointer;
foreach($html->find('ol') as $ol) {
foreach($ol->find('li') as $li) {
// echo '<br>' . $li->innertext ;
foreach($li->find('a') as $a) {
// Original. Causing the reported problem
// $refPointer = $site_to_be_extracted . $a->href;
// echo $refPointer . '<br>';
// changed to (see below). Now the output is correct
// Why?
$a->href = $site_to_be_extracted . $a->href;
$refPointer = $a->href;
print '<a href=' . $refPointer . '>' . $li->innertext . '</a>' . '<br>';
break;
};
}
}
I'm trying to understand how to fix this error.
Warning: prev() expects parameter 1 to be array, string given in
Its in the if statement below. Is this happening since the first value doesn't have a previous and I need to deal with that condition? Weirdly this worked in regular .php but not in the framework I have it in now.
I'm trying to generate an XML file based on a result set returned for a query. (I'm open to better ideas)
$export.= '<Campaigns>';
while ($line = mysql_fetch_assoc($result) ) {
//echo '<Email Timestamp="' . $line['EmailTimeStamp'] . '" ';
$export.= '<Campaign Info="' . $line['EmailTrackingNumber'] . '" EmailId="' .$line['EmailId'] . '">';
$export.= '<Emails>';
if (prev($line['EmailTrackingNumber']) == current($line['EmailTrackingNumber'])) {
$export.= '<Email Timestamp="' . $line['EmailTimeStamp'] . '" ';
$export.= 'City="' . $line['City'] . '" ';
$export.= 'Zip="' . $line['Zip'] . '"';
}
$export.= '</Emails></Campaign>';
}
$export.= '</Campaigns></EmailTrackingData>';
//echo $export;
file_put_contents('DateOfFile-export.xml', $export);
This
prev($line['EmailTrackingNumber'])
is not an array but a string. This
prev($line)
makes more sense. It returns the array entry which is before the current entry of $line.
But I think you would like to compare the last record with the current record. But that does not work like this. You can only access the columns of the current record. You have to temporarly save your last record.
$export.= '<Campaigns>';
$lastLine = null;
while ($line = mysql_fetch_assoc($result)) {
//echo '<Email Timestamp="' . $line['EmailTimeStamp'] . '" ';
$export.= '<Campaign Info="' . $line['EmailTrackingNumber'] . '" EmailId="' .$line['EmailId'] . '">';
$export.= '<Emails>';
if ($lastLine['EmailTrackingNumber'] == $line['EmailTrackingNumber']) {
$export.= '<Email Timestamp="' . $line['EmailTimeStamp'] . '" ';
$export.= 'City="' . $line['City'] . '" ';
$export.= 'Zip="' . $line['Zip'] . '"';
}
$export.= '</Emails></Campaign>';
$lastLine = $line;
}
$export.= '</Campaigns></EmailTrackingData>';
//echo $export;
file_put_contents('DateOfFile-export.xml', $export);
I am trying to use php to parse a JSON feed of posts using Facebook Graph API
I found the following solution for comments...
<?php
$request_url ="https://graph.facebook.com/comments/?
ids=http://www.youtube.com/watch?v=fyF-fj-1coY&feature=player_embedded";
$requests = file_get_contents($request_url);
$fb_response = json_decode($requests);
foreach ($fb_response as $key => $response) {
foreach ($fb_response->$key as $data) {
foreach ($data as $item) {
echo 'NAME: ' . $item->name . '<br />';
echo 'From ID: ' . $item->from->id . '<br />';
echo 'From Name: ' . $item->from->name . '<br />';
echo 'Message: ' . $item->message . '<br />';
echo 'Timestamp: ' . $item->created_time . '<br /><br />';
}
}
}
?>
This is the url id I'm working with: https://graph.facebook.com/210849652406/feed/?access_token={VALID_USER_TOKEN}
I just don't know how to call the items for this feed. I'm trying to make the comments parse with this post/feed but I get essentially nothing. I want the basic items like name of the post, caption, etc. I think if I just could get the name of the post I could figure it all out!
You are looping incorrectly
try this
foreach($fb_response->data as $item){
echo 'Message: ' . $item->message . '<br />';//there is no name returned on a comment
echo 'From ID: ' . $item->from->id . '<br />';
echo 'From Name: ' . $item->from->name . '<br />';
echo 'Message: ' . $item->message . '<br />';
echo 'Timestamp: ' . $item->created_time . '<br /><br />';
}
Do you have warnings/errors displayed? Ensure that you have extension=php_openssl.dll (or .so) enabled in your php.ini or you will get no results. This is because you are fetching from a secure site.
Also $item->name is an undefined property in the JSON. Perhaps you mean $item->id. Everything else looks ok.
Why aren't you using the PHP SDK?
https://developers.facebook.com/docs/reference/php/