i want to display ads in a foreach loop in codeigniter - php

i have some items i want displayed in the view, my per page is set to 15, so each page displays 15 items, but i want to display ads in the middle of the loop, after maybe 4 items displayed, I've tried to put an if statement in the foreach loop to control the display, it displays the first 3 values, but when i put the div for the ad, it loops too, can someone please tell me what to do, or point me in the right direction?? thanks, here is my code so far:
<?php
$counter1 = 0;
//the foreach loop that retrieves the values from the controller
foreach($records as $record){
//an if statement to display the first 4 items..
if ($counter1 <= 3){
?>
<div class='box-scene'>
<div class='dbox'>
<div class='front face'>
<img src="images/newtag.png">
</div>
<a style="font-size:15px;" href="<?php echo base_url();?>music/<?php echo $record->url; ?>">
<div class="side face">
<span>
<?php echo $record->name; ?>
</span>
</div>
</a>
</div>
</div>
<?php
$counter1++;
}
?>
<div style="width:200px; height:200px; float:left; display:inline-block; margin: 0 12.5px 20px 12.5px;">
<div id="ad_200_200">
</div>
</div>
<!-- this div displays more than once, i dont know where to place it
for it to display after the first 3 items -->
<?php
}
?>
i want to know where to place the div, and how to continue displaying the items...thanks

Hi please use else condition.please check replace with below code
<?php
$counter1 = 0;
//the foreach loop that retrieves the values from the controller
foreach ($records as $record) {
//an if statement to display the first 4 items..
?>
<?php if($counter1 % 4 == 0) { ?>
<div style="width:200px; height:200px; float:left; display:inline-block; margin: 0 12.5px 20px 12.5px;">
<div id="ad_200_200">
</div>
</div>
<?php } ?>
<div class='box-scene'>
<div class='dbox'>
<div class='front face'>
<img src="images/newtag.png">
</div>
<a style="font-size:15px;" href="<?php echo base_url(); ?>music/<?php echo $record->url; ?>">
<div class="side face">
<span>
<?php echo $record->name; ?>
</span>
</div>
</a>
</div>
</div>
<!-- this div displays more than once, i dont know where to place it
for it to display after the first 3 items -->
<?php
$counter1++;
}
?>

use also an else statement like below:
<?php
$counter1 = 0;
foreach($records as $record){
if ($counter1 <= 3){ ?>
<div class='box-scene'>
<div class='dbox'>
<div class='front face'>
<img src="images/newtag.png">
</div>
<a style="font-size:15px;" href="<?php echo base_url();?>music/<?php echo $record->url; ?>">
<div class="side face">
<span>
<?php echo $record->name; ?>
</span>
</div>
</a>
</div>
<div>
<?php
$counter1++;
} else{ ?>
<div style="width:200px;height:200px;float:left;display:inline-block; margin: 0 12.5px 20px 12.5px;">
<div id="ad_200_200"></div>
</div>
<?php
$counter1 = 0;
} ?>
<?php
}
?>

Related

PHP Bootstrap row loop every 3 posts

Trying to display the custom posts on my archive page within a bootstrap row containing 3 columns then starting a new row, got the code but new to PHP and dont know where to put the content.
<?php
//Columns must be a factor of 12 (1,2,3,4,6,12)
$numOfCols = 3;
$rowCount = 0;
$bootstrapColWidth = 12 / $numOfCols;
?>
<div class="row">
<?php
foreach ($rows as $row){
?>
<div class="col-md-4"<?php echo $bootstrapColWidth; ?>">
<div class="thumbnail">
<img src="user_file/<?php echo $row->foto; ?>">
</div>
</div>
<?php
$rowCount++;
if($rowCount % $numOfCols == 0) echo '</div><div class="row">';
}
?>
</div>
<div class="embed-container">
<?php the_field('podcast_embed_link'); ?>
</div>
<h3><?php the_title(); ?></h3>
<p><b><?php echo $date->format( $format_out );?></b></p>
<p><?php the_field('description'); ?></p>
<?php if( get_field('thumbnail') ): ?>
<img src="<?php the_field('thumbnail'); ?>" />
<?php endif; ?>
<?php endwhile; // end of the loop. ?>
</div>
</div>
</div><!-- #content -->
Here is the code for the page archive.podcasts.php, where would i add the custom fields within the row loop?
First of all, you don't need to close and open row tag each 3 items. If you leave the code like this:
<div class="row">
<?php
foreach ($rows as $row){
?>
<div class="col-md-<?php echo $bootstrapColWidth; ?>">
<div class="thumbnail">
<img src="user_file/<?php echo $row->foto; ?>">
</div>
</div>
<?php
}
?>
</div>
you will get the same effect, but without the separation that a row tag involves. Notice that the line involving "col-md-4" has already changes in order to not create wrong col size usage.
In this part of code:
<div class="col-md-4"<?php echo $bootstrapColWidth; ?>">
You must get wrong bootstrap class like col-md-41, col-md-412.
Correct you code by this way:
<div class="col-md-<?php echo $bootstrapColWidth; ?>">

Every/other CSS techniques

I am creating a page with 4 different sections. In each section there is a responsive image and on the image there is text and some buttons. Right now they are aligned a bit off center and to the right. I am trying to achieve so that the 1st div the text and buttons float right and the 2nd div the text and buttons float left and so on. I am using the even odd nth property but it is only responding on the even css nth property. Here is the CSS and html. Keep in mind I am just using the background color now as an example and will put in the floats and padding if I get it working.
.popup-open:nth-child(odd) {
background: #ff0000;
}
.popup-open:nth-child(even) {
background: #000000;
}
<div class="popup-open">
<div class="icons-large">
<?php
$fields = CFS()->get('icons_large');
if($fields) :
foreach ($fields as $field) : ?>
<div class="row">
<div class="image">
<img src="<?php echo $field['icons_big'];?>" alt="">
</div>
</div>
<?php endforeach; endif; ?>
</div>
<div class="icons-large">
<?php
$fields = CFS()->get('tour_titles');
if($fields) :
foreach ($fields as $field) : ?>
<?php echo $field['tour_name'];?>
<?php endforeach; endif; ?>
</div>
<div class="title"><?php the_title(); ?></div>
</div>
I think you should add a class to the divs and target them that way, you will have more control over it too.
if($fields) :
$i = 1;
foreach ($fields as $field) :
if($i === 2){
$class = "even";
$i = 1;
}else if($i === 1){
$class = "odd";
$i = 2;
}
<div class="row <?=$class;?>">
<div class="image">
<img src="<?php echo $field['icons_big'];?>" alt="">
</div>
</div>
<?php endforeach; endif; ?>

php code to display rows in two different display

I create two design to display news .
first DIV Display BIGGER images and Separate css class and display only two rows.
Second DIV Display Smaller images and Separate css class and display five rows..
I select rows in asc order from query.
my query is = select * from news order by priority asc.
My Only Need is TO Display two rows in First div and then display another rows in Second DIV.
IS This is possible using if else or any other condition...how to do this.........
but keep all div tag as it is bcoz of page design purpose......
<!-- FIRST DIV -->
<div class="post">
<div class="buffer">
<?php foreach($top2 as $top){ ?>
<div class="content"> <img src="bhaskarcms/uploads/<?php echo $top['photo']; ?>" style="width:285px; height:100px;" />
<h2><?php echo htmlspecialchars_decode($top['headline']); ?></h2>
</div>
<?php } ?>
<!--<p class="details2">8 Comments / Read More</p>-->
</div>
</div>
<!-- end post -->
<!-- SECOND DIV -->
<!-- begin post -->
<div class="post">
<div class="buffer">
<?php foreach($top2 as $top){ ?>
<div class="content1"><img src="bhaskarcms/uploads/<?php echo $top['photo']; ?>" />
<h2><?php echo htmlspecialchars_decode($top['headline']); ?></h2>
</div>
<?php } ?>
</div>
</div>
<!-- end post -->
Yes, it's possible. Assuming your $top2 array has standard numerical/sequential keys, then:
$split = 2;
for($i = 0; $i < $split; $i++) {
... print $top2[0] and $top2[1]
}
for ($i = $split; $i < count($top2); $i++) {
... print the rest of the rows
}
Can use array_slice()
<!-- FIRST DIV -->
<div class="post">
<div class="buffer">
<?php $arr = array_slice($top2, 0, 2) ?>
<?php foreach($arr as $top){ ?>
<div class="content"> <img src="bhaskarcms/uploads/<?php echo $top['photo']; ?>" style="width:285px; height:100px;" />
<h2><?php echo htmlspecialchars_decode($top['headline']); ?></h2>
</div>
<?php } ?>
<!--<p class="details2">8 Comments / Read More </p>-->
</div>
</div>
<!-- end post -->
<!-- SECOND DIV -->
<!-- begin post -->
<div class="post">
<div class="buffer">
<?php $arr = array_slice($top2, 2) ?>
<?php foreach($arr as $top){ ?>
<div class="content1"><img src="bhaskarcms/uploads/<?php echo $top['photo']; ?>" />
<h2><?php echo htmlspecialchars_decode($top['headline']); ?></h2>
</div>
<?php } ?>
</div>
Add a count that you increment in the foreach loop, then break when its over two:
<!-- FIRST DIV -->
<div class="post">
<div class="buffer">
<?php
$rowCount = 0;
foreach($top2 as $top){ ?>
<div class="content"> <img src="bhaskarcms/uploads/<?php echo $top['photo']; ?>" style="width:285px; height:100px;" />
<h2><?php echo htmlspecialchars_decode($top['headline']); ?></h2>
</div>
<?php
$rowCount++;
if($rowCount > 2){
break;
}
} ?>
<!--<p class="details2">8 Comments / Read More</p>-->
</div>
</div>
<!-- end post -->

How to display elements of array, grouped by two in one <div>

I have an array of elements. I need to display all the elements on the page, grouped by two in one 'div'
like this:
<div class='row'>
<article id='1'>item 1 row 1</article>
<article id='2'>item 2 row 1</article>
</div>
<div class='row'>
<article id='3'>item 1 row 2</article>
<article id='4'>item 2 row 2</article>
</div>
This my current php and html code:
<?php if ($arg['teasers']) foreach ($arg['teasers'] as $id => $article)
{ ?>
<div class="shops_<?php echo ($article['show'])?'fair':'draft'; ?> more">
<div class="shops_title">
<?php echo $article['title']; ?>
</div>
<div class="shops_teaser" onclick="window.location='<?php echo $arg['linkbase'].'/?id='.$id ?>'">
<?php echo $article['teaser']; ?>
</div>
<?php show_button_detail(array('uri'=>$arg['linkbase'].'/?id='.$id)); ?>
</div>
<?php
} ?>
Perfect example: http://tympanus.net/codrops/category/tutorials/
Maybe you could try something like this:
/* div.more */
div.row {
display: table-row;
width: 100%; /* your parent element should specify a width for this to work */
}
/* div.more > div[class^="shops_"] */
div.row > article {
display: table-cell;
width: 50%;
}

Add new row after 4th album PHP

so I am trying to add a new row after every 4th gallery and continue on until I am out of galleries to add. So if there is 17 galleries there will be 4 rows of 4 galleries and 1 row of the remaining gallery. here is an example of how it looks: http://www.csulb.edu/centers/latinohealth/media/galleries/
here is my code:
<?php $this->start_element('nextgen_gallery.gallery_container', 'container', $displayed_gallery); ?>
<div class="row-fluid secondone">
<div class="ngg-albumoverview span12">
<div class="row-fluid">
<?php $count = 0;?>
<?php foreach ($galleries as $gallery) {
$count++;
?>
<div class="ngg-album span3">
<div class="ngg-albumtitle">
<?php echo_safe_html($gallery->title); ?>
</div>
<div class="ngg-albumcontent">
<div class="ngg-thumbnail">
<a class="gallery_link" href="<?php echo nextgen_esc_url($gallery->pagelink); ?>"><img class="Thumb" alt="<?php echo esc_attr($gallery->title); ?>" src="<?php echo nextgen_esc_url($gallery->previewurl); ?>"/></a>
</div>
<div class="ngg-description">
<p><?php echo_safe_html($gallery->galdesc); ?></p>
<?php if (isset($gallery->counter) && $gallery->counter > 0) { ?>
<p><strong><?php echo $gallery->counter; ?></strong> <?php _e('Photos', 'nggallery'); ?></p>
<?php } ?>
</div>
</div>
</div>
<?php if ($count % 4 == 0 ) ?>
</div>
<div class="row-fluid">
<?php } ?>
</div>
</div>
</div>
<?php $this->end_element(); ?>
Found the problem:
the row:
<?php if ($count % 4 == 0 ) ?>
should be:
<?php if ($count % 4 == 0 ) { ?>
You need to do what you want with css styles, not with the php.
Create a container block with fixed width that can contain exacly 4 galleries and use the float property on the boxes of the galleries.

Categories