group php foreach loop (Closed) [closed] - php

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

Related

Foreach in foreach [PHP]

Actually I work with PHP framework Codeigniter and I want to compare value from first foreach to second one, but I'm getting an error. Example here:
<?php foreach($posts->result() as $post): ?>
(html content)
<?php foreach($tags->result() as $tag) {
if($tag->id_users == $post->id_users) echo $tag->tag_name;
} ?>
(html content)
<?php endforeach; ?>
When I compare $post->id_users inner second foreach I'm getting error, how can I get around this?
Its better to avoid loop inside a loop
$tag_ids = array();
foreach($tags->result() as $tag) {
$tag_ids[] = $tag->id_users;
}
foreach ($posts->result() as $key => $post) {
if(in_array($post->id_users, $tag_ids)) {
}
}
You don't close the second foreach. For eg
<?php foreach($posts->result() as $post): ?> foreach1
(...some html)
<?php foreach($tags->result() as $tag) { if($tag->id_users == $post->id_users) echo $tag->tag_name; } ?> //foreach2
(...some html)
<?php endforeach; ?>
<?php endforeach; ?>
You should not use $posts->result() and $tags->result() inside foreach loop. Because it will go to check for every time while foreach is live.
Overall it decrease the performance of script.
<?php
$posts = $posts->result();
$tags = $tags->result();
foreach($posts as $post) {
?>
<< Other HTML code goes here >>
<?php
foreach($tags as $tag) {
if($tag->id_users == $post->id_users) {
echo $tag->tag_name;
}
?>
<< Other HTML code >>
<?php
}
}

Embed div elements in php foreach? [closed]

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.

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

Matching up two foreach loops using array value

I have two foreach loops. The first grabs a load of questions from Wordpress, the second is supposed to grab the multiple answers. This is straight forward had it not involved some randomisation of the questions, which makes it confusing.
This is the two foreach loops without them being randomised.
<?php
$repeater = get_field('step_by_step_test');
foreach( $repeater as $repeater_row ){ ?>
<p><?php echo $repeater_row['question']; ?></p>
<?php $rows = $repeater_row['answer_options'];
foreach ($rows as $row){ ?>
<?php echo $row['answer']; ?><br />
<?php } ?>
<?php } ?>
This loops through each question and also grabs the multiple answers.
How can I incorporate it randomising the questions? This is my attempt, this works for getting a random set of questions but I'm getting an error for the answers part (invalid argument supplied for foreach).
<?php
$amount = get_field('select_number_of_questions');
$repeater = get_field('step_by_step_test');
$random_rows = array_rand( $repeater, $amount );
echo implode(', ', $random_rows);
foreach( $random_rows as $repeater_row ){ ?>
<p><?php echo $repeater[$repeater_row]['question']; ?></p>
<?php $rows = get_sub_field('answer_options');
foreach ($rows as $row){ ?>
<?php echo $row['answer']; ?><br />
<?php } ?>
<?php } ?>
I use this plugin for wordpress - http://www.advancedcustomfields.com/
First I'm going to rewrite your first code block to not look like chewed cud.
<?php
$repeater = get_field("step_by_step_test");
foreach($repeater as $repeater_row) {
echo "<p>".$repeater_row['question']."</p>";
$rows = $repeater_row['answer_options'];
foreach($rows as $row) {
echo $row['answer']."<br />";
}
}
?>
And now for the magic: Add shuffle($rows) immediately before the foreach($rows as $row) { line, and the answers will appear in random order.
EDIT in response to comments: Start your code like this:
$repeater = get_field("step_by_step_test");
shuffle($repeater);
$repeater_limit = array_slice($repeater,0,5);
foreach($repeater_limit as $repeater_row) {
....

php foreach : each link in separate variable

I get some links from feed with this code on simplepie :
if ($check) :
foreach ($feed->get_items(0,3) as $item):
$links = $item->get_permalink();
echo $links;
endforeach; endif;
thats result me:
http://link1....
http://link2....
http://link3....
i want to put each link in separate variable like :
$links1 = 'http://link1....';
$links1 = 'http://link2....';
$links1 = 'http://link3....';
thanks , mori
$i = 0;
foreach($feed->get_items(0,3) as $item)) {
${'link' . ++$i} = $item->get_permalink();
}
Maybe what you want is array variable?
try this:
if ($check) :
foreach ($feed->get_items(0,3) as $item):
$links[] = $item->get_permalink();
endforeach; endif;
if ($check) :
$i=0;
foreach ($feed->get_items(0,3) as $item):
$links.$i = $item->get_permalink();
echo $links.$i;
$i++;
endforeach; endif;
I think it may works...

Categories