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>
Related
I have a problem looping my html tags. My goal is to enclose every 4 div with class "item" inside the class of "item-wrap". So far here's my code:
$speakers will return 8 rows so "item-wrap" class will be display twice with 4 "item" class inside
<?php
$speaker_ctr = 0;
if(count($speakers) > 0){
?>
<div class="item-wrap">
<?php foreach($speakers as $speaker){ ?>
<div class="item"> <img class="lazy" data-lazy-src="<?php echo $speaker['imagepath']; ?>" />
<h5 class="txt-18 bold-font text-uppercase no-mbt mt-20"><?php echo $speaker['name']; ?></h3>
<p class="no-mn"><?php echo $speaker['position']; ?></p>
<?php echo $speaker['company']; ?>
</div>
<?php } ?>
</div>
<?php
$speaker_ctr++;
}
?>
Try below code:-
<?php
if(count($speakers) > 0){
for($i=0;$i<=count($speakers);$i++){
if($i%4==0){
echo '<div class="item-wrap">';
}
?>
<div class="item"> <img class="lazy" data-lazy-src="<?php echo $speakers[$i]['imagepath']; ?>" />
<h5 class="txt-18 bold-font text-uppercase no-mbt mt-20"><?php echo $speakers[$i]['name']; ?></h3>
<p class="no-mn"><?php echo $speakers[$i]['position']; ?></p>
<?php echo $speakers[$i]['company']; ?>
</div>
<?php
if($i%4==0){
echo '</div>';
}
}
}
Try with -
$speaker_ctr = 0;
if(count($speakers) > 0){
?>
<div class="item-wrap">
<?php
foreach($speakers as $speaker){
$speaker_ctr++; // increment
?>
<div class="item"> <img class="lazy" data-lazy-src="<?php echo $speaker['imagepath']; ?>" />
<h5 class="txt-18 bold-font text-uppercase no-mbt mt-20"><?php echo $speaker['name']; ?></h3>
<p class="no-mn"><?php echo $speaker['position']; ?></p>
<?php echo $speaker['company']; ?>
</div>
<?php
// print if divisible by 4
// every 4th element
if($speaker_ctr%4 == 0 && $speaker_ctr != count($speakers)) {
echo "</div><div class='item-wrap'>";
}
}
?>
</div>
<?php
}
I've got a while loop that displays 50 logos. But what I need is another loop that creates a new div(.autogrid_wrapper .cte .block) every 5 images.
<?php
$cn = 1;
while($result->next()) {
if ($cn % 5 == 0) {
?>
<div class="autogrid_wrapper cte block">
<div class="inner">
<?php } ?>
<div class="ce_card autogrid-type_cte n5 one_fifth autogrid_mode_auto autogrid <?php echo $class2; ?> <?php echo $class; ?> block">
<div class="card_wrapper">
<a class="download_image" title="<?php echo $result->name; ?>"
<div class="ce_image attribute image">
<div class="ce_image block">
<figure class="image_container">
<img src="<?php echo $imageVar->path; ?>" onerror="this.onerror=null; this.src='files/Intershop/media/images/customers/<?php echo $rest; ?>.png'" title="<?php echo $entry->field('name')->value(); ?>" alt="<?php echo $entry->field('name')->value(); ?>" >
</figure>
</div>
</div>
</a>
</div>
</div>
<div class="clear autogrid_clear"></div>
<?php if ($cn % 5 == 0) { ?>
</div>
</div>
<?php
}
$cn++;
?>
<?php } ?>
I hope you guys can help me.
Ok , after ask you in comments I know what's your purpose.
You wanna print div(.autogrid_wrapper .cte .block) before 1st item and close this div after while walk through 5th item and so on.
$cn = 1;
while($result->next()) {
if($cn % 5 == 1) {
//div(.autogrid_wrapper .cte .block)
}
// HTML wraps image
if($cn % 5 == 0) {
//print the close tag of div(.autogrid_wrapper .cte .block)
}
$cn ++;
}
Embed this flow control in your code, I think this will work.
Try with this hope it's helps
<?php
$cn = 1;
while($result->next()) {
if ($cn % 5 == 0) { //check if number is divided by 5 like 5,10,15 etc
//new div'
}
YOUR HTML
if ($cn % 5 == 0) {
//close div'
}
$cn++;
} ?>
I have a list of content that I would have a paging. Until now I was able to count correctly how content we are, then we have 3 content, and imposed a maximum of 2 applications per page, in my index shows the link to go to the next page.
Unfortunately, however, always displays the same content. For example:
http://i62.tinypic.com/156skjo.png
http://i60.tinypic.com/30xcy1l.png
However, I paste the current code, I hope you can help me out. Thank You
<?php require_once 'app/init.php'; ?>
<?php echo View::make('header')->render() ?>
<div class="row">
<!-- START MAIN GRID -->
<?php
// Create a variable imposed where the number of records
// To display on each page
$x_pag = 2;
// Retrieve the current page number.
// Usually you use a querystring
$pag = isset($_GET['pag']) ? $_GET['pag'] : 1;
// Check if $pag is valued and if numeric
// ... Otherwise I assign a value of 1
if (!$pag || !is_numeric($pag)) $pag = 1;
$quest = DB::table('questions')
->count();
// Using a simple mathematical operation define the total number of pages
$all_pages = ceil($quest / $x_pag);
// Calculation of which record start
$first = ($pag - 1) * $x_pag;
$questions = DB::table('questions')
->orderBy('id', 'desc')
->take($x_pag)
->get();
foreach ($questions as $question):
$user = User::find($question->user_id);
?>
<div class="col-md-4 col-lg-4">
<div class="main-grid">
<div class="profile-inner img-responsive" style="background-image: url('images/<?php echo $question->h_image; ?>');border-radius: 10px 10px 0px 0px;">
<img src="<?php echo $user->avatar; ?>" class="small-thumb" >
</div>
<div class="description">
<h5><strong><?php echo $question->user_name; ?></strong>
<?php if (Auth::check() && Auth::user()->id != $question->user_id): ?>
<?php $contact = Contact::find(Auth::user()->id, $question->user_id); ?>
<?php if (!empty($contact) && !empty($contact->accepted)): ?>
( <?php _e('main.remove_contact') ?> )
<?php elseif (!empty($contact)): ?>
( <a href="javascript:EasyLogin.removeContact(<?php echo $question->user_id ?>)" data-contact-id="<?php echo $question->user_id ?>" ><?php _e('main.cancel_contact') ?></a> )
<?php else: ?>
( <?php _e('main.add_contact') ?> )
<?php endif ?>
<?php endif ?>
</h5>
<h3><?php echo $question->h_title; ?>?</h3>
<hr />
Help +
</div>
</div>
</div>
<?php endforeach; ?>
<!-- MAIN GRID END -->
</div>
<?php
// If the total pages are more than 1 ...
// Mold the link to go back and forth between different pages!
if ($all_pages > 1){
if ($pag > 1){
echo "<a href=\"" . $_SERVER['PHP_SELF'] . "?pag=" . ($pag - 1) . "\">";
echo "Back</a> ";
}
if ($all_pages > $pag){
echo "<a href=\"" . $_SERVER['PHP_SELF'] . "?pag=" . ($pag + 1) . "\">";
echo "Next</a>";
}
}
?>
<?php echo View::make('footer')->render() ?>
Consider the following loop:
<?php foreach ($this->item->extra_fields as $key=>$extraField): ?>
<?php if($extraField->value != ''): ?>
<div class="<?php echo ($key%2) ? "odd" : "even"; ?> type<?php echo ucfirst($extraField->type); ?> group<?php echo $extraField->group; ?>">
<span class="itemExtraFieldsValue"><?php echo $extraField->value; ?></span>
</div>
<?php endif; ?>
<?php endforeach; ?>
I wanted to wrap the first 12 items in a div and then the last 2 items in a div. The problem is there are not always 12 items exactly in the first div. There can be between 2 AND 12 items.
How would I manipulate this loop to achieve such? Many thanks
Just use a counter inside the loop to see how many times you've been through it.
<?php $count = 1; ?>
<?php $break= count($this->item->extra_fields) - 2; ?>
<?php echo "<div>"; ?>
<?php foreach ($this->item->extra_fields as $key=>$extraField): ?>
<?php if($extraField->value != ''): ?>
<div class="<?php echo ($key%2) ? "odd" : "even"; ?> type<?php echo ucfirst($extraField->type); ?> group<?php echo $extraField->group; ?>">
<span class="itemExtraFieldsValue"><?php echo $extraField->value; ?></span>
</div>
<?php $count++; ?>
<?php endif; ?>
<?php if ($count == $break) : ?>
<?php echo "</div><div>"; $count ==0; ?>
<?php endif; ?>
<?php endforeach; ?>
<?php echo '</div>'; ?>
i have to modify this code, to echo every 4 thumbs (extraimage) inside a div...
I searched on Stackoverflow but all answers talk about setting a counter, i want to avoid this (if possible) using a counter that is already counting the extraimages.. i think it most be as easy as a conditional
if counter extraimages==3 echo div.. but how do i go back to 0 ,, or maybe i am missunderstanding the way to do this..
This is the part of the code where the array is set and the for each is set.
<?php if($extraimagecount >0){?>
<?php foreach ($extraimage as $key=>$value){?>
<?php }?>
<?php }?>
<a href="<?php echo DATA_DIR."/".$id."/".$this->get_variable('firstimage');?>" >
<img src="<?php echo DATA_DIR."/".$id."/".$this->get_variable('firstimage');?>" class="minis"/>
</a>
<?php if($extraimagecount >0){
$rotate=1;
$tumppr=0;
?>
<?php foreach ($extraimage as $key=>$value){
$rotate=$rotate+1;
?>
<a href="<?php echo DATA_DIR."/".$id."/".$value['image'];?>" >
<img src="<?php echo DATA_DIR."/".$id."/t_".$value['image'];?>" class="minis"/>
</a>
<?php
if($rotate==8)
{
$rotate=0;
$tumppr=$tumppr+1;
?>
<?php
}
?>
<?php }?>
<?php }?>
</div>
<?php
$lftstr="";
$rgtstr="";
if($extraimagecount >0)
{
$extcnt=count($extraimage);
$extcntnew=$extcnt+1;
$extdivide=intval(($extcntnew/8));
$extmode=($extcntnew % 8);
for($i=0;$i<$extdivide;$i++) //************ For Right Arrow ***************/
{
?>
<div id="rgt_<?php echo $i;?>" class="rgt" <?php if($i >0 || $extmode ==0){?>style="display: none;"<?php }?> ><img class="rgtimg" src="images/rnext.png"></div>
<?php
$rgtstr=$rgtstr.$i.'_';
}
for($ii=1;$ii<$extdivide;$ii++) //************ For left Arrow ***************/
{
?>
<div id="lft_<?php echo $ii;?>" class="lft" style="display: none;" ><img class="lftimg" src="images/lnext.png"></div>
<?php
$lftstr=$lftstr.$ii.'_';
}
if($extmode >0)
{
?>
<div id="lft_<?php echo $extdivide;?>" class="lft" style="display: none;" ><img class="lftimg" src="images/lnext.png"></div>
<?php
$lftstr=$lftstr.$extdivide.'_';
}
}
?>
Its actually quite easy, use the % operator. a%b will return the remainder of a/b. heres how you use it
for($i=0;$i<9;$i++)
{
echo $i%3." ";
}
this will print out
0 1 2 0 1 2 0 1 2
You can then use this to create groups of 4 in your case.