How can I adjust this div in to foreach loop? - php

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

Related

Getting images from RSS using PHP

I want to display an RSS feed on a web page. Currently I'm only getting the title and description but I want to get images also.
<?php
$html = "";
$url = "http://rss.cnn.com/rss/edition.rss";
$xml = simplexml_load_file($url);
foreach ($xml->channel->item as $itm) {
$title=$itm->title;
$link=$itm->link;
$pubDate=$itm->pubDate;
$description=$itm->description;
$img=$itm->media;
}
?>
<div class="container">
<div class="row">
<div class="col-md-6">
<?php echo $title; ?><br>
<span class="text text-info"><?php echo $pubDate; ?></span><br>
<p align="right" class="text text-info"><?php echo $description; ?></p>
<img src="<?php echo $img; ?>"></img>
</div>
</div>
</div>
<?php } ?>
You've finished your foreach loop twice. That's almost certainly not what you want to happen, or you wouldn't have used the foreach loop.
Try removing the first } (before the bulk of the HTML) and also replacing <img src="<?php echo $img; ?>"></img> with <img src="<?php echo $img; ?>" />.

Foreach loop doesn't work but it gets 2 items from my array?

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']

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.

PHP: link a Block

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;
});

php foreach generating trash

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 :)

Categories