I want to apply css class as per below condition - php

I want to apply css class as per below condition but after applying this code each content is repeated by 8 times and if i complete for loop before div then css classes are not getting applied.
<?php $size=count($Spacategories);
for($i=0;$i<8;$i++){
if($i==0 || $i==5){ ?>
<li class="valign">
<?php } elseif($i==1 || $i==7) { ?>
<li class="lalign">
<?php } elseif($i==2 || $i==6) { ?>
<li class="talign">
<?php } else { ?>
<li class="ralign">
<?php } ?>
<div class="image">
<img src="<?php echo $spacategory['thumb']; ?>" title="<?php echo $spacategory['name']; ?>" alt="<?php echo $spacategory['name']; ?>" />
</div>
<div class="text">
<h3><?php echo $spacategory['name']; ?></h3>
<p><?php echo substr($spacategory['description'],0,150); ?></p>
<?php echo $text_view_more; ?>
</div>
</li>
<?php } } ?>

Change all instances of
$spacategory
to
$Spacategories[$i]
This will fix it.
Also, I don't see what's populating $text_view_more. You should probably change that to some data point from $Spacategories?
Finally, it seems you need to be looping based on the number of categories, so change
for($i=0;$i<8;$i++){
to
for($i=0;$i<$size;$i++){

In your code, $spacategory is not an item from $spacategories with the index $i in each iteration. You should have started the for:
$spacategory = $spacategories[$i];
or use a foreach instead:
<?php
$classesIndexMap = array(
0 => 'valign',
1 => 'lalign',
2 => 'talign',
5 => 'valign',
6 => 'talign',
7 => 'lalign',
);
$html_view_more = htmlspecialchars($text_view_more, ENT_NOQUOTES, 'UTF-8');
?>
<?php foreach($spacategories as $i => $spacategory) : ?>
<li class="<?= isset($spacategory[$i]) ? $spacategory[$i] : 'ralign' ?>">
<div class="image">
<img src="<?= htmlspecialchars($spacategory['thumb'], ENT_QUOTES, 'UTF-8') ?>"
title="<?= htmlspecialchars($spacategory['name'], ENT_QUOTES, 'UTF-8') ?>"
alt="<?= htmlspecialchars($spacategory['name'], ENT_QUOTES, 'UTF-8') ?>" />
</div>
<div class="text">
<h3><?= htmlspecialchars($spacategory['name'], ENT_NOQUOTES, 'UTF-8') ?></h3>
<p><?= substr($spacategory['description'],0,150); ?></p>
<a href="<?= htmlspecialchars($spacategory['href'], ENT_QUOTES, 'UTF-8'); ?>"
class="learmmore"><?= $html_view_more ?></a>
</div>
</li>
<?php endforeach; ?>

Related

Wordpress - strange symbol appeared beside the post thumbnail

Sorry I feel it an idiot question, but I can't fix it, those characters "> appeared beside the post thumbnail, and I don't know what's wrong there!
<?php
$notification_id = $notification->ID;
$notification_time = strtotime($notification->post_date);
$type = get_post_meta($notification_id, 'dw_notification_type', true);
$custom_type = get_post_meta($notification_id, 'dw_notification_custom_type', true);
$image = get_post_meta($notification_id, 'dw_notification_image', true);
?>
<li id="dw-notif-<?php echo $notification_id;?>" class="<?php echo dwnotif_check_user_read(false, $notification_id)?'read':'unread'; ?> <?php echo $type?$type:''; ?> <?php echo $custom_type?$custom_type:''; ?>" data-time="<?php echo $notification_time;?>">
<a href="<?php echo get_permalink($notification->ID);?>">
<div class="notif-avatar">
<?php if(has_post_thumbnail($notification->ID)): ?>
<?php echo get_the_post_thumbnail($notification->ID, 'post-thumbnail'); ?>
<?php elseif($image): ?>
<img src="<?php echo $image; ?>">
<?php else: ?>
<img src="<?php echo dwnotif_default_image(); ?>">
<?php endif; ?>
</div>
<div><?php echo $notification->post_title; ?></div>
<date class="notif-time"><?php echo human_time_diff( $notification_time, current_time('timestamp') ) . __( ' ago', 'dw-notifications' ); ?></date>
</a>
</li>

PHP: Foreach with "if-else" statement

I'm doing some changes on an online store that is running OpenCart 2.2.
When browsing below the categories on the left side, there are 2 carousels that show different promotions. I want to modify it that before the second carousel there is a description text. To do that I should edit the template file. And here is where I get lost..
This is the code in the template file:
<div id="banner<?php echo $module; ?>" class="owl-carousel">
<?php foreach ($banners as $banner) { ?>
<div class="item">
<?php if ($banner['link']) { ?>
<img src="<?php echo $banner['image']; ?>" alt="<?php echo $banner['title']; ?>" class="img-responsive" />
<?php } else { ?>
<img src="<?php echo $banner['image']; ?>" alt="<?php echo $banner['title']; ?>" class="img-responsive" />
<?php } ?>
</div>
<?php } ?>
</div>
From I see in the browser inspector and in my case, this code generates 2 banners with the ids - "banner0" and "banner1". The description text should go at the top of the code (right before ). If I use a simple paragraph, it gets displayed twice - above each banner.. How should I change it so that it will display the paragraph only above the second banner (id - banner1)?
I was thinking about an if-else statement, but I'm not sure if that will work... Could anyone help out a bit? My knowledge in PHP isn't much... :S
Thanks in advance!
Best regards,
Tsvetko Krastev
Then you need to know that this is the second time round the foreach loop. So change the foreach to include the index like this, and add a test inside the loop for $i == 1
<div id="banner<?php echo $module; ?>" class="owl-carousel">
<?php //foreach ($banners as $banner) {
foreach ($banners as $i => $banner) {
?>
<div class="item">
<?php if ($banner['link']) {
if ( $i == 1 ) {
echo 'YOUR HTML CONTAINING SOME TEXT HERE';
}
?>
<img src="<?php echo $banner['image']; ?>" alt="<?php echo $banner['title']; ?>" class="img-responsive" />
<?php } else { ?>
<img src="<?php echo $banner['image']; ?>" alt="<?php echo $banner['title']; ?>" class="img-responsive" />
<?php } ?>
</div>
<?php } ?>
</div>
If I get the code right, $module is 0/1 (if id's get values banner0 and banner1), then this should work:
<div id="banner<?php echo $module; ?>" class="owl-carousel">
<?php foreach ($banners as $banner) { ?>
<?php if ($module == "1") { ?>
<p>Your description</p>
<?php } ?>
<div class="item">
<?php if ($banner['link']) { ?>
<img src="<?php echo $banner['image']; ?>" alt="<?php echo $banner['title']; ?>" class="img-responsive" />
<?php } else { ?>
<img src="<?php echo $banner['image']; ?>" alt="<?php echo $banner['title']; ?>" class="img-responsive" />
<?php } ?>
</div>
<?php } ?>
</div>
Thank you so very much guys!!! Tried both versions and they throw a error, but looking more carefully into the code and both your solutions, I came up with this:
<div id="desc<?php echo $module; ?>">
<?php if ($module == 1) { ?>
<p>Description</p>
<?php } ?>
<div id="banner<?php echo $module; ?>" class="owl-carousel">
<?php foreach ($banners as $banner) { ?>
<div class="item">
<?php if ($banner['link']) { ?>
<img src="<?php echo $banner['image']; ?>" alt="<?php echo $banner['title']; ?>" class="img-responsive" />
<?php } else { ?>
<img src="<?php echo $banner['image']; ?>" alt="<?php echo $banner['title']; ?>" class="img-responsive" />
<?php } ?>
</div>
<?php } ?>
</div>
</div>
Thank you very much again for the quick responses and the help! :) :3
Best regards,
Tsvetko Krastev

Arrange divs on new row every third time

I have this most annoying problem; I'm trying to arrange three divs on a row, and then new row, and another three divs and so on, like this:
<div class="container">
<div class="row">
<div class="col-sm-1">1</div>
<div class="col-sm-1">2</div>
<div class="col-sm-1">3</div>
</div>
<div class="row">
<div class="col-sm-1">4</div>
<div class="col-sm-1">5</div>
<div class="col-sm-1">6</div>
</div>
</div>
As for this accepted answer,
There is one catch: 0 % 3 is equal to 0. This could result in
unexpected results if your counter starts at 0.
So how would i implement this into this code:
<div class="col-md-8">
<?php
foreach($this->movies->movie_data as $key => $movie){
$string = file_get_contents("http://example.com/?t=" . urlencode($movie->movie_titel). "&y=&plot=short&r=json");
$result = json_decode($string);
if($result->Response == 'True'){
?>
<div class="col-sm-4">
<?php if($result->Poster == 'N/A') : ?>
<a href="<?php echo Config::get('URL')?>ladybug/day/<?php echo $this->city ?>/<?php echo $movie->movie_id ?>">
<img src="<?php echo Config::get('URL')?>/images/na.png" class="img-responsive img-thumbnail"></a>
<?php else: ?>
<a href="<?php echo Config::get('URL')?>ladybug/day/<?php echo $this->city ?>/<?php echo $movie->movie_id ?>">
<img src="<?php echo $result->Poster; ?>" class="img-responsive img-thumbnail"></a>
<?php endif; ?>
<div><b><?php echo $result->Title; ?></b></div>
<div><i><?php // echo $result->Plot; ?></i></div>
</div>
<?php }else{ ?>
<div class="col-sm-4">
<a href="<?php echo Config::get('URL')?>ladybug/day/<?php echo $this->city ?>/<?php echo $movie->movie_id ?>">
<img src="<?php echo Config::get('URL')?>/images/na.png" class="img-responsive img-thumbnail"></a>
<div><b><?php echo $movie->movie_titel ?></b></div>
<div class="plot"><i><?php //echo 'N/A' ?></i></div>
</div>
<?php }}} ?
</div>
For some reason, divs is arranged like this:
My question: How do I arrange thumbnails on a new row, every third time?
Found the answer in the other Q... Didn't read, sorry about that.
<?php }
if (($key + 1) % 3 == 0) { ?>
</div>
<?php }
}} ?>

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

generating links dynamically

so i guess this is pretty easy for most of you, but i can't figure this out.
im trying to make the links dynamic eg: href="linkname(#1 or #2 etc)"
any ideas?
<?php if ($top_fundraisers && is_array($top_fundraisers)): ?>
<?php foreach ($top_fundraisers as $index => $fundraiser): ?>
<a title="" class="fancybox" href="linkname(GENERATE CODE HERE)">
<div class="top-fundraiser">
<div id="newo<?php print htmlentities($index + 1); ?>" class="top-fundraiser-image">
<img src="<?php
if($fundraiser['member_pic_medium']) {
print htmlentities($fundraiser['member_pic_medium']);
} else {
print $template_dir . '/images/portrait_placeholder.png';
}
?>"/>
</div>
</div>
</a>
<?php endforeach;?>
<?php endif; ?>
Suppose below is what you need.
<?php if ($top_fundraisers && is_array($top_fundraisers)): ?>
<?php foreach ($top_fundraisers as $index => $fundraiser): ?>
<a title="" class="fancybox" href="linkname(#<?php echo $index + 1; ?>)">
<div class="top-fundraiser">
<div id="newo<?php print htmlentities($index + 1); ?>" class="top-fundraiser-image">
<img src="<?php
if($fundraiser['member_pic_medium']) {
print htmlentities($fundraiser['member_pic_medium']);
} else {
print $template_dir . '/images/portrait_placeholder.png';
}
?>"/>
</div>
</div>
</a>
<?php endforeach;?>
<?php endif; ?>

Categories