Append html element in php like jQuery append - php

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;

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; ?>">

Pagination in php using div and bootstrap

I'm trying to implement a simple pagination using PHP and Twitter Bootstrap but got stuck.
Everything seems to work fine the only problem is my div's are floating all around leaving some empty spaces in the page like in this picture:
Below is my code:
<?php foreach($tester as $data): ?>
<div class="col-sm-2">
<img src="" width="150" height="150"/>
<br>
<span><?php echo $data['Description'];?></span><br>
<span><?php echo $data['Unit_Price'];?></span><br>
<span>In Stock:<?php echo $data['Qty'];?></span><br>
<span>Arriving Soon:<?php echo $data['In_Transit'];?></span><br>
<span>Show All Products In:<?php echo $data['P_Class_Name'];?></span>
</div>
<?php endforeach;?>
Put class="clearfix" div after each 6 elements to create non float sensitive line break
<?php $i=0; foreach($tester as $data): $i++; ?>
<div class="col-sm-2">
<img src="" width="150" height="150"/>
<br>
<span><?php echo $data['Description'];?></span><br>
<span><?php echo $data['Unit_Price'];?></span><br>
<span>In Stock:<?php echo $data['Qty'];?></span><br>
<span>Arriving Soon:<?php echo $data['In_Transit'];?></span><br>
<span>Show All Products In:<?php echo $data['P_Class_Name'];?></span>
</div>
<?php if ($i % 6 == 0) { ?><div class="clearfix"><?php } ?>
<?php endforeach;?>
However, this solution shouldn't be used if you need a different number of elements to displayed per row for a different viewpoint size.

For loop to repeat and increment day number in code

I need loop some code trough 24 times to create a 24 day Christmas calendar. I have the for loop below but how do i add the '$i' to the code to make sure the wherever there is a day number it changes from 1 through to 24 each time? What would the code look like?
<?php for ($i = 1; $i < 25; $i++) { ?>
<!-- DAY 1 -->
<a href="offer.php?day=1<? echo '&dealership='. $dealership; ?>" class="item <? if ($today[mday] == 1) { echo "current yellow"; } else if ($today[mday] < 2 ) { echo "disabled"; }?>">
<div class="offer">
<h2>Day 1</h2>
<p><? echo Day_1_Offer('CAL_OFFER'); ?></p>
<? echo $termsLink; ?>
</div>
<div class="offer-img">
<img src="img/day1.jpg">
</div>
</a>
<?php } ?>
Just add <?php echo $i; ?> whenever you need the number.
<!-- DAY 1 -->
<a href="offer.php?day=<?php echo $i; ?><? echo '&dealership='. $dealership; ?>" class="item <? if ($today[mday] == 1) { echo "current yellow"; } else if ($today[mday] < 2 ) { echo "disabled"; }?>">
<div class="offer">
<h2>Day <?php echo $i; ?></h2>
<p><? echo call_user_func('Day_'.$i.'_Offer', 'CAL_OFFER'); ?></p>
<? echo $termsLink; ?>
</div>
<div class="offer-img">
<img src="img/day<?php echo $i; ?>.jpg">
</div>
</a>
<?php } ?>
To modify the Day_1_Offer call you must do a things a bit differently, as it's a function and in the PHP and not part of the HTML. To do this, you need call_user_func(). Try this:
<p><? echo call_user_func('Day_'.$i.'_Offer', 'CAL_OFFER'); ?></p>

Inserting a block after a specified number of loop iterations

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

How to echo CSS class only once in first iteration foreach in PHP?

I'm facing a little problem, I have a css class which is called "negative margin" which I just want to apply once in the first div in a series of divs as it's part of my design. The code is as follows:
<?php foreach($records as $rec){ ?>
<div class="grid grid-lightgrey grid-pad negative-margin">
<div class="col-1-1">
<div class="module module-grey">
<em>Project title: <?php echo $rec->project_title; ?></em><br>
<em>Category: </em><?php echo $rec->project_category; ?><br>
<em>Description: </em><?php echo $rec->project_description; ?>
<br>
<em>This project was started in: <?php echo $rec->project_year; ?></em><br>
<em>This project ID is: </em>
<?php echo $rec->project_id; ?>
</div>
</div>
</div>
<?php } ?>
The class negative-margin is applied to all the divs making them stack on top of each other and it's not what I wan't but I need this negative margin to be applied in the first div as I said for design purposes but I really don't know how this could be achieved. Any ideas would be greatly appreciated.
Use the first-child selector to define a CSS style that only applies to the first div.
div.grid:first-child {
margin-left: -10px;
}
You need a variable to track the first element because there is no other way to know the index in foreach loop. You can do:
<?php $i = 0; foreach($records as $rec): ?>
<div class="grid grid-lightgrey grid-pad <?php echo ($i == 0) ? 'negative-margin' : ''; ?>">
<div class="col-1-1">
<div class="module module-grey">
<em>Project title: <?php echo $rec->project_title; ?></em><br>
<em>Category: </em><?php echo $rec->project_category; ?><br>
<em>Description: </em><?php echo $rec->project_description; ?>
<br>
<em>This project was started in: <?php echo $rec->project_year; ?></em><br>
<em>This project ID is: </em>
<?php echo $rec->project_id; ?>
</div>
</div>
</div>
<?php $i++; endforeach; ?>

Categories