I have here a quite complicated loop what I would like to modify while is not really working as I expected. The problem is: my if statements else condition is generating trash and I don't really know where the problem comes
<div id="container">
<?php include 'header.php';
foreach ($vars as $item):
if (is_array($item['productItems'])):?>
<div class="product_box">
<h4>
<?php echo $item['title']?>
</h4>
<?php foreach($item['productItems'] as $products):?>
<?php foreach($products as $key=>$product):?>
<div class="product">
<div class="image_holder">
<a href="<?php echo $product['image']['medium'];?>" class="preview">
<img src="<?php echo $product['image']['small'];?>"
alt="gallery thumbnail" title="" />
</a>
<?php foreach($product['trackingLinks'] as $key=>$trackinglink): ?>
<a href="<?php echo $trackinglink[0]['ppc']?>"> <span><?php echo $product['name'];?>
</span>
</a>
</div>
<h5>
<?php echo 'Preise: '. $product['price'].' '.$product['currency'];?>
</h5>
<span><?php echo 'Versand Kosten: '.$product['shippingCosts'].' EUR';?>
</span>
<p>
<?php echo (substr($product['descriptionLong'], 0,150 )).'...mehr';?>
</p>
<a class="shopping_button"
href="<?php echo $trackinglink[0]['ppc']?>">zum Shop</a>
</div>
<?php endforeach ?>
<?php endforeach ?>
<?php endforeach ?>
<div class="ajax_bar">
<select id="ajax_call" size="1" name="Weitere Produkte">
<option value='50'>Weitere Produkte</option>
<option value='100'>weitere 100</option>
<option value='150'>weitere 150</option>
</select>
</div>
</div>
<?php else: ?>
<div class="text_box">
<h2>
<?php echo $item['title'];?>
</h2>
<p>
<?php echo $item['text'];?>
</p>
</div>
<?php endif; ?>
<?php endforeach; ?>
<?php include 'footer.php';?>
</div>
I'm going to make a wild guess here and say that this is because you don't escape your output using htmlspecialchars or htmlentities.
If you're not sure what's inside the for-loop, use this nifty construct to debug on page:
echo '<pre>', htmlspecialchars(print_r($vars, true)), '</pre>';
It also helps to look at the page source instead; this usually gives vital clues as to why your page layout is so messed up.
Update
Okay got it! It's because of this:
<?php else: ?>
<div class="text_box">
<h2>
<?php echo $item['title'];?>
</h2>
<p>
<?php echo $item['text'];?>
</p>
The $item is a string here, and $item['title'] is understood by PHP as $item[0] which is the first character of $item :)
Related
Attributes are (crop_name, crop_img, crop-detail) and I want to show attribute value in this dive (crop_img) inside the <img> tag, (crop_name) inside the <span> tag and (crop_detail) inside the <em> tag.
<?php
foreach($records as $r) { }?>
<div class="cols">
<figure class="pad_bot2 pad_top1"><img src="" alt="" width="200px" height="211px"></figure>
<p class="pad_bot2"> <span class="font1 color2"></span><br>
<em></em><br>
Read More
</p>
</div>
PHP tags can be directly embedded into HTML tags. You can use like this.
<?php
foreach($records as $r) { ?>
<div class="cols">
<figure class="pad_bot2 pad_top1"><img src="" alt="" width="200px" height="211px"></figure>
<p class="pad_bot2"> <span class="font1 color2"></span><br>
<em></em><br>
Read More
</p>
</div>
<?php }?>
Loop the HTML like below and update value to each markup like for image src=<?php echo $r->crop_img; ?>
<?php foreach($records as $r) { ?>
<div class="cols">
<figure class="pad_bot2 pad_top1"><img src="<?php echo $r->crop_img; ?>" alt="" width="200px" height="211px"></figure>
<p class="pad_bot2"> <span class="font1 color2"><?php echo $r->crop_name; ?></span><br>
<em><?php echo $r->crop_detail; ?></em><br>
Read More
</p>
</div>
<?php }?>
Simple add html inside of foreach body. Try this code. If you need any more help please let me know.
if you are receiving array in $records then use
<?php echo $r['index']; ?>
if receiving object then use
<?php echo $r->index; ?>
Below is complete code as you want.
<?php foreach($records as $r) { ?>
<div class="cols">
<figure class="pad_bot2 pad_top1"><img src="" alt="" width="200px" height="211px"></figure>
<p class="pad_bot2"> <span class="font1 color2"></span><br>
<em></em><br>
Read More
</p>
</div>
<? } ?>
just trying to make things more readable:
<?php foreach($records as $r): ?>
your codes here
<?php endforeach; ?>
i made this foreach loop but it just doesn't show me anything on the screen doesn anybody see what the problem is?
There're 2 items of the array $data['page']['children']
<?php foreach ($data['page']['childeren'] as $news): ?>
<div class="donerenNews">
<div class="doneren-image">
<img class="group list-group-image doneren-image" src="<?php echo $site_url ?>assets/img/placeholderSubPage.png" alt="">
</div>
<div class="caption doneren-tekst-left">
<h1>
<?php echo $news['title'] ?>
</h1>
<p>
<?php echo $news['description'] ?>
</p>
</div>
</div>
<?php endforeach; ?>
it looks like you put childreren instead of children in
foreach ($data['page']['childeren']
so change it to
foreach ($data['page']['children']
quick question, i want to link the BLOCK1 to another page. Is it possible? do anyone got a solution? i have tried abit and my brain is going mad T_T. there are 5 other blocks that i which to link to other pages aswell.
this is the code below:
<div class="midrow_block axn_block1">
<div class="mid_block_content">
<!--BLOCK1 IMAGE-->
<?php if(!empty($optimizer['block1_image']['url']) && empty($optimizer['block1_img_bg'])){ ?>
<div class="block_img"><img src="<?php echo $optimizer['block1_image']['url']; ?>" width="<?php echo $optimizer['block1_image']['width']; ?>" height="<?php echo $optimizer['block1_image']['height']; ?>" /></div>
<?php } ?>
<div class="block_content">
<h3>
<?php echo do_shortcode( $optimizer['block1_text_id']); ?>
</h3>
<?php echo do_shortcode($optimizer['block1_textarea_id']); ?>
</div>
</div>
</div>
</div>
<?php } ?>
<!--BLOCK1 END-->
Just put a <a>-Tag arount the block you want to link:
<a href="'your link here'"> <!-- start link here -->
<div class="midrow_block axn_block1">
<div class="mid_block_content">
<!--BLOCK1 IMAGE-->
<?php if(!empty($optimizer['block1_image']['url']) && empty($optimizer['block1_img_bg'])){ ?>
<div class="block_img"><img src="<?php echo $optimizer['block1_image']['url']; ?>" width="<?php echo $optimizer['block1_image']['width']; ?>" height="<?php echo $optimizer['block1_image']['height']; ?>" /></div>
<?php } ?>
<div class="block_content">
<h3>
<?php echo do_shortcode( $optimizer['block1_text_id']); ?>
</h3>
<?php echo do_shortcode($optimizer['block1_textarea_id']); ?>
</div>
</div>
</div>
</a> <!-- end link here -->
Or put it around any other part you want to have the link on. It's just basic HTML and has nothing to do with PHP.
here is the fullcode of the block. i missed to copy the top of it:
<?php if ((!empty ($optimizer['block1_text_id'])) || (!empty ($optimizer['block1_textarea_id'])) ) { ?>
<div class="midrow_block axn_block1">
<div class="mid_block_content">
<!--BLOCK1 IMAGE-->
<?php if(!empty($optimizer['block1_image']['url']) && empty($optimizer['block1_img_bg'])){ ?>
<div class="block_img"><img src="<?php echo $optimizer['block1_image']['url']; ?>" width="<?php echo $optimizer['block1_image']['width']; ?>" height="<?php echo $optimizer['block1_image']['height']; ?>" /></div>
<?php } ?>
<div class="block_content">
<h3>
<?php echo do_shortcode( $optimizer['block1_text_id']); ?>
</h3>
<?php echo do_shortcode($optimizer['block1_textarea_id']); ?>
</div>
</div>
</div>
You can use with this JS
<div class="midrow_block axn_block1" onclick="location.href='url'">content</div>
jQuery:
$("div").click(function(){
window.location=$(this).find("a").attr("href"); return false;
});
I have this code for a slideshow in my Drupal 7, Nexus 7.x-1.3 theme.
My website is multilingual and I need to use a different image for every language, eg. I would like to change slide-image-1.jpg (en language) with other image-name for (gr language).
My code:
<?php if ($is_front): ?>
<?php if (theme_get_setting('slideshow_display','nexus')): ?>
<?php
$slide1_head = check_plain(theme_get_setting('slide1_head','nexus')); $slide1_desc = check_markup(theme_get_setting('slide1_desc','nexus'), 'full_html'); $slide1_url = check_plain(theme_get_setting('slide1_url','nexus'));
$slide2_head = check_plain(theme_get_setting('slide2_head','nexus')); $slide2_desc = check_markup(theme_get_setting('slide2_desc','nexus'), 'full_html'); $slide2_url = check_plain(theme_get_setting('slide2_url','nexus'));
$slide3_head = check_plain(theme_get_setting('slide3_head','nexus')); $slide3_desc = check_markup(theme_get_setting('slide3_desc','nexus'), 'full_html'); $slide3_url = check_plain(theme_get_setting('slide3_url','nexus'));
?>
<div id="slidebox" class="flexslider">
<ul class="slides">
<li>
<img src="<?php print base_path() . drupal_get_path('theme', 'nexus') . '/images/slide-image-1.jpg'; ?>"/>
<?php if($slide1_head || $slide1_desc) : ?>
<div class="flex-caption">
<h2><?php print $slide1_head; ?></h2><?php print $slide1_desc; ?>
<a class="frmore" href="<?php print url($slide1_url); ?>"> <?php print t('CONTACT US'); ?> </a>
</div>
<?php endif; ?>
</li>
<li>
<img src="<?php print base_path() . drupal_get_path('theme', 'nexus') . '/images/slide-image-2.jpg'; ?>"/>
<?php if($slide2_head || $slide2_desc) : ?>
<div class="flex-caption">
<h2><?php print $slide2_head; ?></h2><?php print $slide2_desc; ?>
<a class="frmore" href="<?php print url($slide2_url); ?>"> <?php print t('CONTACT US'); ?> </a>
</div>
<?php endif; ?>
</li>
<li>
<img src="<?php print base_path() . drupal_get_path('theme', 'nexus') . '/images/slide-image-3.jpg'; ?>"/>
<?php if($slide3_head || $slide3_desc) : ?>
<div class="flex-caption">
<h2><?php print $slide3_head; ?></h2><?php print $slide3_desc; ?>
<a class="frmore" href="<?php print url($slide3_url); ?>"> <?php print t('CONTACT US'); ?> </a>
</div>
<?php endif; ?>
</li>
</ul><!-- /slides -->
<div class="doverlay"></div>
</div>
<?php endif; ?>
<?php endif; ?>
Is there any solution for that? Thank you.
There is a Drupal connector from Smartling that may be of help. Here is a video showing how the connector works.
Many sites running on Smartling do language/country-specific image replacement, which is explained here.
Hope this helps,
Nataly
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<SCRIPT>
function english(){
$(#image).attr('src','http://link.to.your/english/image.png')
}
function greek(){
$(#image).attr('src','http://link.to.your/greek/image.png')
}
</SCRIPT>
<IMG id="image" src="http://link.to.your/greek/image.png">
Change to English<BR>
Change to Greek
Finally I fixed it in page.tpl with php code. One if/else on your page language to set you picture path.
Regards, HashKey
I'm making a website with Joomla and am using RokSprocket to display some news. Now, some Titles are too long, so I wanted to shorten it after a certain character number, but it's not working. Am a total php noob, my apologies.
Here's the whole code:
<li data-lists-item>
<h4 class="sprocket-lists-title ">
<?php if ($item->custom_can_have_link): ?><a href="<?php echo $item->getPrimaryLink()->getUrl(); ?>"><?php endif; ?>
<?php if(strlen($item->getTitle)>10)
echo substr($item->getTitle,0,10) . ' ...';
else
echo $item->getTitle();?></a>
<div class="date"><?php echo date('d.M. Y', strtotime($item->getDate()));?></div>
</h4>
<span class="sprocket-lists-item" data-lists-content>
<span class="sprocket-padding">
<?php if ($item->getPrimaryImage()) :?>
<img src="<?php echo $item->getPrimaryImage()->getSource(); ?>" class="sprocket-lists-image" />
<?php endif; ?>
<?php echo $item->getText(); ?>
<?php if ($item->getPrimaryLink()) : ?>
<span><?php rc_e('READ_MORE'); ?></span>
<?php endif; ?>
</span>
</span>
</li>
I'm hoping it's something very obvious and I'm just being stupid. Thanks a bunch already!
$item->getTitle is probably a method not a property, so try:
$item->getTitle(); // <-- parenthesis
Change to:
<?php if(strlen($item->getTitle())>10)
echo substr($item->getTitle(),0,10) . ' ...';