Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 9 years ago.
Improve this question
how do you create an if else statement that contains an include statement?
In ASP you need to have the double quotes but I am not sure how to do it in PHP.
I believe the issue lies with this:
<?php include 'i_main-nav-wohl.php' ?>
I Tried the following:
<?php $url = $_SERVER["REQUEST_URI"];
if (strpos($url, "/occupational/wohl/") === 0) {
echo '<?php include ''i_main-nav-wohl.php'' ?>';
} else {
echo '<?php include ''i_main-nav-wohl.php'' ?>';
}
?>
<?php $url = $_SERVER["REQUEST_URI"];
if (strpos($url, "/occupational/wohl/") === 0) {
echo '<?php include "'i_main-nav-wohl.php'" ?>';
} else {
echo '<?php include "'i_main-nav-wohl.php'" ?>';
}
?>
<?php $url = $_SERVER["REQUEST_URI"];
if (strpos($url, "/occupational/wohl/") === 0) {
echo '<?php include 'i_main-nav-wohl.php' ?>';
} else {
echo '<?php include 'i_main-nav-wohl.php' ?>';
}
?>
Echoing in php means that it displays in the browser and does not implement itself. Just set the include directly in the if statement instead of echoing it.
You dont need a second php tag within your if-else-statement. Otherhwise your PHP output will result in a PHP document containing the content of your if or else branch.
Thus, if you want to produce conditional HTML or CSS use echo. Otherwhise just write your PHP commands without additional php tags.
You can do that like that :
<?php
$url = $_SERVER["REQUEST_URI"];
if (strpos($url, "/occupational/wohl/") === 0) {
include 'i_main-nav-wohl.php';
} else {
include 'i_main-nav-wohl.php';
}
?>
Or even better :
<?php
$url = $_SERVER["REQUEST_URI"];
$fileToInclude = strpos($url, "/occupational/wohl/") === 0 ? 'i_main-nav-wohl.php' : 'i_main-nav-wohl.php';
include($fileToInclude);
?>
BTW you are including the same file in both cases.
You shouldn't echo php tag in php code .
If you want your php code to be executed by your server,
Instead of this code :
echo '<?php include ''i_main-nav-wohl.php'' ?>';
just do this :
include 'i_main-nav-wohl.php';
Related
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 5 years ago.
Improve this question
I want to show the server status of an IP address on every page, but to check the status I need a PHP script. This script is what I found on the Internet:
<?php
$server = 'google.be:80';
$split = explode(':', $server);
$ip = $split[0];
$port = (empty($split[1])) ? '80' : $split[1];
$server = $ip . ':' . $port;
$fp = #fsockopen($ip, $port, $errno, $errstr, 1);
if($fp) {
echo $server . ' is online';
fclose($fp);
}
else {
echo $server . ' is offline';
}
?>
I want the echoes to be formatted like my CSS content is formatted, so I could just replace the echoes with:
?>
<p>Server is offline<p>
<?php
and
?>
<p>Server is online<p>
<?php
But then I would have to make every HTML file a PHP file. Would you recommend that or is there a different way to handle this?
On my server all the files are a PHP since I need to include PHP functions such as echo username and such, and I believe it doesn't hurt to convert .html to .php. Another thing is that the following page provides information on styling PHP echoes with CSS.
How can I style a PHP echo text?
I think it would be better have all PHP files.
You could use jQuery AJAX to send the PHP data to your HTML page. You could json_encode the response and receive that data as a JSON object and get the data out of it.
EDIT: In a production enviroment and for efficiency, it would be best if you convert the HTML files to PHP files, it will be worth the labour. However this little snippet below could be used for other functionality if modified or built upon so it's a learning experience for you to see basic jQuery AJAX calls.
The following code is a working example of calling your PHP file and getting back the result. Seeing a basic example of using jQuery and AJAX will help you get a firm grounding of how to use it.
check_server.php
<?php
$server='google.be:80';
$split=explode(':',$server);
$ip=$split[0];
$port=(empty($split[1]))?'80':$split[1];
$server=$ip.':'.$port;
$fp = fsockopen($ip, $port, $errno, $errstr, 1);
$result = new stdClass();
if($fp){
$result->result = 'success';
fclose($fp);
}
else{
$result->result = 'offline';
}
echo json_encode($result);
?>
index.html
<html>
<head>
<title>Website</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script>
$.ajax({
url : "check_server.php",
type : "POST",
dataType: "json",
success : function(results){
if (results.result === 'success')
{
$('#status').append('Server online.');
}
else
{
$('#status').append('Server offline.');
}
},
error : function()
{
$('#status').append('An error has occurred.');
}
});
</script>
</head>
<body>
<div id="status"></div>
</body>
</html>
It is not possible to implement PHP in an HTML file. To create HTML in a .php file is the best solution to solve this.
You can use HTML in a PHP file and you do not have to use PHP in the file if you name it a PHP file.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
Code PHP
<p>'
if($Show['Name6'] != NULL) {
echo '<p><b>Name: </b>'.$Show["Name6"].' <b> Post : </b>'.$Show["Post6"];
}'</p>
</div>';
}
?>
I need help To correct this error
Use like.
<?php
echo '<p>';
if($Show['Name6'] != NULL) {
echo '<p><b>Name: </b>'.$Show["Name6"].' <b> Post : </b>'.$Show["Post6"];
}
echo'</p></div>';
}
?>
<?php
echo '<p>';
if($Show['Name6'] != NULL) {
echo '<p><b>Name: </b>' . $Show["Name6"] . ' <b> Post : </b>'.$Show["Post6"];
}
echo '</p></div>';
?>
Try to use like this.
Your whole code is messed up. Try to fix it like this.
<?php
echo '<p>';
if($Show['Name6'] != NULL) {
echo '<p><b>Name: </b>'.$Show["Name6"].' <b> Post : </b>'.$Show["Post6"];
}
echo '</p>';
?>
This should work
<?php
echo '<p>';
if($Show['Name6'] != NULL) {
echo '<p><b>Name: </b>'.$Show["Name6"].' <b> Post : </b>'.$Show["Post6"];
}
echo '</p></div>';
?>
The HTML will most likely not be "correct" but the code will execute.
Why?
Your code is simply invalid.
This, for example
<p>'
if
Has no proper meaning.
Is
<p>'
suppose to be a string? You need to finish all statements with a semicolon and each string much start with a ' or a ", indicating the beginning of a string.
And this
}'</p>
</div>';
will not echo anything since you're missing the echo keyword.
Try like this way:
<?php
echo "<div><p>";
if($Show['Name6'] != NULL) {
echo <<< END
<p><b>Name: </b>{$Show['Name6']} <b> Post : </b>{$Show['Post6']}
END;
}
echo "</p></div>";
?>
http://www.phpf1.com/tutorial/php-heredoc-syntax.html
If you are using php within html. You should use php syntax when you are using php in html
like :
<p>'
<?php if($Show['Name6'] != NULL) {
echo '<p><b>Name: </b>'.$Show["Name6"].' <b> Post : </b>'.$Show["Post6"];
} ?>'</p>
</div>';
}
?>
As you haven't posted full code. so try adjusting php syntax.
Hope this helps.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have a PHP variable which I am trying to pass into a javascript function. Using pure PHP I am to echo the desired string output, but when I shove it into a variable and output it in javascript it doesn't work.
It seems javascript doesn't seem to see anything at all.
Here's the code:
<?php $a = get_post_thumbnail_id( $post -> ID ); ?>
<?php $img = wp_get_attachment_image_src( $a ); ?>
<?php $b = $img[0]; ?>
<script type="text/javascript">
var myVar = <?php echo $b; ?>;
alert(myVar);
</script>
Whilst this is just a test piece of code I'm trying to make work, the results of which I am trying to make work with something like this:
<?php $a = get_post_thumbnail_id( $post -> ID ); ?>
<?php $img = wp_get_attachment_image_src( $a ); ?>
<?php $b = $img[0]; ?>
<script type="text/javascript">
$(".imgWindow").backstretch("<?php echo $b; ?>");
</script>
There's clearly some underlining principal of PHP and Javascript I must be missing.
Enlighten me please. Help appreciated.
<?php $a = get_post_thumbnail_id( $post -> ID ); ?>
<?php $img = wp_get_attachment_image_src( $a ); ?>
<?php $b = $img[0]; ?>
<script type="text/javascript">
var myVar = '<?php echo $b; ?>';
alert(myVar);
</script>
You have to wrap the value in quotes for non-integer values.
Alternatively, assign like this:
var myVar = '<?=$b;?>';
if it's a string "image name" use quotes
var myVar = '<?php echo $b; ?>';
This question already has answers here:
How to add http:// if it doesn't exist in the URL
(8 answers)
Closed 9 years ago.
I am currently editing a wordpress theme with custom field outputs.
I have successfully made all the edits and everything works as it should.
My problem is that if a url is submitted into the custom field, the echo is exactly what was in there, so if someone enters www.somesite.com the echo is just that and adds it to the end of the domain: www.mysite.com www.somesite.com .
I want to check to see if the supplied link has the http:// prefix at the beginning, if it has then do bothing, but if not echo out http:// before the url.
I hope i have explained my problem as good as i can.
$custom = get_post_meta($post->ID, 'custom_field', true);
<?php if ( get_post_meta($post->ID, 'custom_field', true) ) : ?>
<img src="<?php echo bloginfo('template_url');?>/lib/images/social/image.png"/>
<?php endif; ?>
parse_url() can help...
$parsed = parse_url($urlStr);
if (empty($parsed['scheme'])) {
$urlStr = 'http://' . ltrim($urlStr, '/');
}
You can check if http:// is at the beginning of the string using strpos().
$var = 'www.somesite.com';
if(strpos($var, 'http://') !== 0) {
return 'http://' . $var;
} else {
return $var;
}
This way, if it does not have http:// at the very beginning of the var, it will return http:// in front of it. Otherwise it will just return the $var itself.
echo (strncasecmp('http://', $url, 7) && strncasecmp('https://', $url, 8) ? 'http://' : '') . $url;
Remember, that strncmp() returns 0, when the first n letters are equal, which evaluates to false here. That may be a little bit confusing.
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 4 years ago.
Improve this question
I am working on a website with 2 other developers. I am only responsible to creating the views.
The data is available in an object, and I have getters to read the data then create XHTML pages.
What is the best practice to do this, without using any template engine?
Thanks a lot.
If you don't want to use a templating engine, you can make use of PHP's basic templating capabilities.
Actually, you should just write the HTML, and whenever you need to output a variable's value, open a PHP part with <?php and close it with ?>. I will assume for the examples that $data is your data object.
For example:
<div id="fos"><?php echo $data->getWhatever(); ?></div>
Please note that, all PHP control structures (like if, foreach, while, etc.) also have a syntax that can be used for templating. You can look these up in their PHP manual pages.
For example:
<div id="fos2">
<?php if ($data->getAnother() > 0) : ?>
<span>X</span>
<?php else : ?>
<span>Y</span>
<?php endif; ?>
</div>
If you know that short tag usage will be enabled on the server, for simplicity you can use them as well (not advised in XML and XHTML). With short tags, you can simply open your PHP part with <? and close it with ?>. Also, <?=$var?> is a shorthand for echoing something.
First example with short tags:
<div id="fos"><?=$data->getWhatever()?></div>
You should be aware of where you use line breaks and spaces though. The browser will receive the same text you write (except the PHP parts). What I mean by this:
Writing this code:
<?php
echo '<img src="x.jpg" alt="" />';
echo '<img src="y.jpg" alt="" />';
?>
is not equivalent to this one:
<img src="x.jpg" alt="" />
<img src="y.jpg" alt="" />
Because in the second one you have an actual \n between the img elements, which will be translated by the browser as a space character and displayed as an actual space between the images if they are inline.
Use a separate file to read the data:
<?php
if ($foo == False)
{
$bar = 1;
}
else
{
$bar = 0;
}
?>
Then reference the resulting state in the HTML file:
require 'logic.php';
<html>
<!--...-->
<input type="text" value="<?php echo $bar; ?>" > //Logic is separated from markup
<!--...-->
</html>
i dont know it i get realy your question. so if my answer not exaclty i will like to de deleted
this class will create simple view
class View
{
public function render($filename, $render_without_header_and_footer = false)
{
// page without header and footer, for whatever reason
if ($render_without_header_and_footer == true) {
require VIEWS_PATH . $filename . '.php';
} else {
require VIEWS_PATH . '_templates/header.php';
require VIEWS_PATH . $filename . '.php';
require VIEWS_PATH . '_templates/footer.php';
}
}
private function checkForActiveController($filename, $navigation_controller)
{
$split_filename = explode("/", $filename);
$active_controller = $split_filename[0];
if ($active_controller == $navigation_controller) {
return true;
}
// default return
return false;
}
private function checkForActiveAction($filename, $navigation_action)
{
$split_filename = explode("/", $filename);
$active_action = $split_filename[1];
if ($active_action == $navigation_action) {
return true;
}
// default return of not true
return false;
}
private function checkForActiveControllerAndAction($filename, $navigation_controller_and_action)
{
$split_filename = explode("/", $filename);
$active_controller = $split_filename[0];
$active_action = $split_filename[1];
$split_filename = explode("/", $navigation_controller_and_action);
$navigation_controller = $split_filename[0];
$navigation_action = $split_filename[1];
if ($active_controller == $navigation_controller AND $active_action == $navigation_action) {
return true;
}
// default return of not true
return false;
}
}
soo now you can create your templates and can call it from any where just like
$this->view->my_data = "data";
$this->view->render('index/index');
//
and on your index/index.php you can call the data $this->my_data;