I Have a ordered list and i want to generate a incemental number for the data-slide-to starting from 0
That is the php code
get("display_indicators", 1)): ?>
$item){
$activeclass = "";
if($key == 0){
$activeclass = "active";
}
?>
id;?>" data-slide-to="" class="">
You could use a variable to contain a counter...
<?php $counter = 0; ?>
<li data-target="#carousel<?php echo $module->id;?>" data-slide-to="<?php
echo $counter;
$counter++;
?>" class="<?php echo $activeclass; ?>"></li>
You could even use the following instead of echo-ing and incrementing on two lines...
echo $counter++;
This echoes the current count and increments afterwards, but some people prefer to avoid this.
Try this, hope it'll help you
<?php $uniqueNo=0; ?>
<li data-target="#carousel<?php echo $module->id;?>" data-slide-to="
<?php echo $uniqueNo+=1; ?>" class="<?php echo $activeclass; ?>"></li>
Use an incremential counter
<?php $counter = 0; ?>
<ul>
enter code here
<li data-target="#carousel<?php echo $module->id;?>" data-slide-to="<?=$counter++?>"
class="<?php echo $activeclass; ?>"></li>
this however could not be "unique" application wide
Related
I've been researching if this is possible, but I've drawn a blank, I'm wondering if it's possible to optimize these for and if statements together? cheers.
Edit (Updated #2) : Their is an issue with the code not looping through the pages.
<?php
// calculate total number of pages
$total_pages = ceil($total_rows / $records_per_page);
// range of links to show
$range = 2;
// display links to 'range of pages' around 'current page'
$initial_num = $page - $range;
$condition_limit_num = ($page + $range) + 1;
?>
<ul class="pagination margin-zero">
<?php if ($page>1) : ?>
<li>
<a href='<?php echo $page_url; ?>page=1' title='Go to the first page.'>First Page</a>
</li>
<?php endif; ?>
<?php
for ($x = min($initial_num, 0); $x <= max($condition_limit_num-1, $total_pages); $x++) :
if ($x == $page) :
?>
<li class='active'>
<?php echo $x; ?> <span class="sr-only">(current)</span>
</li>
<?php else : ?>
<li>
<a href='<?php echo $page_url; ?>page=<?php echo $x; ?>'><?php echo $x; ?></a>
</li>
<?php
endif;
endfor;
?>
<?php
if ($page<$total_pages) : ?>
<li>
<a href='<?php echo $page_url; ?>page=<?php echo $total_pages; ?>' title='Last page is <?php echo $total_pages; ?>'>
Last Page
</a>
</li>
<?php endif; ?>
</ul>
Of course you can combine them if you think about the maximum and minimum value that $x is allowed to get assigned.
The outer for loop would require $x to be contained in the interval [0; $condition_limit_num)
If you look only at the for and the first if you could decide that the minimum has to be larger than zero or initial_num, so you could for example use the minimum of initial_num and zero. The limit would work the same way using the maximum of condition_limit_num-1 and total_pages as uppermost reachable value, i.e. the interval [min($x, 0); max($condition_limit_num-1;$total_pages)].
But if you take into account the innermost if you require $x to have a specific value ($page). That means that this if is only "true" whenever $page is contained in the intervall [min($x, 0); max($condition_limit_num-1;$total_pages)] - you can reduce that check to a single if.
Update after question update:
Since the innermost if also has an else path the loop cannot be reduced to a single if:
<?php
for ($x = max($initial_num, 0); $x <= min($condition_limit_num-1, $total_pages); $x++)) :
if($x == $page) :
?>
<li class='active'>
<?php echo $x; ?> <span class="sr-only">(current)</span>
</li>
<?php else : ?>
<li>
<a href='<?php echo $page_url; ?>page=<?php echo $x; ?>'><?php echo $x; ?></a>
</li>
<?php
endif;
endfor;
?>
I use Kirby CMS as backend.
I want following structure for my html output:
<ul>
<li class="link-1">Link</li>
<li class="link-2">Link</li>
<li class="link-3">Link</li>
<li class="link-4">Link</li>
</ul>
I have following code:
<?php foreach($pages->visible() AS $p): ?>
<?php $nbr = $pages->countVisible()?>
<li class="link-<?php for ($i = 1; $i <= $nbr; $i++){echo $i;} ?>">
<a<?php echo ($p->isOpen()) ? ' class="active"' : '' ?> href="<?php echo $p->url() ?>"><?php echo html($p->title()) ?></a></li>
<?php endforeach ?>
But instead I only get the css class
link-1234
in each of the links, so it is making the for loop, but I need only one number per foreach loop.
This code made it work:
<li class="link-<?php static $x=1; echo $x; $x++; ?>">
<li class="link-<?php for ($i = 1; $i <= $nbr; $i++){echo $i;} ?>">
only loops inside that element
<?php for ($i = 1; $i <= $nbr; $i++){
echo "<li class=\"link-$i\">";
echo 'the rest of the line';
} ?>
should loop the whole block
I have some php generated magento top-links with a screenshot below:
My goal is to separate the "Log In" link and have it floated to the left of the same row.
I am hoping for an efficient way to select the last generated list item element and apply some CSS to it.
The code is below:
<ul class="links pull-right"<?php if($this->getName()): ?> id="<?php echo $this->getName() ?>"<?php endif;?>>
<?php foreach($_links as $_link): ?>
<?php if ($_link instanceof Mage_Core_Block_Abstract):?>
<?php echo $_link->toHtml() ?>
<?php else: ?>
<li<?php if($_link->getIsFirst()||$_link->getIsLast()): ?> class="<?php if($_link->getIsFirst()): ?>first<?php endif; ?><?php if($_link->getIsLast()): ?> last<?php endif; ?>"<?php endif; ?> <?php echo $_link->getLiParams() ?>><?php echo $_link->getBeforeText() ?><a href="<?php echo $_link->getUrl() ?>" title="<?php echo $_link->getTitle() ?>" <?php echo $_link->getAParams() ?>><?php echo $_link->getLabel() ?></a><?php echo $_link->getAfterText() ?></li>
<?php endif;?>
<?php if (! $_link->getIsLast()):?>|<?php endif;?>
<?php endforeach; ?>
</ul>
Any ideas would be greatly appreciated!
CSS offers a way to style the last-child of a collection, no tinkering with PHP required.
http://tinker.io/926d2
ul.links.pull-right :last-child {
margin-left: 2em;
}
I answered a similar question not long ago. This adds the class "last-item" to the last item processed.
<?php list($parent) = split('/', $this->url); ?>
<?php $last_articles = $this->find('/news')->children(array('limit'=>5, 'order'=>'page.created_on DESC')); ?>
<ul id="latest-news">
<?php $count = count($last_articles); $num = 0; ?>
<?php foreach ($last_articles as $article): ?>
<li <?php if($num == $count-1){ ?> class="last-item" <?php } ?>>
<?php echo '<h3>'.$article->link($article->title()).'</h3>'; ?>
<?php echo strip_tags(substr($article->content(),0,100)).'...'; ?>
</li>
<?php $num++ ?>
<?php endforeach; ?>
</ul>
Evening All,
Id try and limit the amount of business logic you are adding to your templates. As what you are looking to achieve is custom to this instance of magento I would create a very basic module. I would then look to either implement a new block or just a helper function that will return the data you want.
If your working on the block function ensure your class extends the Magento navigation class. ( Sorry I have not checked what this is ) Then create the action: E.g.
public function getNavigation()
{
$links = $this->getLinks();
$linkArray = array();
$linkCount = count($links);
$i;
foreach($links as $link) {
if($i == $linkCount) {
$last = true;
} else {
$last = false;
}
$linkArray[] = 'link' => $link->getLink()->toHtml(),
'isLast' => $last
$i++;
}
return $linkArray();
}
Your block would then have minimal logic applied. Mainly just iterating through the result set.
Hope that makes sense, If not let me know and I will get you what you require.
I am looking to offset a group of PHP array results to create multiple rows of data.
For example, the first row will contain the first four array results, with the second row displaying results 5-8 and the third row containing results 9-12.
Currently, I am printing out all 12 in one list, however I'd like a bit more display control (i.e. ordering the results into columns which I can style independent of each), hence why I'd like to offset the results.
My current PHP is below:
<?php
if (empty($tmdb_cast['cast'])) {
} else {?>
<section class="cast">
<p class="title">Cast</p>
<ul class="section_body"><?php
foreach($tmdb_cast['cast'] as $key => $castMember){
if ($key < 12) {?>
<li class="actor_instance clearfix"><?php
if ($castMember['profile_path'] != '') {?>
<img src="http://cf2.imgobject.com/t/p/w45<?php echo $castMember['profile_path']; ?>" class="actor_image pull-left" alt="<?php echo $castMember['name']; ?>" title="<?php echo $castMember['name']; ?>" /><?php
} else {?>
<img src="assets/images/castpic_unavailable.png" class="actor_image pull-left" alt="No image available" title="No image available" /><?php
}?>
<p class="actor"><?php echo $castMember['character']; ?> - <?php echo $castMember['name']; ?></p>
</li><?php
}
}?>
<div class="morecast pull-right">[...view all cast]</div>
</ul>
</section><?php
}?>
P.S. Apologies for how I've formatted the code in the above block - I'm still not 100% sure how to make it look "nice" on StackOverflow.
Use array_chunk to break a single dimension array into a 2D array. Then you can loop through each chunk, and then each result, to get that "offset" effect between them.
$chunks = array_chunk($tmdb_cast['cast'], 4); // 4 here, is the number you want each group to have
foreach($chunks as $key => $chunk)
{
foreach($chunk as $castMember)
{
//display castMember here
}
// add code between groups of 4 cast members here
}
First: mixing php and html this way is a very bad habbit... one day you will have to maintain your code and that day you will vomit all over your desk because you mixed different languages in one file...
That being said..
and without creating a new array:
foreach($tmdb_cast['cast'] as $key => $castMember)
{
if ($key < 12)
{
// your code goes here
// if key is 3, or 7 the code below will close the listcontainer and open a new one...
if( ($key+1)%4 == 0 AND $key <11)
echo '</ul> <ul class="section_body">';
}
}
Also replace this:
if (empty($tmdb_cast['cast']))
{
}
else {
with this:
if (!empty($tmdb_cast['cast']))
{
I'm not entirely sure what you're looking for but here's how I would format your code. Alternative syntax for 'if' is a little more readable, and the use of for loops instead of a foreach will give you the control over row numbers you say you're looking for.
<?php if( ! empty($tmdb_cast['cast'])): ?>
<section class="cast">
<p class="title">Cast</p>
<ul class="section_body">
<?php
for($i = 0; $i < 12; $i++):
$castMember = $tmdb_cast['cast'][$i];
?>
<li class="actor_instance clearfix">
<?php if($castMember['profile_path'] != ''): ?>
<img src="http://cf2.imgobject.com/t/p/w45<?php echo $castMember['profile_path']; ?>" class="actor_image pull-left" alt="<?php echo $castMember['name']; ?>" title="<?php echo $castMember['name']; ?>" />
<?php else: ?>
<img src="assets/images/castpic_unavailable.png" class="actor_image pull-left" alt="No image available" title="No image available" />
<?php endif; ?>
<p class="actor"><?php echo $castMember['character']; ?> - <?php echo $castMember['name']; ?></p>
</li>
<?php endfor; ?>
<div class="morecast pull-right">[...view all cast]</div>
</ul>
</section>
<?php endif; ?>
<?php foreach ($rows as $id => $row): ?>
<li class="<?php print $classes[$id]; ?>"><?php print $row; ?></li>
<?php endforeach; ?>
the above code is used to output an article list. now i want to add big space after the lines which is the multiple of 10.namely,add a big space (eg:margin-bottom:30px,only to after every 10 bullet points but the space between other li is 15px )after every 10 bullet points. how to change the above code. then i can use css to get that.
–
Although I don't have a clue what you mean by 'big space', I assume you want to place an arbitry HTML tag after every 10th list item. You can do this like so, for example:
<?php $i = 0; ?>
<?php foreach ($rows as $id => $row): ?>
<li class="<?php print $classes[$id]; ?>"><?php print $row; ?></li>
<?php if ($i++ === 10): ?><br /><?php $i = 0; endif; ?>
<?php endforeach; ?>
EDIT, you clarified:
no,eg:30px, but the space between other <li> is 20px.
Which can be done like so:
<?php $i = 1; ?>
<?php foreach ($rows as $id => $row): ?>
<?php $i++; ?>
<li style="margin-bottom:<?php echo ($i === 10) ? : '30' : '20'); ?>px;" class="<?php print $classes[$id]; ?>"><?php print $row; ?></li>
<?php if ($i === 10) $i = 1; ?>
<?php endforeach; ?>
The modulus operator (%) is ideal for this:
<?php $i = 0; foreach ( $rows as $id => $row ): ?>
<li style="margin-bottom: <?php echo $i ++ % 10 ? '30' : '20' ?>px;" class="<?php echo $classes[$id] ?>">
<?php echo $row ?>
</li>
<?php endforeach ?>
As a slight amendment to Aron's code, if you wanted to use CSS to style the <li>:
<?php $i = 1;
foreach ($rows as $id => $row):
if($i === 10){
$end_class = ' end-class';
$i = 1;
} else {
$end_class = '';
$i++;
}
echo '<li class="' . $classes[$id] . $end_class . '">' . $row . '</li>';
endforeach;
?>
You can then apply the style to .end-class.