Is it possible to configure PhpStorm 8 to indent HTML and PHP code separately?
I'll copy the examples from this question: How to properly indent PHP/HTML mixed code?
How PhpStorm formats the code currently:
<table>
<?php foreach ($rows as $row): ?>
<tr>
<?php if ($row->foo()): ?>
<?php echo $row ?>
<?php else: ?>
Something else
<?php endif ?>
</tr>
<?php endforeach ?>
</table>
How I want it to look like:
<table>
<?php foreach ($rows as $row): ?>
<tr>
<?php if ($row->foo()): ?>
<?php echo $row ?>
<?php else: ?>
Something else
<?php endif ?>
</tr>
<?php endforeach ?>
</table>
No, not currently possible. See this comment
Related
For a web dev projet for my school, I've to program a Trello-like. I've to display board, with different columns but I really don't know how to generate one column per element in my foreach loop.
If someone could help me I'd be thanksfull!
Here's the foreach loop:
<?php foreach ($currentColumns as $currentColumn): ?>
(<?= $currentColumn['title']?> ) <?= $currentColumn['position'] ?>
<?php endforeach; ?>
here, you can do like this..
<table>
<tr>
<?php foreach ($currentColumns as $currentColumn): ?>
<td>
(<?= $currentColumn['title']?> ) <?= $currentColumn['position'] ?>
</td>
<?php endforeach; ?>
</tr>
</table>
I have found following JQuery plugin to show the records in alphabetical orders.
http://esteinborn.github.io/jquery-listnav/
I am using demo one. I have implemented in codeigniter and got successful to print all the records. However, all the records appearing only under "All" option. It does not display records alphabetically.
I am not sure either am I using this plugin in the correct way ? Following is my code:
<?php if (!$companies): ?>
<tr>
<td colspan="4">No companies in the system yet...</td>
</tr>
<?php
else :
?> <ul id="demoOne" class="demo">
<?php
$i = 0;
foreach($companies as $company) :
?>
<li>
<?php print $company->title; ?>
</li>
</ul>
</tr>
<?php
$i++;
endforeach;
endif;
?>
Following is script:
<script>
$(function(){
$('#demoOne').listnav();
});
</script>
Your html doesn't seem to be correct. Try:
<?php if (!$companies): ?>
<tr>
<td colspan="4">No companies in the system yet...</td>
</tr>
<?php else : ?>
<tr>
<ul id="demoOne" class="demo">
<?php
$i = 0;
foreach($companies as $company) :
?>
<li>
<?php print $company->title; ?>
</li>
<?php
$i++;
endforeach;
?>
</ul>
</tr>
<?php
endif;
?>
I want to be able to display titles of the books of the author who is currently logged in. I'm using PHP session
<? foreach ($books as $book ): ?>
<? foreach ($book as $selbook => $author): ?>
<option value="$selbook>"$author['author'] == $_SESSION["sess_username"] ? ' selected="selected"' : ''?>> $author?></option>
<li class="active"><a href=""><span class="pull-right"><input id="button" type="submit" name="submitr" value="Edit"></span><i class="icon-fire$
<? echo htmlspecialchars($book['Title'], ENT_QUOTES, 'UTF-8'); ?> <strong> - </strong><em>
<? echo htmlspecialchars($book['author'], ENT_QUOTES, 'UTF-8');?></em></a></li>
<? endforeach; ?>
<? endforeach; ?>
You start 2nd foreach with this:
<?php foreach ($book as $selbook => $author) { ?>
And end with this:
<?php endforeach; ?>
It is not correct. Use this:
<?php foreach ($book as $selbook => $author): ?>
// some code
<?php endforeach; ?>
Also, why u use <?php? Short tag is not enabled? Use <? it is much faster to write and code is more readable. Also, when you want to echo some variable, using php tag use this:
<?=$variable;?>
Much faster and more readable too.
Update
Try this:
<? foreach ($books as $book ): ?>
<? foreach ($book as $selbook => $author): ?>
<option value="<?=$selbook;?>" <? if($author['author'] == $_SESSION["sess_username"]): ?>selected="selected"<? endif; ?> ><?=$author;?></option>
<li class="active"><a href=""><span class="pull-right"><input id="button" type="submit" name="submitr" value="Edit"></span><i class="icon-fire$
<? echo htmlspecialchars($book['Title'], ENT_QUOTES, 'UTF-8'); ?> <strong> - </strong><em>
<? echo htmlspecialchars($book['author'], ENT_QUOTES, 'UTF-8');?></em></a></li>
<? endforeach; ?>
<? endforeach; ?>
I've been trying to pull one single customer review onto the product page.
I've no code to show as honestly i'm not sure where to start and can't find any mention of it online.
Anyone have any ideas?
I wrote a tutorial earlier on bringing all the review elements onto the product page, so you could follow this tutorial: http://www.e-commercewebdesign.co.uk/blog/magento-tutorials/product-reviews-on-product-view-page.php
All you'd have to do is rename the list block and bring it out in the same way. Then simply modify the loop which brings out the reviews in any way you see fit. E.g. limit to a certain number or only echo out review from a certain user.
EDIT:
To get the latest review is quite simple because the reviews are in date order anyway.
Go to review > product > list.phtml
Replace the code in that file with this:
<?php $_items = $this->getReviewsCollection()->getItems();?>
<div class="box-collateral box-reviews" id="customer-reviews">
<?php if (count($_items)):?>
<h2><?php echo $this->__('Customer Reviews') ?></h2>
<?php echo $this->getChildHtml('toolbar') ?>
<dl>
<?php $r_count = 0; ?>
<?php foreach ($_items as $_review):?>
<?php if ($r_count == 0) { ?>
<dt>
<?php echo $this->htmlEscape($_review->getTitle()) ?> <?php echo $this->__('Review by <span>%s</span>', $this->htmlEscape($_review->getNickname())) ?>
</dt>
<dd>
<?php $_votes = $_review->getRatingVotes(); ?>
<?php if (count($_votes)): ?>
<table class="ratings-table">
<col width="1" />
<col />
<tbody>
<?php foreach ($_votes as $_vote): ?>
<tr>
<th><?php echo $this->escapeHtml($_vote->getRatingCode()) ?></th>
<td>
<div class="rating-box">
<div class="rating" style="width:<?php echo $_vote->getPercent() ?>%;"></div>
</div>
</td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
<?php endif; ?>
<?php echo nl2br($this->htmlEscape($_review->getDetail())) ?>
<small class="date"><?php echo $this->__('(Posted on %s)', $this->formatDate($_review->getCreatedAt()), 'long') ?></small>
</dd>
<?php } ?>
<?php $r_count++; ?>
<?php endforeach; ?>
</dl>
<?php echo $this->getChildHtml('toolbar') ?>
<?php endif;?>
<?php echo $this->getChildHtml('review_form') ?>
</div>
I've simple put an interator into the loop $r_count and put a check inside the foreach which prevent it from progressing on the next loop iteration.
I use brackets when using foreach loops. What is endforeach for?
It's mainly so you can make start and end statements clearer when creating HTML in loops:
<table>
<? while ($record = mysql_fetch_assoc($rs)): ?>
<? if (!$record['deleted']): ?>
<tr>
<? foreach ($display_fields as $field): ?>
<td><?= $record[$field] ?></td>
<? endforeach; ?>
<td>
<select name="action" onChange="submit">
<? foreach ($actions as $action): ?>
<option value="<?= $action ?>"><?= $action ?>
<? endforeach; ?>
</td>
</tr>
<? else: ?>
<tr><td colspan="<?= array_count($display_fields) ?>"><i>record <?= $record['id'] ?> has been deleted</i></td></tr>
<? endif; ?>
<? endwhile; ?>
</table>
versus
<table>
<? while ($record = mysql_fetch_assoc($rs)) { ?>
<? if (!$record['deleted']) { ?>
<tr>
<? foreach ($display_fields as $field) { ?>
<td><?= $record[$field] ?></td>
<? } ?>
<td>
<select name="action" onChange="submit">
<? foreach ($actions as $action) { ?>
<option value="<?= $action ?>"><?= action ?>
<? } ?>
</td>
</tr>
<? } else { ?>
<tr><td colspan="<?= array_count($display_fields) ?>"><i>record <?= $record['id'] ?> has been deleted</i></td></tr>
<? } ?>
<? } ?>
</table>
Hopefully my example is sufficient to demonstrate that once you have several layers of nested loops, and the indenting is thrown off by all the PHP open/close tags and the contained HTML (and maybe you have to indent the HTML a certain way to get your page the way you want), the alternate syntax (endforeach) form can make things easier for your brain to parse. With the normal style, the closing } can be left on their own and make it hard to tell what they're actually closing.
It's the end statement for the alternative syntax:
foreach ($foo as $bar) :
...
endforeach;
Useful to make code more readable if you're breaking out of PHP:
<?php foreach ($foo as $bar) : ?>
<div ...>
...
</div>
<?php endforeach; ?>
as an alternative syntax you can write foreach loops like so
foreach($arr as $item):
//do stuff
endforeach;
This type of syntax is typically used when php is being used as a templating language as such
<?php foreach($arr as $item):?>
<!--do stuff -->
<?php endforeach; ?>
It's just a different syntax. Instead of
foreach ($a as $v) {
# ...
}
You could write this:
foreach ($a as $v):
# ...
endforeach;
They will function exactly the same; it's just a matter of style. (Personally I have never seen anyone use the second form.)
How about this?
<ul>
<?php while ($items = array_pop($lists)) { ?>
<ul>
<?php foreach ($items as $item) { ?>
<li><?= $item ?></li>
<?php
}//foreach
}//while ?>
We can still use the more widely-used braces and, at the same time, increase readability.
Using foreach: ... endforeach; does not only make things readable, it also makes least load for memory as introduced in PHP docs
So for big apps, receiving many users this would be the best solution
How about that?
<?php
while($items = array_pop($lists)){
echo "<ul>";
foreach($items as $item){
echo "<li>$item</li>";
}
echo "</ul>";
}
?>