I'm having a hard time getting the "price" to be displayed. Am I using php the right way in this case?
<html>
<head>
<script src="http://code.jquery.com/jquery-1.12.0.min.js"></script>
</head>
<body>
<table width="100%">
<td>Bitstamp</td>
<?php
$url = "https://www.bitstamp.net/api/ticker/";
$fgc = file_get_contents($url);
$json = json_decode($fgc, true);
$price = $json["last"];
?>
<td><?php echo $price; ?></td>
</table>
</body>
</html>
Yes, it's correct. If you don't get reply from file_get_contents function, you might want to set php setting allow_url_fopen=true
http://php.net/manual/en/filesystem.configuration.php#ini.allow-url-fopen
Related
and thank you in advance for your great work here, i need help with some coding, i got a web page where i show a list of records of a mysql table, with a hyperlink when you click on it creates a variable like world.php?id=1&xx=1050&yy=300 so when the user clicks it takes him to next page and scroll down and up to the x and y it, works fine in web browsers but not working in mobiles. can you help me? the page world.php has
,)">
this is the code i'm using
echo '<a href="world.php?xx='. $_SESSION['x'] .'&yy='. $_SESSION['y'] .'">';
echo '<b>';
echo $row['usuario_atacante'];
echo '</b>';
echo '</a>';
Can you do something like this ?
Row 5
and make links in your world.php file.
<table>
<tr id="row1"><td><h2>Row 1</h2></td></tr>
<tr id="row2"><td><h2>Row 2</h2></td></tr>
<tr id="row3"><td><h2>Row 3</h2></td></tr>
<tr id="row4"><td><h2>Row 4</h2></td></tr>
<tr id="row5"><td><h2>Row 5</h2></td></tr>
</table>
Use a link like: world.php?x=345&y=200 to scroll to that location.
<?php
if(isset($_GET["x"]) && isset($_GET["y"])) {
$x = $_GET["x"];
$y = $_GET["y"];
}
?>
<!DOCTYPE html>
<html>
<head>
<title>World Map</title>
<script>
function scrollWin() {
window.scrollTo(<?php echo $x.",".$y; ?>);
}
</script>
</head>
<body onload="scrollWin()">
<img src="https://upload.wikimedia.org/wikipedia/commons/e/e2/Geoffroy's_Cat.jpg" />
</body>
</html>
This is just a example to get you started.
I have written a PHP script that goes through and changes the HTML generated by Visual Page. (I know - it is a REALLY old program - but I like it.) Anyway, in each of these HTML web pages I'm working with I put in:
{copyright}
Where I want the copyright to show up. I did the following loop:
foreach( $file as $k1=>$v1 ){
if( preg_match("/\{copyright\}/i", $v1) ){
$file[$k1] = preg_replace( "/\{copyright\}/i", $copyright, $v1 );
}
}
This DID NOT WORK. I would echo out the $file[$k1] before and after the IF statement so I could see what was going on and PHP just wouldn't change the {copyright} to the copyright. The variable $copyright had something similar to:
<tr><td>Copyright 2007-NOW MyCompany. All rights reserved.</td></tr>
Now - here is the freaky thing: I put a BREAK after it did the preg_replace - and - it worked. So just changing the above to
foreach( $file as $k1=>$v1 ){
if( preg_match("/\{copyright\}/i", $v1) ){
$file[$k1] = preg_replace( "/\{copyright\}/i", $copyright, $v1 );
break;
}
}
Made it work. Does anyone have ANY kind of an idea why? I'm completely stumped by this.
Note: I thought I'd post what I had gotten the HTML down to.
<html>
<head>
<title>Test</title>
</head>
<body>
<table border='1' cellspacing='0' cellpadding='0'><tbody>
<tr><td>This is a test</td></tr>
{copyright}
</tbody></table>
</body>
</html>
That is what I boiled my test case down to.
Also note : I did get this to work. Don't know why I had to put in a BREAK statement and I put it in on a whim. My thinking went "Maybe there is something that is making it re-evaluate the string after the change? Let me try putting in a break statement." I did and - it worked. But I have no idea WHY it worked.
Maybe I'm missing something here but it doesn't seem like you need any regex's here. The str_replace function, http://php.net/str_replace, should work fine for this.
Example:
$string = "<html>
<head>
<title>Test</title>
</head>
<body>
<table border='1' cellspacing='0' cellpadding='0'><tbody>
<tr><td>This is a test</td></tr>
{copyright}
</tbody></table>
</body>
</html>";
$copyright = '<tr><td>Copyright 2007-NOW MyCompany. All rights reserved.</td></tr>';
echo str_replace('{copyright}', $copyright, $string);
Output:
<html>
<head>
<title>Test</title>
</head>
<body>
<table border='1' cellspacing='0' cellpadding='0'><tbody>
<tr><td>This is a test</td></tr>
<tr><td>Copyright 2007-NOW MyCompany. All rights reserved.</td></tr>
</tbody></table>
</body>
</html>
Demo: http://sandbox.onlinephpfunctions.com/code/f53b1a96f270e52392303d7dfb7c327372747d0b
Update per comment:
$string = "<html>
<head>
<title>Test</title>
</head>
<body>
<table border='1' cellspacing='0' cellpadding='0'><tbody>
<tr><td>This is a test</td></tr>
{copyright}
</tbody></table>
</body>
</html>";
$copyright = '<tr><td>Copyright 2007-NOW MyCompany. All rights reserved.</td></tr>';
foreach(explode("\n", $string) as $line) {
echo str_replace('{copyright}', $copyright, $line) . "\n";
}
Output:
<html>
<head>
<title>Test</title>
</head>
<body>
<table border='1' cellspacing='0' cellpadding='0'><tbody>
<tr><td>This is a test</td></tr>
<tr><td>Copyright 2007-NOW MyCompany. All rights reserved.</td></tr>
</tbody></table>
</body>
</html>
Although I am not yet sure exactly what is wrong - with the help of chris85 I no longer believe it is a PHP problem but instead is probably a hardware problem.
Chris posted to the PHP sandbox what I said I was using and it worked without a flaw. When I tried it ON the PHP sandbox (which means it used the sandbox's PHP interpreter) - it also worked without a problem.
Then I modified the program so it did the same thing but in two other ways. Thus, test #1 was just the single string, test #2 was the exploded string, and test #3 echoed out each individual string as it was modified.
On >MY< machine, the third method caused unexpected output to be generated. As stated though, the PHP sandbox computer did NOT have anything strange happen to it. Thus, this means that there is a hardware problem with my computer and not with PHP.
Sandbox link: http://sandbox.onlinephpfunctions.com/code/f34f57f6521cc6eebcc704b7c127974c0403834d
This question can now be closed.
I'm using this line of php in my main page
echo generateRadioButtons("fbresponse.php", "moRating1", 6);
Which when posting the following on the response file
echo $_POST['moRating1']
It works fine and displays the correct result, but! my question is how would i add text to that so..
Blah blah blah, you rated x question: 'moRating1'
I've tried doing
<html>
<head>
<title>Questions</title>
</head>
<body>
<h1>Survey responses</h1>
<p>How well did you rate it : <?php print $moRating1 ?></p>
</body>
</html>
inside the response file but that just doesnt load anything..
Any help please!
It's probably because this function uses eval() to execute its content (I guess it from lack of PHP tags in your first example).
If it's true, then you should be able to close PHP tag, print HTML and open it again.
?>
<html>
<head>
<title>Questions</title>
</head>
<body>
<h1>Survey responses</h1>
<p>How well did you rate it : <?php print $_POST['moRating1'] ?></p>
</body>
</html>
try doing:
$mRating1 = $_POST['moRating1'];
...
?>
...
<p>How well did you rate it: <?php echo $mRating1?></p>
I would like to ask some help and ideas on how to implement a loop inside the template. I can do foearch below but how can i include it to the template and show it in the results.
foreach($results as $row) {
$name = $row['name'];
$address = $row['address'];
}
What i want to achieve the results is something like below and how do I put the $template->publish(); in a variable so I can use it to store that data to the DB. thanks a lot.
<html>
<head>
<title>My Template Class</title>
</head>
<body>
<table><tr>
<td>
<h3>Hello William!</h3>
<p>The time is: 03/10/04</p>
<p>Embedded PHP works too!</p>
<p>Name goes here</p>
<p>Address goes here </p>
</td>
<td>
<h3>Hello William!</h3>
<p>The time is: 03/10/04</p>
<p>Embedded PHP works too!</p>
<p>Name goes here</p>
<p>Address goes here </p>
</td>
<td>
<h3>Hello William!</h3>
<p>The time is: 03/10/04</p>
<p>Embedded PHP works too!</p>
<p>Name goes here</p>
<p>Address goes here </p>
</td>
</tr>
</table>
</body>
</html>
The template class
<?
class Template {
public $template;
function load($filepath) {
$this->template = file_get_contents($filepath);
}
function replace($var, $content) {
$this->template = str_replace("#$var#", $content, $this->template);
}
function publish() {
eval("?>".$this->template."<?");
}
}
?>
The template design.html
<html>
<head>
<title>#title#</title>
</head>
<body>
<h3>Hello #name#!</h3>
<p>The time is: #datetime#</p>
<? echo "<p>Embedded PHP works too!</p>"; ?>
</body>
</html>
the index.php
<?
include "template.class.php";
$template = new Template;
$template->load("design.html");
$template->replace("title", "My Template Class");
$template->replace("name", "William");
$template->replace("datetime", date("m/d/y"));
$template->publish();
?>
PHP itself is as good at templates as any other engine.
No need anything else
$pagetitle = "My Template Class";
foreach($results as $row) {
$row['date'] = date("m/d/y");
$data[] = $row;
}
$data = chunk_split($data,3);
Then in template
<html>
<head>
<title><?=$pagetitle?></title>
</head>
<body>
<table>
<?php foreach ($data as $chunk): ?>
<tr>
<?php foreach ($chunk as $row): ?>
<td>
<h3>Hello <?=$name?>!</h3>
<p>The time is: <?=$date?></p>
<p>Embedded PHP works in the template</p>
<p><b>But embed PHP in the data is a VERY BAD IDEA</b></p>
<p><?=$address?></p>
</td>
<?php endforeach ?>
</tr>
<?php endforeach ?>
</table>
</body>
</html>
I made your example a bit more complicated yet closer to the real life.
It will print your table in the rows by 3 columns in each
Just don't re-invent the wheel, PHP works wonderfully as a templating language:
The template class
<?
class Template
{
private $template;
private $vars;
function load($filepath) {
$this->template = $filepath;
}
function replace($var, $content)
{
$this->vars[$var] = $content;
}
function publish()
{
extract($this->vars);
include($this->template);
}
}
?>
The template design.phtml
<html>
<head>
<title><?php echo $title ?></title>
</head>
<body>
<?php foreach($rows as $row) { extract($row); ?>
<h3>Hello <?php echo $name; ?></h3>
<p>The time is: <?php echo $datetime; ?></p>
<?php echo "<p>Embedded PHP works too!</p>"; ?>
<?php } ?>
</body>
</html>
The use is pretty much the same, just assign more than one row to make use of it:
<?
include "template.class.php";
$template = new Template;
$template->load("design.phtml");
$template->replace("title", "My Template Class");
$rows = array();
$rows[] = array(
"name" => "William",
"datetime" => date("m/d/y"),
);
$template->replace("rows", $rows);
$template->publish();
?>
Hope this is helpful.
Your PHP code:
$htmldata ="";
($results as $row) {
$name = $row['name'];
$address = $row['address'];
$htmldata .="
<tr><td>
<h3>Hello William!</h3>
<p>The time is: 03/10/04</p>
<p>Embedded PHP works too!</p>
<p>".$name."</p>
<p>".$address." </p>
</td>
</tr>
";
}
Then in your template design.html, you will pass the $htmltable variable and embedd there:
<html>
<head>
<title>#title#</title>
</head>
<body>
<h3>Hello #name#!</h3>
<p>The time is: #datetime#</p>
<? echo "<p>Embedded PHP works too!</p>"; ?>
<table>
<?php echo $htmltable; ?>
</table>
</body>
</html>
I would simply like to know if something similar to this is possible in php somehow:
<?php
$myhtmlstring = "
?>
<table>
<tr>
<td>test</td>
</tr>
</table>
<?php
";
?>
The reason for this is I would like to be able to write the html in this nice looking format but have php trim the white space after the fact.
You can use heredoc.
You can use the alternative heredoc syntax:
$myhtmlstring = <<<EOT
<table>...</table>
EOT;
Or you can use output buffering:
<?php
ob_start();
?>
<table>...</table>
<?php
$myhtmlstring = ob_get_clean();
?>
Yes
<?php
$myhtmlstring = '
<table>
<tr>
<td>test</td>
</tr>
</table>
<?php
';
// Do what you want with the HTML in a PHP variable
// Echo the HTML from the PHP variable to make the webpage
echo $myhtmlstring;
?>
I usually use the buffer functions, like so:
<?php
$whatever = "Hey man";
// This starts the buffer, so output will no longer be written.
ob_start();
?>
<html>
<head>
<title><?php echo $whatever ?></title>
</head>
<body>
<h1><?php echo $whatever ?></h1>
<p>I like this in part because you can use variables.</p>
</body>
</html>
<?php
// Here's the magic part!
$myhtmlstring = ob_get_clean();
?>
For more information about the buffer functions, look up ob_start() on
php.net.
do you mean so?
<?php
$string = '<table border="1">
<tr>
<td> test </td>
</tr>
</table>';
echo $string;
?>