I have a table inwhich I also use php's echo to display data inside td elements. Now I have another condition I need to apply for it as whenever the condition is satisfied the table is displayed or else it is removed/turned off.
<?php if(condition)
{
<table>
</table>
}
?>
I would like to know which way to do this best and could help me not to type in " ' " at the beginning of each element's square bracket throughout the whole table ?
UPDATE
It's very simple to geeks and all the talented coders.
if the condition is satisfied then display the table, otherwise don't
display anything in the view. Please remember also that inside my table I also use echo > $data etc to display table's data elements.
<?php
if (condition)
{
?>
<table>
</table>
<?php
}
?>
I do not understand the second part of the question (about the single quotes).
You can use any one method out of given two:
First:
<?php if(condition)
{
?>
<table>
<?php //some statement?>
</table>
<?php
}
?>
Second:
<?php if(condition) : ?>
<table>
<?php //some statement?>
</table>
<?php
endif;
?>
if this is what you mean..
<?php if(condition): ?>
<table>
...
</table>
<?php else: ?>
... do something else in html TAG
<?php endif; ?>
if you dont want to use quotes then yes the Logans answer is the best
Else you can use print or echo
All the best
Related
I need help writing a php echo that checks if two cells are the same in two tables.
The cells are:
posts>id and reblogs>newid
I have the following code to start with:
<?php if(!empty($reblogDetails->newid)): ?>
<table><tr><td>
<?php echo $newid;?>
</td><tr></table>
<?php else: ?>
original upload
<?php endif ?>
which I borrowed from another snippet but isn't working.
can anyone help?
Your question is not completely clear, but I am assuming you want to show the table with an anchor tag only if the posts->id is equal to the reblogDetails->newid? If that is the case the code you would want would be more like the below.
<?php if(!empty($reblogDetails->newid) && $posts->id == $reblogDetails->newid): ?>
<table><tr><td>
<?php echo $newid;?>
</td><tr></table>
<?php else: ?>
original upload
<?php endif ?>
That being said I doubt your anchor tag is doing what you want. I would anticipate it creating an anchor that looks something like
12
You can see that I have a 3 element array. I am using a while loop to then print out all 3 values into a table, one row for each of the three values, but I do not understand how three rows are being printed out when I only have one row hard coded using html code. The PHP while loop does not echo the tr and td tags for each row because those row and detail tags are outside the PHP code. The code works -- it prints out one additional new table row for each value of "mary","donna","shirley", but I do not understand how. I could see it working if the tr and td tags were output by a PHP echo statement inside the while loop, but that is not the case here.
<html>
<body>
<table cellspacing ="2" cellpadding ="2" align ="center" border="8">
<?php
$ar1=["mary","donna","shirley"];
$len=count($ar1);
$ct=0;
?>
<?php while ( $ct<$len) { ?>
<tr>
<td>
<?php echo $ar1[$ct];
$ct++;
?>
</td>
</tr>
<?php } //end while loop?>
</table>
</body>
</html>
I believe the answer lies in the fact that HTML is an interpreted language not a compiled language.
So in this case your php while loop is setting the browser back to the spot just before your first tr tag so it goes through and interprets those tags again, and as it does this it puts them to the page again. Doing your while loop like this is a cheap way to do echos essentially.
I'm not a PHP master by any means, but from my understanding of how HTML is read and how PHP works this is my answer.
Your <tr> tag is inside the while loop. If you want just one row, try this-
<html>
<body>
<table cellspacing ="2" cellpadding ="2" align ="center" border="8">
<?php
$ar1=["mary","donna","shirley"];
$len=count($ar1);
$ct=0;
?>
<tr>
<?php while ( $ct<$len) { ?>
<td>
<?php echo $ar1[$ct];
$ct++;
?>
</td>
<?php } //end while loop?>
</tr>
</table>
</body>
</html>
This piece of code:
<?php while ( $ct<$len) { ?>
<tr>
<td>
<?php echo $ar1[$ct];
$ct++;
?>
</td>
</tr>
<?php } //end while loop?>
is the same as
<?php while ( $ct<$len) {
echo "<tr>
<td>";
echo $ar1[$ct];
$ct++;
echo "</td>
</tr>";
} //end while loop?>
if you analyze code more deeply..u will understand it yourself..you said "The php while loop does not echo the tr and td tags for each row because those row and detail tags are outside the php code"
but it does ..the html code is not the part of php code it coded outside php scope...and whenever your while loop executes it again reads the tr and tg tag and insert row and hence u get three rows printed ...
It's an easy case of PHP basic capabilities.
See http://php.net/manual/en/language.basic-syntax.phpmode.php
Everything outside of a pair of opening and closing tags is ignored by the PHP parser which allows PHP files to have mixed content.
PHP parser, don't need to know what is inside the While Loop, it just repeat to the output.
In a php file, by escaping the php code (by way of ?> you basicly say to the script, now comes something non php, you provide Html tags which are then interpreted by the browser as html code. But because you're still in the loop (you haven't ended it by adding an closing tag } you repeat the exit from the code, presenting html, and then entering the code again.
Some coders prefer to just exit php code and to show some html code with a few php tags here and there when there's a large amount of html being displayed. It's a lot less typing than continually using echo statements.
This probably might be a silly question, but what I am trying to do is to make an if statement to do the following:
<?php if ($_SESSION['login'] == true) { ?>
Display this HTML code (without converting it to PHP echos
<?php } else { ?>
Display this instead
<?php } ?>
Or will I need to echo, and in turn escape all the required characters in order to do what I am after.
Thanks
Just try it out. But for the record, this works. And is in fact an idiomatic way of solving this.
<?php if ($_SESSION['login']) { ?>
<p>Display this HTML code</p>
<?php } else { ?>
<p>Display this instead</p>
<?php } ?>
Indented for readability (however, this messes with the HTML structure indentation so maybe it’s not appropriate).
Alternatively, the following style is often used because the lone brace at the end gets lonely:
<?php if ($_SESSION['login']): ?>
<p>Display this HTML code</p>
<?php else: ?>
<p>Display this instead</p>
<?php endif; ?>
(In both cases I’ve removed the == true from the conditional because it’s utterly redundant. Don’t write == true.)
you can also use if and endif
<?php if ( expression ) : ?>
<p>some message here</p>
<?php else : ?>
<p>other message</p>
<?php endif ?>
Look into the HEREDOC or NOWDOC syntax
<?php
if ($_SESSION['login']) {
$html =<<<HTML
Add HTML here
HTML;
echo $html;
} else {
$other_html =<<<'OTHERHTML'
Add HTML here
OTHERHTML;
echo $other_html;
?>
Anything not in PHP tags will be outputted as HTML anyway, so your original code will work fine.
<?php if ($_SESSION['login'] == true) { ?>
Log Out
<?php } else { ?>
Login
<?php } ?>
Yes, it works.
<?php if ($_SESSION['login'] == true) { ?>
<span>hello</span>
<?php } else { ?>
<span>going already?</span>
<?php } ?>
I am trying to write PHP code to loop through an array to create an HTML table. I have been trying to do something like:
<div id="results">
<table class="sortable">
<?php $results = $statement->fetchAll(PDO::FETCH_ASSOC); ?>
<?php do: ?>
<tr>
<?php for ($i = 0; $i < count($columns); $i++): ?>
<td><?php echo $row[$i] ?></td>
<?php endfor; ?>
</tr>
<?php while (($row = next($results)) != false); ?>
</table>
</div>
So 2 questions:
Is there an equivalent do-while
syntax as there is a for, if, or foreach syntax in
PHP, where you can split the PHP
code up and have HTML in between?
What is this called when you split
PHP code up with HTML in between?
(if there is a special term for it)
I do not know of a do while syntax that behaves like that, but you can still end your PHP block like this:
<div id="results">
<table class="sortable">
<?php $results = $statement->fetchAll(PDO::FETCH_ASSOC); ?>
<?php do { ?>
<tr>
<?php for ($i = 0; $i < count($columns); $i++): ?>
<td><?php echo $row[$i] ?></td>
<?php endfor; ?>
</tr>
<?php } while (($row = next($results)) != false); ?>
</table>
</div>
You can use curly brackets:
<?php do { ?>
foo
<?php } while ($i--); ?>
No. From http://php.net/manual/en/control-structures.alternative-syntax.php :
PHP offers an alternative syntax for some of its control structures; namely, if, while, for, foreach, and switch.
On the other hand, do { ?> ... <?php } while(...) will work just fine.
What you're trying to do can be done with two foreach loops :
<div id="results">
<table class="sortable">
<?php foreach ($statement->fetchAll(PDO::FETCH_ASSOC) as $row): ?>
<tr>
<?php foreach ($row as $element): ?>
<td><?php echo $element ?></td>
<?php endforeach; ?>
</tr>
<?php endforeach; ?>
</table>
</div>
It solves the problem of having to initialize $row (and handling the empty list special case).
I'm not aware of any specific name for this, but I suspect that if you said "embedded HTML inside my PHP", people would understand.
While I've never used the colon syntax as in your example, everything looks basically right except that on your first time through $row is unassigned.
I would switch it around to look like this:
<div id="results">
<table class="sortable">
<?php $results = $statement->fetchAll(PDO::FETCH_ASSOC);
if ($results) {
while ($row = next($results)) {
?>
<tr>
<?php for ($i = 0; $i < count($columns); $i++): ?>
<td><?php echo $row[$i] ?></td>
<?php endfor; ?>
</tr>
<?php }
} ?>
</table>
</div>
This is excessive use of the embedded php tags.. When there is more PHP than HTML, you're better off using PHP and echoing the HTML.
You can mix php and html in every kind of loop, but your current loop does will not work because $row is not defined the first time it gets there.
It doesn't have a special name :)
You may find using the heredoc syntax a bit cleaner than stepping in and out of PHP / HTML like this, though: http://php.net/manual/en/language.types.string.php
You can use variables inside heredoc blocks so you'll be able to access your database results actually inline without dancing about with braces and new lines.
1) Your best bet is to echo the HTML in strings, all within the <%php ... ?> tag.
What you're doing now is an empty for loop inside of a do-while, which is essentially pointless.
2) I believe that's called "inline" coding, where the code and HTML intermingle, but the practice is frowned upon because you generally want to separate logic (PHP) and content (HTML) when developing for the web.
I wanted to know, is there any way to insert an HTML page into PHP without using the include function? I do not want to use an external html file, I want to write the html coding directly into the php coding.
Thanks for your help!
Interleave it:
<?php
// Some php code.
?>
<html>
<head>
<title>Hello!</title>
</head>
<body>
<h1>Header</h1>
<?php /* More php code. */ ?>
<p>Blah!</a>
</body>
</html>
<?php /* Even more php. */ ?>
From a best practices point of view, though, avoid doing this - having business logic (PHP) and presentation (HTML) in the same place makes maintaining harder.
EDIT: To address your comment. You can either do it the same way, or use echo:
<?php if (x == 5) { ?>
<p>Blah!</a>
<?php } else {
echo '<p>Bleh</p>';
} ?>
If you need to include snippets of HTML based on conditions, you can interleave code like this. In this case it's convenient to use the alternative syntax for loop controls
<?php if ( $var ): ?>
<html>
<title>YAY</title>
</html>
<?php endif; ?>
so the code is clearer to read and you retain HTML syntax coloring (if your editor supports it).
It is very bad habit to mix HTML and PHP (for more than just output control), but here you go:
$html = "<div>This is HTML</div>"
echo $html;
or Heredoc syntax:
$html = <<<EOF
<div>
<p>
Some longer HTML
</p>
</div>
EOF;
echo $html;
or using alternative syntax for control statements if the output depends on some condition (or if you loop through an array etc.)(which is far better than building HTML with strings):
<?php if($foo): ?>
<div> Some HTML output </div>
<?php else: ?>
<div> Some other HTML </div>
<?php endif; ?>
or just
<?php //PHP here ?>
<div>HTML</div>
<?php //more PHP ?>
<div>more HTML</div>
<?php //even more PHP ?>