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.
Related
I have a while loop and I have a HTML code inside the while loop. Inside this HTML code there is PHP code reading a variable that depends on the condition of the while loop.
This variable should be evaluated by PHP. I wrote the code below to solve this but it doesn't work.
$row[0] depends on the condition of while. My code only outputs an empty space for this variable.
<?php
while ($row = mysql_fetch_row($stt)) {
$html='<div class="data">
<?php echo nl2br($row[0]."\n"); ?>
<p>comment
like
share</p>
<p>
0 <span>likes</span>
</p>
<p>
_____________________
</p>
</div>';
echo $html;
}
?>
If you want to display something row by row, do this:
<?php
while($row = mysql_fetch_row($stt)){
$html="<div class='data'>" . nl2br($row[0]) . "\n<p>comment<a href=''>like</a><a href=''>share</a></p><p>0<span>likes</span></p><p></p></div>";
echo $html;
}
?>
> <?php echo nl2br($row[0]."\n"); ?>
this part should be outside of $html.
The interpreter on server side couldnt run.Accordin to php, this part is not a code block. It is just a text.
I found the answer of my question!!!
This is the solution:
<?php
while($row = mysql_fetch_row($stt)){ ?>
<div class="data">
<?php echo nl2br($row[0]."\n"); ?>
<p>comment
like
share</p>
<p>
0
<span>likes</span>
</p>
<p>
_____________________
</p>
</div>
<?php
}
?>
I want to separate my presentation layer from the logic layer but I'm unsure what I should do when I have to loop or check stuff within the presentation layer?
Variables and other somewhat static content is easy, I can either do
<div class='description'><?php echo $product['description']; ?></div>
or
<div class='description'>{{product_description}}</div>
But how about looping stuff?
Let's say I'm creating a list of users within a table. Is there any other way except putting up a foreach within the view and printing HTML inside the foreach?
<table>
if (!empty($users)) {
foreach ($users as $key => $value) {
echo "<tr data-user-id='$value[id]'>";
echo "<td>$value[id]</td>";
echo "<td>$value[first_name]</td>";
echo "<td>$value[last_name]</td>";
echo "<td><a href='?id=$value[id]&edit=1' class='edit'>Edit</a></td>";
echo "</tr>";
}
}
</table>
This would work perfectly fine but I can't get over the fact that it's not looking so "viewish" anymore. Is it possible to get the view cleaner? What about when I have to add conditions? It gets more and more messy.
Sounds like the main problem your having has to do more with making the view presentation readable. An easy way to accomplish this using PHP templates is to use the alternative syntax for control flow structures. Doing this can make a template look much more readable, similar to twig for instance.
Using your example:
<table>
<?php if (!empty($users)): ?>
<?php foreach ($users as $key => $value): ?>
<tr data-user-id="<?php echo $value[id]; ?>">
<td><?php echo $value[id]; ?></td>
<td><?php echo $value[first_name]; ?></td>
<td><?php echo $value[last_name]; ?></td>
<td>Edit</td>
</tr>
<?php endforeach; ?>
<?php endif; ?>
</table>
Notice that I've removed the open curly braquet { and replaced it with a :. This is then closed on line 10 with a endif;. Same idea is repeated with foreach. This is the alternate syntax for control structures. Normally in business logic You would us the other form, but this can really help when you need to write readble template code for view presentation layers.
I've always wondered: does having a large open and close PHP (i.e. a template) hurt?
To clarify:
<html>
<?php echo $test; ?>
<body <?php echo $test2; ?>>
<table>
<?php foreach ($rows as $row): ?>
<tr>
<td><?php echo $row['cell1']; ?></td>
<td><?php echo $row['cell1']; ?></td>
<td><?php echo $row['cell1']; ?></td>
<td><?php echo $row['cell1']; ?></td>
<?php echo $row['added cells']; ?>
</tr>
<?php endforeach; ?>
</table>
<?php echo $someMorePhp; ?>
<div>
<?php /*do some more php stuff */ ?>
</div>
etc etc
etc
Or would it be advisable to i.e.
<html>
<php echo $test.'<body'.$test2; ?>>
<table>
<?php foreach ($rows as $row) {
echo '<tr>
<td>'.$row['cell1'].'</td>
<td>'.$row['cell2'].'</td>
<td>'.$row['cell3'].'</td>
<td>'.$row['cell4'].'</td>
<td>'.$row['cell1'].'</td>
'.$row['added cells'].'
<tr>';
}
</table>
// etc
I know this might seem like a micro optimization and such. To be clear i'm looking for a rule-of-thumb not a specific usecase... Will it hurt entering and exiting the php engine during a single script run...
No, that is no problem, you should always opt for better readability in this case.
Also think about either activating shorttags if possible (but beware, those might have some sideeffects / problems with XML sytnax as stated in the comments)
<? if ($bool) { ?>
<?= $myVarThatIWantToOutput ?>
<? } ?>
Or think about using a template engine like smarty or twig, which restrict you somehow to force splitting of concerns, make some stuff easier (and more readable) and allow caching of compiled templates to make sure you still get the right speed.
I really don't see the difference between these. How is the templating any better? I don't just don't understand it or how I could convince someone to use it I work with. I understand "separation" of concerns, but I haven't had huge issues. Isn't the first example generating multiple <li> tags just like an echo would? And, I would use php as the "templating language," so I'm not concerned with Smarty or some other system. Thanks.
Why should I use templating system in PHP?
<h1><?=$title?></h1>
<ul>
<?php foreach ($items as $item) {?>
<li><?=$item?></li>
<?php } ?>
</ul>
and (some snippet I found in a forum for an example):
echo "<table>";
for ($i = 0; $i < $largestArray; $i++)
{
echo "<tr>";
if ($i < $array1Size)
{
echo "<td>";
echo $array1[$i];
echo "</td>";
}
else
{
echo "<td>";
echo "null";
echo "</td>";
}
if ($i < $array2Size)
{
echo "<td>";
echo $array2[$i];
echo "</td>";
}
else
{
echo "<td>";
echo "null";
echo "</td>";
}
echo "</tr>";
}
echo "</table>";
or something like this:
<table id="working" border="0" cellspacing="1" cellpadding="3">
<tr>
<?php foreach ($csv->titles as $value): ?>
<td><?php echo $value; ?></td>
<?php endforeach; ?>
</tr>
<?php foreach ($csv->data as $key => $row): ?>
<tr>
<?php foreach ($row as $value): ?>
<td><?php echo $value; ?></td>
<?php endforeach; ?>
</tr>
<?php endforeach; ?>
</table>
EDIT: Should this be a community wiki question?
Your identation seems a bit messed up, but the main point is that the second version is far mor readable (you can easily read tag hierarchy, for example). And a readable code is a code which could be easily maintained or refactored.
Writing the code below using only PHP's echo keyword wouldn't be very readable. You would also had concern about quotes and double quotes. Using a template-like approach, you end up with a code which look similar to an HTML document.
<table id="working" border="0" cellspacing="1" cellpadding="3">
<tr>
<?php foreach ($csv->titles as $value): ?>
<td><?php echo $value; ?></td>
<?php endforeach; ?>
</tr>
<?php foreach ($csv->data as $key => $row): ?>
<tr>
<?php foreach ($row as $value): ?>
<td><?php echo $value; ?></td>
<?php endforeach; ?>
</tr>
<?php endforeach; ?>
</table>
Templating systems such as cakephp and codeignitor use MVC which keeps code cleaner and more standards compliant. You could also write your own MVC.
Template system(s) are not the end-all-be-all solution to separation of presentation logic. They are a solution, and one used by many PHP frameworks. In general "MVC"/MVA/MVP/MVVM PHP development, you do not want any business logic in the presentation layer. Echoing out HTML is a (usually) direct violation of this practice; With templates, business logic is not present in the presentation layer, and vice versa.
Skipping back to the first point, templates are a solution, not the solution. There are a ton of options available for the presentation layer, depending largely on your coding style and/or framework/library usage.
On a personal level I prefer templates (for most small-med projects) for a few reasons beyond what's outlined above:
Makes "theming" and swapping site styles a lot easier. Simply modify the template or implement a theme switcher that can load different templates
Going from mockups to templates is a breeze, because templates are (usually) just HTML with some php snippets echoing out data.
Templates are "dumb." They don't need to know about anything that's happening with the business logic, all they care about is echoing out some data into the HTML.
Readability. This one is almost entirely personal preference, especially considering the wide range of template syntaxes out there.
tl;dr - Templating vs. [insert other solution here] is almost entirely subjective, unless you have constraints based on hosting, customer requirements, traffic levels, etc. If that's the case, then find benchmarks and more about efficiency and scalability and make an informed decision based on the requirements of the project.
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