Embed div elements in php foreach? [closed] - php

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I tried to embed 2 elements with class "elementcontent" in element with class "row", can someone tell me what's wrong in my code (see second code block)?
This is raw code without if request to embed 2 elements in one:
<?php
$i = 1;
foreach ($items as $key=>$item) {
?>
<div class="row">
<div class="elementcontent">element content</div>
</div>
<?php
$i++;
}
?>
This is my code where i try to embed elements:
<?php
$i = 1;
foreach ($items as $key=>$item) {
?>
<div class="row">
<div class="elementcontent">element content</div>
<?php
if ($i % 2 == 0) {
echo '</div><div class="row">';
}
?>
</div>
<?php
$i++;
}
?>
This way i found with help from this source: How can I alternate div elements in php foreach loop

To place two elements in each row you want to test the $key -
<?php
$items = array('foo', 'bar', 'glorp', 'baz');
foreach ($items as $key=>$item) {
if($key % 2 == 0){
echo '<div class="row">';
echo '<div class="elementcontent">'. $item . '</div>';
} else {
echo '<div class="elementcontent">'. $item . '</div>';
echo '</div>';
}
}
?>
By doing it with the key there is no need for an iterator.

If I understood you, what you want is a ROW element, with 2 ELEMENTCONTENT inside of it.
You can do this like this:
<?php
$i = 0;
foreach ($items as $key => $item) {
$row = ($i % 2 == 0); ?>
<?php if ($row): ?><div class="row"><?php endif; ?>
<div class="elementcontent">element content</div>
<?php if (!$row): ?></div><?php endif; ?>
<?php
$i++;
}
if (count($items) % 2 === 1) {
echo '</div>';
}
?>
Every 2 items will be wrapped with a div with a class row.
I've used PHP's alternative syntax for more elegant code.
Answer updated due to an error and also checking if the number of items is an odd number, to close the div tag.

Related

PHP - How to use a div to wrap all items Inside a foreach loop

I have been scratching my head for half hour now trying to figure this out
I have a wrapper div inside a foreach loop and I'm trying to force it to echo one and not duplicate
foreach ($apples as $apple) {
//echo only once bellow
<div class="Wrapper">
//echo only once Above
echo $apple;
//echo only once bellow
</div>
//echo only once Above
}
I do not wish to move my Wrapper div outside the foreach, It is very important for the div to be inside the foreach and to be without duplicates.
You didn't mention but If you need to put Wrapper div only if you have any count in $apples variable then simply check with if(count($apples)) and then put Wrapper
<?php
if(count($apples)){
echo '<div class="Wrapper">';
foreach ($apples as $apple) {
echo $apple;
}
echo '</div>';
}
?>
Note: I know this is not good way to write but as per his condition I am suggesting this code.
I don't know why you don't want to put div outside foreach loop but anyway you can use the following code to achieve that...
<?php
foreach ($apples as $apple) {
static $i = 0;
if ($i == 0) {
echo "<div class='Wrapper'>";
}
echo $apple;
$i++;
if ($i == count($apples)){
echo "</div>";
}
}
?>
Maybe this will help since you want to keep it inside foreach loop
$numItems = count($arr);
$i = 0;
foreach($arr as $ar) {
$i = $i+1;
if ($i == 1) {
echo '<div class="class">';
}
// do your stuff
if($i == $numItems) {
echo "</div>";
}
}

Show values of the db in organization with Bootstrap row class for every 3 columns

I wrote some code which retrieves data from the database and gives the output to the web page using php. I use bootstrap to show the data in some organized way on the web page. But the code returns some syntax errors.
function break_array($array, $page_size) {
$arrays = array();
$i = 0;
foreach ($array as $index => $item) {
if ($i++ % $page_size == 0) {
$arrays[] = array();
$current = & $arrays[count($arrays)-1];
}
$current[] = $item;
}
return $arrays;
}
?>
<?php
foreach (break_array($row = $sql->fetch(), 3) as $columns) {
<div class="row"> *** Error is gives on here ***
foreach ($columns as $article) {
<div class="col-md-4">
<p>{{ strip_tags(str_limit($article->body, $limit = 90, $end = '...')) }}</p>
</div>
}
</div>
}
How can I correct this?
Your 'code' is mixing HTML with PHP in an incorrect way. PHP needs to be interpreted server side to generate HTML, this HTML is then sent to the browser and is the source-code for the browser.
You can mix PHP & HTML in a couple of ways. Here some examples:
1. Enter and leaver php parsing mode
Note that all PHP-code that needs to be interpreted is between <?php ... ?> tags, this is interpreted at the server side.
<?php foreach (break_array($row = $sql->fetch(), 3) as $columns) { ?>
<div class="row">
<?php foreach ($columns as $article) { ?>
<div class="col-md-4">
<p><?php {{ strip_tags(str_limit($article->body, $limit = 90, $end = '...')) }} ?></p>
</div>
<?php } ?>
</div>
<?php } ?>
2. Echo HTML as strings in PHP
Note that all HTML-code is inside a php echo() statement:
<?php
foreach (break_array($row = $sql->fetch(), 3) as $columns) {
echo '<div class="row">';
foreach ($columns as $article) {
echo '<div class="col-md-4">';
echo ' <p>' . strip_tags(str_limit($article->body, $limit = 90, $end = '...')) . '</p>';
echo '</div>';
}
echo '</div>';
}
?>

Grab first item of array through custom content types manager

I'm using Wordpress and Custom Content Types Manager, how do I go about editing the code below to only grab the first image from the array below?
<?php
$array_of_images = get_custom_field('slide_images:to_array');
foreach ($array_of_images as $img_id) {
?>
<div><?php print CCTM::filter($img_id, 'to_image_tag'); ?> </div>
<?php } ?>
I tried adding in array_slice($array_of_images, 0, 1); but no luck so far. Thanks!
If all else fails you could do the same as what you have except add an $i value. It's kind of dumb but it would work if you can't get a normal method to work. This would be a last ditch effort sort of thing...
<?php
$array_of_images = get_custom_field('slide_images:to_array');
$i = 0;
foreach ($array_of_images as $img_id) { ?>
<div><?php print CCTM::filter($img_id, 'to_image_tag'); ?> </div>
<?php if($i == 0) break; } ?>
$key = array_keys($array_of_images);
$value = $array_of_images[$key[0]];

Center Logo Inside Dynamic li Navigation

So i have a CMS which uses a foreach loop to generate the navigation which consists of individual list items inside a ul.
Basically what I want is to have my logo inserted in the center of these links, with an equal number of links on either side.
I've got my code to split up into two different columns of navigation with a gap, but I cant figure out where to place the logo div so it doesn't repeat more than once, current code also throws out some empty list items which I don't need.
<ul>
<?php $i = 0; foreach($items as $item): ?>
<li><a<?php ecco($item->isOpen(), ' class="active"') ?> href="<?php echo $item->url() ?>"><?php echo html($item->title());?></a></li>
<?php if (++$i % 3 === 0 && $i !== count($items)) echo "</li><li>"; endforeach ?>
</ul>
Thanks.
How about something like this?
UPDATE
Since your count doesn't work, try this:
<ul>
<?php
$j = 0;
$breakPoint = 0;
foreach($items as $item) {
$j++;
$breakPoint = $j;
}
$i = 0;
foreach($items as $item) { ?>
if ($i === $breakPoint) {
/* insert Logo Code */
} else { ?>
<li><a<?php ecco($item->isOpen(), ' class="active"') ?> href="<?php echo $item->url() ?>"><?php echo html($item->title());?></a></li>
<?php
}
$i++;
} ?>
</ul>
Haven't tested it. And it depends if you have a odd or even number of $item elements.

group php foreach loop (Closed) [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
How can i group php foreach?i'v already tried 5 days,but still can't get it work
i don't know php foreach how it works, but I'm learning it,
Thanks to everyone's advice
original php:
<?php if(isset($this->leading) && count($this->leading)): ?>
<?php foreach($this->leading as $key=>$item): ?>
<?php
$this->item=$item;
echo $this->loadTemplate('item');
?>
<?php endforeach; ?>
<?php endif; ?>
<?php if(isset($this->primary) && count($this->primary)): ?>
<?php foreach($this->primary as $key=>$item): ?>
<?php
$this->item=$item;
echo $this->loadTemplate('item');
?>
<?php endforeach; ?>
<?php endif; ?>
<?php if(isset($this->secondary) && count($this->secondary)): ?>
<?php foreach($this->secondary as $key=>$item): ?>
<?php
$this->item=$item;
echo $this->loadTemplate('item');
?>
<?php endforeach; ?>
<?php endif; ?>
i tried
<?php if(isset($this->leading) && count($this->leading)) && (isset($this->primary) && count($this->primary)) && (isset($this->secondary) && count($this->secondary)): ?>
<!-- Leading items -->
<?php foreach (array($this->leading, $this->primary, $this->secondary) as $key=>$item) ($this->leading as $key=>$item): ?>
<?php
// Load category_item.php by default
$this->item=$item;
echo $this->loadTemplate('item');
?>
<?php endforeach; ?>
<?php endif; ?>
but not work
As always, your assistance is appreciated!
Thanks!every one:)
Thanks,Steven!
do you mean something like this..?
<?php
$arr = array();
$arr[] = $this->leading;
$arr[] = $this->primary;
$arr[] = $this->secondary;
foreach($arr as $v) {
if(is_array($v) && (count($v) > 0)) {
foreach($v as $item) {
$this->item=$item;
echo $this->loadTemplate('item');
}
}
}
?>
Define a function like:
function foobar ($tmp) {
if(isset($tmp) && count($tmp)) {
foreach ($tmp as $key => $item) {
// Load category_item.php by default
$this->item = $item;
echo $this->loadTemplate('item');
}
}
}
and call it with your data:
foobar($this->leading);
foobar($this->primary);
foobar($this->secondary);
The simplest way to handle this is to first build a list containing all the items you need, and then use just one foreach to cycle through that list.
I'd also recommend some consistancy in whether the three variables (leading, primary, secondary) are defined. If you can guarantee that they're set and are arrays - even if they're empty arrays, it can make your code as simple as this:
<?php
$items = array_merge($this->leading, $this->primary, $this->secondary);
foreach ($items as $Item) {
$this->item = $item;
$this->loadTemplate('item');
}
?>
If you really don't know whether the variables are set, and you can't refactor your code elsewhere to change this, you could do this instead:
<?php
$items = array();
if (isset($this->leading)) $items = array_merge($items, $this->leading);
if (isset($this->primary)) $items = array_merge($items, $this->primary);
if (isset($this->secondary)) $items = array_merge($items, $this->secondary);
foreach ($items as $Item) {
$this->item = $item;
$this->loadTemplate('item');
}
?>

Categories