How to use if-else in html [closed] - php

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 6 years ago.
Improve this question
I want to use selected option with condition. if $type is room then bed will be selected and if it is bathroom then toilet will be selected. what is wrong with this code. how to correct it????
<html>
<body>
<?php
$type="bathroom";
?>
<form method="post" action="<?php $_PHP_SELF ?>">
<select name="bedtype">
<% if($type=="bathroom"){ %>
<option value="room">BED</option>
<option value="bathroom" selected>TOILET</option>
<% } else { %>
<option value="room" selected>BED</option>
<option value="bathroom" >TOILET</option>
<% } %>
</select>
</form>
</body>
</html>

You are mixing ASP and PHP tags.
While ASP tags were allowed in PHP 5, as of PHP 7, they have been deprecated and removed. You should switch to strictly <?php ?> and <?= ?>.
http://php.net/manual/en/language.basic-syntax.phptags.php
You also have $_SERVER['PHP_SELF'] wrong.
Ternary conditionals would simplify the code greatly. They are in the form if true statement ? do truth : do falsehood. Providing an echo statement before the ternary conditional would echo out the true or false result.
<html>
<body>
<?php $type = "bathroom"; ?>
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<select name="bedtype">
<option value="room" <?php echo $type == "room" ? 'selected' : ''; ?>>BED</option>
<option value="bathroom" <?php echo $type == "bathroom" ? 'selected' : '' ?>>TOILET</option>
</select>
</form>
</body>
</html>

Your code is correct in PHP < 7.0.
You use optional ASP tags ( <% (...) %> ) to embed PHP code.
To activate it, you have to modify your php.ini file turning this line:
asp_tags = Off
in:
asp_tags = On
Please note that ASP tags are rarely used and completely removed on PHP 7.0.
To avoid future incompatibility issue, use standard php tags <?php (...) ?>
As alternative, to echo something, you can use short tags <?= (...) ?>.
So, this:
<?php echo 'Hello World'; ?>
is the same as:
<?= 'Hello World'; ?>
or (you can omit closing semicolon):
<?= 'Hello World' ?>
For the records, there is another short-tag style ( <? (...) ?> ) that must be enabled in php.ini. However, the use of this short-tag is discouraged and substantially abandoned (this style is primary used to identify XML).

Related

Can someone explain this PHP code? [closed]

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 7 years ago.
Improve this question
<h1 class="site-title">
<a href="<?php echo esc_url( home_url( '/' ) ); ?>"
rel="home"><?php bloginfo( 'name' ); ?>
</a>
</h1>
if (is_category('Ponies')) { ?>
// overlay a pretty rainbow on the logo for the ponies category
<img id="rainbow"
src='<?php bloiginfo('template_directory');?>/img/rainbow.png"
alt="OMG! Ponies! " />
<?php } ?>
I'm having trouble matching the PHP tags. The comment for the code says "Now any time the category of the content is Ponies, your header also includes the rainbow.png." But it's clear how that is happening. The actual code is on p245 of WordPress Design and Developement by Williams. Thanks for putting another pair of eyes on it.
"If" is not inside <?php ... ?>. Must be:
<?php if (is_category('Ponies')) { ?>
I prefer to use <?php if (condition): ?> when there's HTML in-between.
But anyhow...
1) The if() statement needs to be inside php tags.
2) You don't need echo to retrieve the bloginfo.
bloginfo() documentation
3) You've misspelled bloginfo at the bottom...
My code:
<h1 class="site-title">
<a href="<?php echo esc_url(home_url('/')); ?>" rel="home">
<?php $bloginfo('name'); ?>
</a>
</h1>
<?php if (is_category('Ponies')) : ?>
<img id="rainbow"
src="<?php get_bloginfo('template_directory') . '/img/rainbow.png'; ?>"
alt="OMG! Ponies!" />
<?php endif; ?>

Why am I getting an unexpected eof? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
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.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Improve this question
I am working on coding this up with a mixture of PHP and HTML but I can't seem to understand why I am getting an eof yet when I exclude the middle php part, it works just fine. i can't seem to understand why it is giving me an error. I compared it to another part of my program and when I looked at a similar page, it works but this one is simply giving me an unexpected eof
Here is the code. Any suggestions?
<!DOCTYPE html>
<?php
require_once( "data.php" );
error_reporting(0);
?>
<html>
<head>
<link rel="stylesheet" href="http://netdna.bootstrapcdn.com/bootstrap/3.0.0/css/bootstrap.min.css">
</head>
<body>
<div class="container">
<div class="page-header">
<h1>Course Listings</h1>
</div>
<table class="table">
<?php
foreach( $data as $value )
{
?>
<tr>
<td> <? $value[number] ?> </td>
<td> <? $value[name] ?> </td>
<td> <? $value[instructor] ?> </td>
</tr>
<?
};
?>
</table>
</div>
<script src="http://code.jquery.com/jquery.min.js"></script>
</body>
</html>
The closing bracket of your foreach is not being processed because short tags are disabled. This causes the unexpected eof because php still expects a closing bracket.
Use the standard tag <?php everywhere.
Fred-ii most likely had it right in his comment. if that doesn't completely solve it, replace your <table> contents with this and see if it works. As the other guys mentioned, you may have an issue with <?=
<table class="table">
<?php foreach($data as $value ): ?>
<tr>
<td> <?php echo $value['number']; ?> </td>
<td> <?php echo $value['name']; ?> </td>
<td> <?php echo $value['instructor']; ?></td>
</tr>
<?php endforeach; ?>
</table>

PHP within HTML etiquette [closed]

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 9 years ago.
Improve this question
This has been bothering me for quite some time and I want to know what the typical etiquette is when writing PHP code within HTML.
I have the following code:
<?php
$country_list = array("AF"=>"Afghanistan", "AL"=>"Albania", "DZ"=>"Algeria");
echo '<div class="some-class">
<font color="red">Just some text.</font>
<select>';
foreach($country_list as $country_code => $country_name) {
echo '<option value="' . $country_code . '">' . $country_name;
}
echo '</select>
</div>';
?>
This to me looks extremely ugly because of how the HTML isn't lined up and it's hard to work with something that doesn't look good.
Is there any way I can improve so that it looks better? Is there some sort of PHP etiquette when writing PHP code like this inline with HTML?
Thanks.
It's common practice to separate your code that outputs the HTML (the "view") from your logic (typically in a "controller"). At a minimum, this can be done with PHP code that just handles output, but is usually done with a template engine, such as Smarty.
A template engine can be overkill if you have a simple application. In these cases, there are some things you can do to clean up your code:
Disregard HTML indentation. Only worry about your code indentation in PHP. That's where it matters to you anyway. If you need to use indented HTML, let your browser's developer tools worry about it.
Avoid closing/opening PHP tags repeatedly. Just stick to echo. Again, if you need to mix PHP and HTML so much that you feel the need to repeatedly close/open PHP tags, consider a template engine.
Finally, be sure to use htmlspecialchars() around any arbitrary data you output into HTML to ensure that you are generating valid HTML, and avoiding certain kinds of XSS attack vulnerabilities.
This is all pretty subjective stuff, but you could consider only breaking into PHP when absolutely necessary:
<div class="some-class">
<font color="red">Just some text.</font>
<select>
<?php
$country_list = array("AF"=>"Afghanistan", "AL"=>"Albania", "DZ"=>"Algeria");
foreach ($country_list as $country_code => $country_name) {
printf('<option value="%s">%s</option>', $country_code, $country_name);
}
?>
</select>
</div>
I shall assume your values don't need to be escaped here, i.e. no "naughty countries".
Alternatively, there are template engines that can help you with it, such as PHPTAL:
<div class="some-class">
<font color="red">Just some text.</font>
<select>
<option tal:repeat="country countries" tal:attributes="value repeat/country/key" tal:content="country" />
</select>
</div>
PHP code:
$tal = new PHPTAL('path/to/template');
$tal->set('countries', $countries);
echo $tal->execute();
The nice thing here is that everything stays in HTML mode and you've separated display logic (how to show data) from business logic (how to generate it).
this is what I usually do
<?php
$country_list = array("AF"=>"Afghanistan", "AL"=>"Albania", "DZ"=>"Algeria");
$countries = '';
foreach($country_list as $country_code => $country_name) {
$countries. = '<option value="' . $country_code . '">' . $country_name;
}
?>
//html part
div class="some-class">
<font color="red">Just some text.</font>
<select name="country>
<?php echo $countries; ?>
</select>
</div
You could do something like this... I do it sometimes
<?php
$country_list = array("AF"=>"Afghanistan", "AL"=>"Albania", "DZ"=>"Algeria");
?>
<div class="some-class">
<font color="red">Just some text.</font>
<select>
<?php foreach($country_list as $country_code => $country_name) { ?>
<option value="<?=$country_code?>"><?=$country_name?></option>
<?php } ?>
<select>
</div>
Not sure about "etiquette" but echoing HTML may not be the best idea. Maybe something like this:
<div class="some-class">
Just some text
<select>
<?php
foreach($country_list as $country_code => $country_name) {
?>
<option value="<?php echo $country_code;?>">
<?php echo $country_name; ?>
</option>
<?php
}
?>
</select>
</div>
So minimize echoing HTML tags.

PHP syntax alternatives? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
Are there any way to use this alternative syntax?
<div class="lines-rates">
<?php foreach($info as $k => $v){ echo : ?>
<div><span>{$k}</span>{$v}</div><?php ; } ?>
</div><!-- #lines -->
I mean:
<?php echo : ?><p>this is your {$username} And this html code could be more than one line and sure other html elements also</p><?php endecho; ?>
So we can easily see html code.
I want to have html inside PHP. Not PHP inside html.
<?php echo '<p>this is your '.$username.' And this html code could be more than one line and sure other html elements also</p>'; ?>
It's definitely not the way that I want.
I'm using
<?php if($info) : ?>
<div class="lines-rates">
<?php foreach($info as $k => $v){ echo "<div><span>{$k}</span>{$v}</div>"; } ?>
</div><!-- #lines -->
<?php endif; ?>
I do not want to use html inside php.. Like
<?php echo "<div><span>{$k}</span>{$v}</div>";?>
Also following line is possible but this is too verbose for me - is it not? Why do I have to write <?php echo $k; ?> to echo simple variable?
<?php $if($info) : ?>
<h2>Weekly Charter Rates </h2>
<div class="lines-rates">
<?php foreach($info as $k => $v) : ?>
<div><span><?php echo $k; ?></span><?php echo $k; ?></div>
<?php endforeach; ?>
</div><!-- #lines -->
<?php endif; ?>
I need clean and pure code as much as possible.
This type of writing again fails.
<?php foreach($info as $k => $v) : echo <<<EOT ?>
<p>this is your {$username}</p>
...
some more html
..<div><span>$k</span>$k</div>
<?php EOT; ?>
<?php endforeach; ?>
It was almost best way.
Use this:
<div class="lines-rates">
<?php foreach($info as $k => $v){?>
<div><span><?php echo $k; ?></span><?php echo $v; ?></div>
<?php } ?>
</div>
If you are using only two variable of PHP then no need to echo whole line, just echo those variables.
You can use Heredoc syntax
echo <<<EOT
<p>this is your {$username}</p>
...
some more html
..
EOT;
UPDATE
there must be no other content in the line of second "EOT". Quote from link I've given:
It is very important to note that the line with the closing identifier
must contain no other characters, except possibly a semicolon (;).
UPDATE 2
Change
<?php EOT; ?>
to
<?php
EOT;
?>
DO NOT IDENT SECOND LINE
You could use a templating language such as Twig. Example syntax:
<ul id="navigation">
{% for item in navigation %}
<li>{{ item.caption }}</li>
{% endfor %}
</ul>
what about this
<?php echo '<p>this is your '.$username.' And this html code could be more than one line and sure other html elements also</p>'; ?>
If you're feeling really adventurous you can choose to enable short tags, but both Heredoc and templating engines like Twig mentioned above would be a better idea.
<p>this is your <?=$username;?> And this html code could be more than one line and sure other html elements also</p>
You can also make use of nowdoc
echo <<<'END_OF_HTML'
$hello this is {$a->test}
END_OF_HTML;
OUTPUT:
$hello this is {$a->test}

delete page is not working [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions must demonstrate a minimal understanding of the problem being solved. Tell us what you've tried to do, why it didn't work, and how it should work. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I am creating a cms and have set up all the pages perfectly but the delete page.
I have this code for my delete.php:
<?php
session_start();
include_once('../include/connection.php');
include_once('../include/article.php');
$article = new Article;
if (isset($_SESSION['logged_in'])) {
$articles = $article->fetch_all();
?>
<html>
<head>
<title>testing</title>
<link rel="stylesheet" href="../style.css" />
</head>
<body>
<div class="container">
CMS
<br /><br />
<form action="delete.php" method="get">
<select onchange="this.form.submit();">
<?php foreach ($articles as $article){ ?>
<option value="<?php echo $article['article_id']; ?>"><?php echo $article['article_title']; ?></option>
<php } ?>
</select>
</form>
</div>
</body>
</html>
<?php
} else {
header('Location: index.php');
}
?>
But in my error log It is telling me this:
PHP Parse error: syntax error, unexpected T_ELSE in /delete.php on line 37
Line 37 is "} else {" please can someone advise me as to where I am going wrong?
thank you.
Your php opening tag is invalid. Change
< php } ?>
to
<?php } ?>

Categories