Inserting a block after a specified number of loop iterations - php

I am trying to fetch some info from database under ZF1 environment. I am using a for loop here. What I am trying to accomplish is that inserting a new css division (div) for each four iteration. In other words, I would like to insert a block followed by four loop iterations. For example, if I run a loop twelve times, there should be three new blocks after each four iterations. It is basically an advertisement block that may contain image and link. I need an idea.
Here is an example block.
<div class="flexblock-image">
<a href="https://offers.example.com/offer/600112?cid=BWO_mh.com_html_getbackinshape&keycode=254194">
<img alt="" title="" typeof="foaf:Image" src="http://www.example.com.gif">
</a>
</div>
Here is the loop I have coded in my app.
<?php if ($this->numArticles > 0) : ?>
<?php for ($i = 0; $i < 13; $i++) : ?>
<div class="channel-image ">
<div class="img">
<h2 class="primary-tag">
<a href="/tags/weight-loss-tips" typeof="skos:Concept" property="rdfs:label skos:prefLabel" datatype="">
<?php echo $this->articles[$i]->slug ; ?>
</a>
</h2>
<a href="
<?php echo $this->articles[$i]->getViewUrl(); ?>">
<img typeof="foaf:Image" src="
<?php echo $this->articles[$i]->getCover('crop'); ?>" width="650" height="412" alt="
<?php echo addslashes($article->title); ?>" title="
<?php echo addslashes($article->title); ?>">
</a>
</div>
<div class="channel-content">
<p class="date">
<span property="dc:date dc:created" content="2015-06-04T11:06:18-04:00" datatype="xsd:dateTime">
<?php echo $this->dateFormatter()->diff(strtotime($this->articles[0]->activated_date), $this->timeDiffFormats); ?>
</span>
</p>
<h2 class="article-title" id="title-node-93726">
<a href="
<?php echo $this->articles[$i]->getViewUrl(); ?>">
<?php echo addslashes($this->articles[$i]->title); ?>
</a>
</h2>
<div class="byline-container">
<span class="byline-role">By </span>
<span class="field-author">
<a href="#" typeof="skos:Concept" property="rdfs:label skos:prefLabel" datatype="">
<?php echo sprintf($this->translator()->_('show.by'), $this->articles[0]->getAuthor()); ?>
</a>
</span>
</div>
</div>
</div>
<?php endfor; ?>
<?php endif; ?>

Not an expert on PHP, but wouldn't an if statement inside of your for loop work by using the modulus operator?
<?php for ($i = 0; $i < 13; $i++) : ?>
<?php if ($i % 4 === 0) : ?>
// Custom HTML/CSS here
<?php endif; ?>
<?php endfor; ?>
More on the modulus operator can be found here

Related

Append html element in php like jQuery append

I know jQuery append element, but I want to know is there any way so that I can append 50 similar elements using php with some loop so something else, The elements I want to append 50 times are given here,
<div class='test'>
<span class='<?php echo $result[0]->cc1;?>'><?php echo $result[0]->dd1;?></span>
</div>
<div class='test'>
<span class='<?php echo $result[0]->cc2;?>'><?php echo $result[0]->dd2;?></span>
</div>
<div class='test'>
<span class='<?php echo $result[0]->cc3;?>'><?php echo $result[0]->dd3;?></span>
</div>
<div class='test'>
<span class='<?php echo $result[0]->cc4;?>'><?php echo $result[0]->dd4;?></span>
</div>
// and so on.. up to 50.
Tried this but not working,
<?php for ($i=0; $i <50; $i++) : ?> <span class='<?php echo $result[0]->cc'+i+';?>'><?php echo $result[0]->dd'+i+';?></span> <?php endfor; ?>
Create a loop
Then using the $result[0]->{'cc'.$i} syntax address all your properties
for( $i=1; $i<51; $i++) :
?>
<div class='test'>
<span class='<?php echo $result[0]->{'cc'.$i};?>'><?php echo $result[0]->{'dd'.$i};?></span>
</div>
<?php
endfor;

Display nav list of content is available with php

I've got this PHP code:
<div class="connect_wrap <?php echo $this->class; ?> block" <?php echo $this->cssID; ?>>
<?php if(!$this->empty): ?>
<?php foreach($this->entries as $entry): ?>
<div class="entry block <?php echo $entry->class; ?>">
<div class="tab">
<ul class="tabs">
<li class="tab-link current" data-tab="Kunden">Kunden</li>
<li class="tab-link" data-tab="Loesungen">Lösungen</li>
</ul>
<?php
$this->import('Database');
$pName = $entry->field('name')->value();
$result = \Database::getInstance()->prepare("SELECT * FROM kunden WHERE partner=?")->execute($pName);
?>
<?php if($result->numRows):?>
<div id="Kunden" class="tab-content current">
<?php while($result->next()) { ?>
<div class="items">
<a href="{{env::url}}/kunden-detail/<?php echo $result->alias; ?>">
<div class="logo">
<img src="<?php $objFile = \FilesModel::findByUuid($result->logo); echo $objFile->path;?>"width="180" height="135">
</div>
</a>
</div>
<?php } ?>
</div>
<?php endif;?>
<?php
$this->import('Database');
$pName = $entry->field('name')->value();
$result = \Database::getInstance()->prepare("SELECT * FROM solutions WHERE solution_provider=?")->execute($pName);
?>
<?php if($result->numRows):?>
<div id="Loesungen" class="tab-content">
<?php while($result->next()) { ?>
<div class="items">
<a href="{{env::url}}/synaptic-commerce-solution/<?php echo $result->alias; ?>">
<div class="logo">
<img src="<?php $objFile = \FilesModel::findByUuid($result->logo); echo $objFile->path;?>"width="180" height="135">
</div>
</a>
</div>
<?php } ?>
</div>
<?php endif;?>
</div>
</div>
<?php endforeach; ?>
<?php else: ?>
<?php endif;?>
In that code, I've got two DB queries. My problem is, if there is no data for both queries, the div with the class "connect_wrap" should not be displayed.
How can I do that?
Thanks in advance.
do you want to check if the query has data in it and its not empty ? if so try num_rows it will return the number of rows that the query found/affected for example to check if th query returned one or more rows
if($result->num_rows >= 1) {
//do stuf
} else {
// no result found
}
Move the query execution up or preferably: not within the html but in a different file/class. Then add a check before the content_wrap div: if($kundenResult->num_rows >= 1 || $solutionsResult->num_rows >= 1). If you just keep the individual checks like you already have, it will only show the content_wrap div if either or both of the queries return rows of data.

opencart make table of categories insted of carousel

I am just trying to remake manufacturers carousel so that it will show categories with their images. Part of it I have done. Now i jast want to show them in table 4 in a row. Here is the code that is file catalog/view/module/carousel.tpl/
<div id="carousel<?php echo $module; ?>" class="owl-carousel">
<div class="category">
<table>
<?php if($categories) { ?>
<?php $count = 0;?>
<?php foreach ($categories as $category) { ?>
<?php if($count % 4 == 1){?>
<tr>
<?php}?>
<td>
<a href="<?php echo $category['href']; ?>" <?php echo $category['name']; ?></a>
<img src="<?php echo $category['image'] ?>" alt="<?php echo $category['name']; ?>" title="<?php echo $category['name']; ?>" class="img-thumbnail" />
<p><?php echo $category['name']?>
</td>
<?php if($count % 4 == 0){?>
</tr>
<?php}?>
<?php } ?>
<?php } ?>
</table>
</div>
</div>
<script type="text/javascript"><!--
$('#carousel<?php echo $module; ?>').owlCarousel({
items: 15,
autoPlay: 1000000,
navigation: false,
navigationText: ['<i class="fa fa-chevron-left fa-5x"></i>', '<i class="fa fa-chevron-right fa-5x"></i>'],
pagination: false
});
--></script>
As a result of it I am getting error unexpected end of file. What am I doing wrong? Are their better solutions? I am using opencart version 2.0.1.1
You need to make some minor changes and this will work:
<?php if($count % 4 == 0){?>
</tr>
<?php } ?> //here you need to change
$count++;
Give the space after <?php tag. due to this the error occurs. There is require a space after this tag.
And you are increasing the value of $count.

Foreach 4 items use a div, and then every other 4 items repeat another div

I am trying to create an image carousel, but that requires 2 seperate divs for the scroller, one active one, and then another which is not active but can be scrolled to...
This is what I'm trying to acomplish:
//the first 4 of a foreach
<div class="item active">
<img src="<?php $images[$i]; ?>">
<img src="<?php $images[$i]; ?>">
<img src="<?php $images[$i]; ?>">
<img src="<?php $images[$i]; ?>">
</div>
//the next 4 of a foreach
<div class="item">
<img src="<?php $images[$i]; ?>">
<img src="<?php $images[$i]; ?>">
<img src="<?php $images[$i]; ?>">
<img src="<?php $images[$i]; ?>">
</div><!-- /item -->
//now after every 4 repeat the <div class="item"> with the images inside, </div>
This is my foreach
<?php $i = 1; foreach(explode(",",$this->product->images) as $images): ?>
On each 4 I don't want the 4 divs to be repeated on the img inside the div.
I'm not sure how I will accomplish this?
Thanks to the posters. but now it's showing 5 instead of 4?
<div class="item active">
<?php $i=1 ; foreach(explode( ",",$this->product->images) as $images): ?>
<div data-target="#carousel" data-slide-to="<?php echo $i; ?>" class="thumb">
<img src="<?php echo Config::get('URL') .'images/products/'.System::escape($this->product->username). '/item' .System::escape($this->product->id) .'/'. System::escape($images); ?>"></div>
<?php if ($i % 4==0 ) {
echo '</div><div class="item">';
echo '<div data-target="#carousel" data-slide-to="'.$i. '" class="thumb"><img src="'.Config::get( 'URL') . 'images/products/'.System::escape($this->product->username). '/item' .System::escape($this->product->id) .'/'. System::escape($images).'"></div>';
} $i+=1; endforeach; ?>
</div>
<!-- /item -->
You can use the modulo operation to check, if your counter variable is the multiple of 4:
if ($i % 4 == 0) {
echo '</div><div>';
}
You just need to make sure that your foreach actually increases the value of $i on every iteration.
Alternatively you could reset $i back to 0 inside of the if body, so it always counts only to 4 and never higher. But I prefer the modulo solution, because that way you have an actual counter variable, not a fake one. :)
You can use Simple approach , add at start and also closing at the end like
<?php
$i=1;
foreach($arrayItems as $single){
if($i%4 == 0){
?>
<div class="Special">
<?php
}
?>
<div class="regular">Regular content</div>
<?php
if($i%4 == 0){
?>
</div>
<?php
}
$i+=1;
}
?>

PHP while loop, display in pairs of two

In Wordpress, I am creating a slider to slide through some content that the user specify. I am using ACF (advanced custom fields) if anyone is familiar with that, however what I am trying to accomplish is to show two content items at a time, while sliding through the jQuery slider.
Here is my loop:
<?php while(has_sub_field('popular_topic')): ?>
<li>
<div class="slide">
<img src="<?php the_sub_field('popular_topic_image'); ?>" alt="" />
<div class="img-wrapper"></div>
<div class="slider-content">
<?php
$len = 60;
$popularTopicTitle = get_sub_field('popular_topic_title');
$newContent = substr($popularTopicTitle, 0, $len);
if(strlen($newContent) < strlen($popularTopicTitle)) {
$newContent = $newContent.'...';
}
echo '<p>'.$newContent.'</p>';
?>
<a class="more-arrow" href="<?php the_sub_field('popular_topic_link'); ?>">Read More</a>
</div>
</div>
</li>
<?php endwhile; ?>
This is currently working, however it only shows one slide. I want it to show two slides and a time. Is there something I can do with a counter? Does this make sense?
my syntax might be a little off but maybe give this a shot?
<?php $i = 0; ?>
<?php while(has_sub_field('popular_topic')): ?>
<?php
$len = 60;
$popularTopicTitle = get_sub_field('popular_topic_title');
$newContent = substr($popularTopicTitle, 0, $len);
if(strlen($newContent) < strlen($popularTopicTitle)) {
$newContent = $newContent.'...';
}
$contentVar[$i] = array (
'img' => the_sub_field('popular_topic_image'),
'title' => $newContent,
'link' => the_sub_field('popular_topic_link')
);
$i++;
?>
<?php endwhile; ?>
<?php $j = 0; ?>
<?php while ($j < $i) : ?>
<li>
<div class="slide">
<img src="<?php echo $contentVar[$j]['img']; ?>" alt="" />
<div class="img-wrapper"></div>
<div class="slider-content">
<p><?php echo $contentVar[$j]['title']; ?></p>
<a class="more-arrow" href="<?php echo $contentVar[$j]['link']; ?>">Read More</a>
</div>
<?php $j++; ?>
<?php if ($j <= $i) : ?>
<img src="<?php echo $contentVar[$j]['img']; ?>" alt="" />
<div class="img-wrapper"></div>
<div class="slider-content">
<p><?php echo $contentVar[$j]['title']; ?></p>
<a class="more-arrow" href="<?php echo $contentVar[$j]['link']; ?>">Read More</a>
</div>
<?php endif; ?>
</div>
</li>
<?php $j++; ?>
<?php endwhile; ?>

Categories