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>
Related
In my app I have a table in where I'd like lo highlight some rows if a date field is empty, here's my code:
<table>
<?php foreach ($conts as $cont): ?>
<tr <?php if ($cont['Cont']['shdate']!==NULL) echo 'class="bg-success"'; ?>>
</tr>
<?php endforeach; ?>
</table>
It does not work although I've checked in the database and the field is NULL, why could this be happening?
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
I have created a product attribute called product_downloads, I can put a file name there and it appears in the product view page in the tab "Additional Information" with a link to the file name. I have successfully implemented this idea for one download, but for some products I will have multiple downloads...
Like I said, I have the code working with one download per product, all that took was a small amount of code inside of app/design/frontend/base/default/template/catalog/product/view/attributes.phtml
Where the original code was:
<?php foreach ($_additional as $_data): ?>
<tr>
<th class="label"><?php echo $this->escapeHtml($this->__($_data['label'])) ?></th>
<td class="data"><?php echo $_helper->productAttribute($_product, $_data['value'], $_data['code']) ?></td>
</tr>
<?php endforeach; ?>
this is the code I have replaced that with:
<?php foreach ($_additional as $_data): ?>
<?php if ((string)$_data['value'] != '' and $_data['value'] != 'N/A'): ?> <!-- IF NOTHING DO NOTHING-->
<tr>
<th class="label"><?php echo $this->htmlEscape($this->__($_data['label'])) ?></th>
<?php if ($_data['code'] == 'product_download'): ?>
<td>
Product PDF
</td>
<?php else: ?>
<?php echo $_helper->productAttribute($_product, $_data['value'], $_data['code']) ?>
<?php endif; ?>
</tr>
<?php endif; ?>
<?php endforeach; ?>
This works perfectly, however... What I need now is to look at the value of product_download, if it has a comma(or some other divider, if this would interfere with a csv format) make two separate links to two seperate files..
One major thing to note... I rely heavily on MAGMI, which is why I have taken this route as no extension allows me to massively import attachments to products.
This is for spec sheets and such... some products have multiple spec sheets. I want to use a single attribute to store multiple file names and have code that will differentiate between the two.
you can use logic with explode function And change your if condition with my custom code as below
<?php if ($_data['code'] == 'product_download'): ?>
<?php
$product_downloads = explode(",",$_product->getProductDownload());
foreach($product_downloads as $download):
?>
<td>
Product PDF
</td>
<?php endforeach; ?>
<?php else: ?>
<?php echo $_helper->productAttribute($_product, $_data['value'], $_data['code']) ?>
<?php endif; ?>
this is just logic you can use as per your thoughts as well.
hope this will sure help you.
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 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>";
}
?>