I have a database with the following fields
ID
title
URL
summary
This data is stored in a multi dimensional associative array like this:
$array[0][title] = Title-1
$array[0][summary] = Summary-1
$array[1][title] = Title-2
$array[1][summary] = Summary-2
I also have a script that can loop through these and render as needed.
However I'm finding it tough getting this output into the body of a mail.
Looking around got me the following
Dynamic Content into mail() $message
Dynamic HTML Table in PHP Mail
HTML mail with dyanmic values
How to send HTML Dynamic Table as mail in php?
I found many more solutions via google, but they're more or less similar to the one's above.
These have not been helpful as the content is part static and part dynamic. In my case the whole body would be dynamic
Here's what I have as of now
function get_mailer_contents($email_category) {
global $mailer_contents;
$fetch_mailer_contents = mysql_query("SELECT * from `selected_posts` WHERE industry='$email_category'");
$id = "0";
while($temp_mailer_contents = mysql_fetch_array($fetch_mailer_contents))
{
$mailer_contents[$id]['title'] = $temp_mailer_contents['title'];
$mailer_contents[$id]['description'] = $temp_mailer_contents['description'];
$id ++;
}
}
get_mailer_contents($email_category);
foreach($mailer_contents as $title_cat)
{
echo "==================";
echo "<br>";
echo $title_cat['title'];
echo "<br>";
echo $title_cat['description'];
echo "<br>";
}
The output rendered here is not the final output. I'm using this just for testing purpose.
My problem is, a foreach function(or anything similar that would loop through the array) cannot be part of $message(body of the mail) mailer and I need to have a mechanism as the data is dynamic
Hope I have been clear enough. Do let me know if you need more specifics
You simply need to assign your output to variable and then use it in PhpMailer as in the following code:
$html = '<html><head><meta charset="utf-8" /></head><body>';
foreach ($array as $item) {
$html.= '<p>'.$item['title'].' '.$item['summary']."</p>";
}
$html.= '</body></html>';
// ... setting up phpMailer
$phpMailer->Body = $html;
$phpMailer->AltBody = nl2br(strip_tags($html));
$phpMailer->IsHTML(true); // you need to set HTML format
You also need to use IsHTML method to tell phpMailer to send the content as HTML, you should also set AltBody to display your mail for people who don't want/cannot display it in HTML format. Above I used a very simple way to transform html to text. However you can put here any other text as you want.
Related
im studying simple html dom.
as mentioned in their documentation, if we want to retrieve headers from website like , we would proceed as following:
<?php
include('simple_html_dom.php');
$html = file_get_html('https://www.w3schools.com/');
//to find h1 headers from a webpage
$headlines = array();
foreach($html->find('h2') as $header) {
$headlines[] = $header->plaintext;
}
print_r($headlines);
?>
when i test this sample on my local server, it prints only:
array ()
if i had well understood it should print:
Python Php Java etc....everything that is inside <h2> tag.
am i missing something?
I have a database query that returns the raw HTML for a page, but if I use it on my page, it gets shown as plain text (of course). How would I format it as HTML so that it uses the tags and such.
An example of what I have in my database:
<div class="test">SOME TEXT HERE</div>
But it is also displayed like that. I would like it to format the text as if it was HTML. So it would just display:
SOME TEXT HERE
But that it would also be in a div with the class: "test"
What would be the best approach to reach this goal?
Im using Twig in the MVC model to render the page. So the page renderer is like this
public function renderArticle() {
$twig = new TwigHelper();
$args['title'] = "Artikel $this->articleId";
$args['blogHTML'] = BlogController::retrieveBlogHTML($this->articleId);
echo $twig->render('article.twig', $args);
}
And the "BlogController::retrieveBlogHTML" goes like this:
public static function retrieveBlogHTML($id) {
$db = DatabaseHelper::get();
$st = $db->prepare("SELECT PageHTML FROM T_Blog WHERE BlogId = :BlogId");
$st->execute([
':BlogId' => $id,
]);
if ($st->errorCode() !== \PDO::ERR_NONE) {
return null;
}
return $st->fetchAll();
}
This means that I will not be able to use JavaScript at this point in time, if that will be the only way to fix the problem i'll have to build a workaround.
So I dont know if I accidently escape too or something along those lines, but im not using any headers.
You need to escape the html characters (so < becomes < for example).
In javascript you can use the HE library or theres this function, which is generally fine, but doesn't cover all possible cases that the HE library does
var encodedStr = rawStr.replace(/[\u00A0-\u9999<>\&]/gim, function(i) {
return '&#'+i.charCodeAt(0)+';';
});
If your using php you can use htmlentities, other languages will have a similar function either inbuilt or provided via a library.
what im currently doing is i have a text area for user to copy and paste the html code.
i want to get a certain element of that html file.
in pure html, this can be done via jquery selector
but i think its a whole different thing when html code is on a variable and considered as a string.
how can i get a certain element location in that way?
code is:
function searchHtml() {
$html = $_POST; // text area input contains html code
$selector = "#rso > div > div > div:nth-child(1) > div > h3 > a"; //example - the a element with hello world
$getValue = getValueBySelector($selector); //will return hello world
}
function getValueBySelector($selector) {
//what will i do here?
}
searchHtml();
You can look at SimpleHTMLDom Parser (manual at http://simplehtmldom.sourceforge.net/manual.htm). This is a powerful tool to parse the HTML code to find and extract various elements and their attribute.
For your particular case, you can use
// Create a DOM object from the input string
$htmlDom = str_get_html($html);
// Find the required element
$e = $htmlDom->find($selector);
Oh, and you've to pass the provided input value to the getValueBySelector() function :-)
I am having many issues getting html to display correctly in my using a dynamic textField in as3. I am having two main issues.
Either the <br> and <p> are not showing he leaves no line breaks or not the correct formatting with no spaces between some words etc...
there is an error loading because I have it stored in the database wrong, or I am sending the var to flash wrong so flash can't read it or display it correctly.
I understand there are a few levels of complexity to this that could be found in one or all of these 3 codes.
Storing it in the database and using best format.
Pulling it from the database and converting it to a string that can be sent to as3.
Getting the as3 to display the html correctly.
I am not worried about css styles or any other html. I just want it to display the format correctly with the right line breaks etc...
One important note. I am pulling the html from an external page so I don't have control over all the extra html code in this, such as styles etc....
In mySql I am storing the html in a TEXT type as opposed to varchar etc...
Here is the php code that is storing the html text.
<?
// I am only showing what I believe to be the relevant code.
//I am mainly concerned about the Content var.
$content = trim(strip_tags(addslashes($content)));
// Also tried the line below without the strip tags
$content = trim(addslashes($content));
mysqli_query($con,"INSERT INTO myDataBase (Content, Title) VALUES ('$content','$t'");
?>
Here the php code that is getting the content from the sql.
<?
while($row = mysqli_fetch_array($result))
{
$Content[] = stripslashes($row['Content']);
}
/// sending to as3
echo "&Content1=$Content[1]";
?>
Here is the as3 code that is displaying the html. (Not working well)
public function popeContent()
{
var formatT = new TextFormat( );
formatT.bold = false;
formatT.color = 0x454544;
formatT.font = "TradeGothic";
formatT.size = 40;
formatT.leading = 10;
formatT.font = "Arial";
theContent.multiline = true;
theContent.wordWrap = true;
theContent.htmlText = Content1;
theContent.width = 1075;
theContent.y = 0;
theContent.x = 0;
theContent.autoSize = TextFieldAutoSize.LEFT;
theContent.setTextFormat(formatT);
addChild(theContent);
}
Any help to send me in the right direction would be appreciated. Thanks so much.
Good day, I am having a very odd error with my PHP code. I am attempting to dynamically change the content of my webpage by reading from an xml file. I read in some data and assign the data to a PHP variable called $course_title and then attempt to call some Javascript that will update the "innerHTML" of a DOM element. Note: on the line "echo $course_title;' that prints out the correct data to the screen.
$fn = dirname(__FILE__)."/course.xml";
$xml = simplexml_load_file($fn);
$course_bookimagelocation = "";
foreach ($xml->children() as $child)
{
if($child[0]->getName()=="book")
$course_bookimagelocation = $child;
if($child[0]->getName()=="title")
$course_title = $child;
}
//Problems start here...
echo $course_title;
echo '<script type="text/javascript">document.getElementById("heading").innerHTML = "';
echo $course_title;
echo '";</script>';
What is very unique is that the first echo statement prints the correct information, but the next three lines, the .innerHTML does not get changed to that same value. Does anyone know what that is? What is also weird is that if I replace the second "echo '$course_title';" with something like 'Hello world!' then "Hello World!" appears as the innerHTML of the "heading" id object.
EDIT
In the XML file called course.xml, the title element is IC 210 Fall 2010 and the contents of $course_title is IC 210 Fall 2010 so the parsing of the php file appears to be correct and there does not seam to be any special characters like a " or '
I also tried putting everything on the same line and not luck... (I even made sure in the xml file that the <title> element had the text on the same line...
Make sure that .innerHTML = "Some text" are on the same line because .innerHTML = "\nSome Text\n" won't work in javascript.
If you want to have something like this:
.innerHTML = "
Some String
";
try:
.innerHTML = ""+
"Some String" +
"";
"call some Javascript that will update the "innerHTML" of a DOM element." for that we use AJAX, no?
well you can do that way but i think it would be simpler to have the text preloaded in some hidden DIV's and use your functions just to change content's of the DIVS or even toggle visibility of the DIV (as you prefer). That way you will avoid many escaping problem's referred in the comments.
that's the way i would do it if not to use AJAX