Calling R Script from PHP Page - php

I have a Rscript which I want to call from my PHP page. It saves two plots in a particular location and those are need to be displayed on the browser. The script is running fine in R Studio and plots are being saved at their respective locations. However, no output is being shown on browser.
<?php
exec("C:\Program Files\R\R-3.2.2\bin\Rscript code.R");
?>
<html>
<body>
<img src="C:/EasyPHP-12.1/www/RLinl/myplot1.jpeg">
<br>
<img src="C:\EasyPHP-12.1\www\RLinl\myplot2.jpeg">
<br>
<img src="C:/EasyPHP-12.1/www/RLinl/myplot3.jpeg">
<br>
</body>
</html>
Is there any problem in this call to the script ? The script uses a dataset and no parameters are required to pass.

<html>
<body>
<img src="/RLinl/myplot1.jpeg">
<br>
<img src="/RLinl/myplot2.jpeg">
<br>
<img src="/RLinl/myplot3.jpeg">
<br>
the above code is assuming that your document root is www and the images are being generated from R

Related

PHP / HTML: Import HTML file into another one, while merging duplicates

First of all, I know that there are many questions leading towards including HTML.
The thing is, when I include one HTML (1) file into another (2), using <?php include("1.html") ?>, and both files consist of something like this:
<html>
<body>
<div id="specific div">
<span id="span1">1</span>
</div>
</body>
</html>
Having two different spans in the same specific div - once I include one file into the other one, it would look like this:
<html>
<body>
<div id="specific div">
<span id="span1">1</span>
</div>
<div id="specific div">
<span id="span2">2</span>
</div>
</body>
</html>
while I want the contents of the specific div merged into one of them, instead of having to divs with the same id in the end:
<html>
<body>
<div id="specific div">
<span id="span1">1</span>
<span id="span2">2</span>
</div>
</body>
</html>
How do I achieve that?
EDIT: I found a different and less complicated solution for my specific situation. Therefore I can't really select the correct answer now, so I might select one if it gets enough upvotes.
You could use php's DomDocument::loadHTMLFile() function. With this you can load both of your files and merge them the way you like it.
If your file looks like you said, something like this:
<html>
<body>
<div id="specific div1">
<span id="span">bla bla bla</span>
</div>
</body>
</html>
You can use the DomDocument:
$dom1 = new DomDocument();
$dom1->loadHTMLFile("file_1.html", LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD);
$dom2 = new DomDocument();
$dom2->loadHTMLFile("file_2.html", LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD);
$element = $dom2->getElementById("specific div2")->firstChild;
$dom1->getElementById("specific div1")->appendChild($element);
$merged_html = $dom1->saveHTML()
So this would merge the contents of the div[id="specific div2"] to the div[id="specific div1"]
DOMDocument also supports xpath if you like it more than going through the nodes maually or selecting by id.
If you want to include one html file to another then you can either use iframe or can convert the files to php as well to use include.
Like if you want to include index2.html in index1.html then you can use below iframe code in index1.html:
index1.html
<iframe src="index2.html"></iframe>
Or you can convert your files to PHP and then simply include one file to other like below:
index1.php
<?php include('index2.php'); ?>
You can also use Server Side Include like below to include one html to another:
index1.html
<html>
<body>
<!--#include virtual="/index2.html" -->
</body>
</html>

<img src= url> Not showing image on index.php

I used following code on index.php and it is 100% working on localhost, but not appear image on live server "www.chitthu.co".
<div class="block">
<img src="/img/step-1.png" style="width:20%; height:20%; margin:5px;">
<h3>Join Chitthu</h3>
<p>
Signing up takes two
minutes <br>to get a Chitthu account.
</p>
</div>
step-1 to Step-1.
It seems that your server is case sensitive, which means S and s is very different.
Open your directory :
www.chitthu.co/img/
You don't have this image:
www.chitthu.co/img/step-1.png
You have:
www.chitthu.co/img/Step-1.png
Your directory:
Try this Step-1 http://www.chitthu.co/img/Step-1.png
<img src="http://www.chitthu.co/img/Step-1.png" alt="Step-1"/>

Assign php code to smarty template engine

I make a new website for my company and i want to use smarty (v3.1.29) for it. Now the problem is that we store the code for all pages in our database (home, products, downloads, ...). Some pages contains PHP inline functions like:
<?php include("functions.php"); ?>
<p> Hello <?php echo printWorldInColor(); ?> </p>
In my template (.tpl) file I have a div-section to load the content from our database:
<html>
<body>
<div id="content">
{$content}
</div>
</body>
</html>
So my code looks like this afterwards:
<html>
<body>
<div id="content">
<?php include("functions.php"); ?>
<p> Hello <?php echo printWorldInColor(); ?> </p>
</div>
</body>
</html>
Is there a way that smarty processes the PHP-code before parsing it?
Hint: I dont want to edit my template file. I just want to parse the database content to my content-section of the website.
What i tried:
saved database-content into a string and replaced PHP-tags with smarty-PHP-tags, then assign it to the template
SmartyBC-Class
You can't do that in Smarty. Also running php code stored in the database sounds like a terrible idea. But if for some reason you have to go on with this nonsense (and considering that you can't use eval), you can try this:
Read the php code from the database.
Save to a temporal php file
Turn on output buffering with ob_start()
include the file you have created
assign the output to a variable with ob_get_clean()
assign the variable to the template
But if I was you, I would try to do the project in another way.

Display image using absolute path

I am using uploadify to upload img to server at the time of upload i saved absolute path to the data base its like:
/var/www/html/workbench/photogallery/uploads/logo2.jpg
now I want to display the same in browser the following method does not work
<div id="photos">
<h3>Photo title</h3>
<P class="like">Like </P>
<p class="date">date </p>
<p class="pclear" />
<div id="image">
<img src="<?php echo $result_set['path']; ?>" />
</div>
<p class="about">about image goes here</p>
</div>
above code doesn't work. when I edit the path manually to uploads/logo2.jpg it works perfectly
how can I resolve this problem?
You need to use an url, not a path.
This:
/var/www/html/workbench/photogallery/uploads/logo2.jpg
is a physical path, the address on that server where the image is found. You need to use an url available for your visitors. I guess that you have a name mapped to that server (something like localhost or www.example.com).
From the structure I guess that your url would be something like
http://www.example.com/photogallery/uploads/logo2.jpg
where www.example.com is the base url you are using to get to that application
Try this:
<img src="/photogallery/uploads/<?php echo basename($result_set['path']) ?>" />
Try this it works for me and keeps the location of the images hidden from the browser.
<img src='fake.php' style='max-width:90px;'/>
Create a file called fake.php as follows
<?php
$thePic = "/var/www/html/workbench/photogallery/uploads/logo2.jpg";
$image = imagecreatefromjpeg($thePic);
// Output image and free up the memory
header("Content-type: image/jpeg");
imagejpeg($image);
imagedestroy($image);
?>
You can adjust the code in the fake.php file to retrieve the location of $thePic from posted data or database, etc.

How to offer code for others to embed your content into their html pages?

Maybe the answer to this question is less complicated than I am making it...
I have a Zend Framework PHP web application. I am going to create a simple API that will output a report for the user's account. The content will be as simple as this:
<ul>
<li>Item 1</li>
<li>Item 2</li>
</ul>
I want to offer a code snippet that the user can use to embed the report on their own site.
I'm thinking this can be done with an iFrame, but I've never done this before so I don't know the best way to do this.
Update: If possible, I would also like to offer the ability for the user to style the content. Is that possible with an iFrame? I want the embedded content to look as much like the rest of the page as possible.
At its very simplest:
Create a .js file with:
document.write("<ul>");
document.write(" <li>Item 1</li>");
document.write(" <li>Item 2</li>");
document.write("</ul>");
Your users would then include this code wherever they wanted your embedded code to appear:
<script type="text/javascript" src="http://example.com/path/to/file.js"></script>
This approach allows them to style content. They wouldn't be able to style an IFRAME, but some sites providing IFRAMEable widgets solve this by allowing the user to pick colours, background images, etc. on a setup page.
Ceejayoz & Joe have already covered the 2 basic options, Javascript or IFrame.
If you want to check out some examples, take a look at the StackOverflow flair feature, that does exactly what you are trying to do and allows a user to embed the StackOverflow user info box on their own website.
If you want to see how it's implemented, check out the html which a user would link to with an IFrame, or this Javascript, which the user would call from their page. (see the notes under "how do I use it" on the SO flair page.)
[You need to insert your user number in those links to see the relevant html/js files for yourself.]
In the case of SO, the js/html is generated specifically for each user. For completeness I've included the StackOverflow js/html that achieves this.
JavaScript: You can see when executed this basically writes a script element into the users html page that injects a link to the SO css file into the head. Then it just includes some fairly simple div/span tags which get styled by the css. Obviously, the user could in this case provide their own css instead if they wanted to style it differently.
document.write('
<script>
var link = document.createElement(\"link\");
link.href = \"https://stackoverflow.com/content/flair-Default.StackOverflow.css\";
link.rel = \"stylesheet\";
link.type = \"text/css\";
var head = document.getElementsByTagName(\"head\")[0];
head.appendChild(link);
</script>
<div class=\"valuable-flair\">
<div class=\"gravatar\">
<a title=\"See my profile on Stack Overflow\" target=\"_blank\" href=\"https://stackoverflow.com/users/1/jeff-atwood\">
<img src=\"http://www.gravatar.com/avatar/51d623f33f8b83095db84ff35e15dbe8?s=50&d=identicon&r=PG\" height=\"50\" width=\"50\" alt=\"\">
</a>
</div>
<div class=\"userInfo\">
<span class=\"username\">
<img src=\"http://sstatic.net/so/favicon.ico\" />
Jeff Atwood
<span class=\"mod-flair\" title=\"moderator\">♦</span>
</span>
<br />
<span class=\"reputation-score\" title=\"reputation score\">16,907</span>
<br />
<span title=\"5 gold badges\"><span class=\"badge1\">●</span>
<span class=\"badgecount\">5</span></span><span title=\"55 silver badges\">
<span class=\"badge2\">●</span><span class=\"badgecount\">55</span></span>
<span title=\"72 bronze badges\"><span class=\"badge3\">●</span>
<span class=\"badgecount\">72</span></span>\r\n </div>\r\n</div>'
);
Html - for an IFrame: You can see this is a full HTML document (XHTML actually to be pedantic) that includes everything needed including the link to the css in the head and the content in the body.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link rel="stylesheet" href="http://sstatic.net/so/flair-Default.StackOverflow.css" />
</head>
<body>
<div class="valuable-flair">
<div class="gravatar">
<a title="See my profile on Stack Overflow" target="_blank" href="https://stackoverflow.com/users/1/jeff-atwood"><img src="http://www.gravatar.com/avatar/51d623f33f8b83095db84ff35e15dbe8?s=50&d=identicon&r=PG" height="50" width="50" alt=""></a>
</div>
<div class="userInfo">
<span class="username"><img src="http://sstatic.net/so/favicon.ico" />Jeff Atwood<span class="mod-flair" title="moderator">♦</span></span>
<br />
<span class="reputation-score" title="reputation score">16,907</span>
<br />
<span title="5 gold badges"><span class="badge1">●</span><span class="badgecount">5</span></span><span title="55 silver badges"><span class="badge2">●</span><span class="badgecount">55</span></span><span title="72 bronze badges"><span class="badge3">●</span><span class="badgecount">72</span></span>
</div>
</div>
</body>
</html>
iFrame will work best. An ajax call would work, but you'd have to do what Google does and have them include a src script from your site as well. iFrames are basically div tags that load a page into the content area. They take all the CSS arguments like any other block level element save for the internal content such as font, color, etc.

Categories