Looping through database and returning relevant data - php

In my controller I have this Code to loop through database and return the data
$faultgroup = $this->booking_model->Get_Fault_Group_Display($grouptype);
$data['Get_Fault_Group_Display'] = $faultgroup; $getresults = array();
$data['get_fault_group_data'] = array();
foreach ($faultgroup as $key ) {
$show = $key->Showgroup;
$getresults = $this->booking_model->get_fault_group_data($grouptype,$show);
$data['get_fault_group_data'] = $getresults ;
}
In my View i have this Code to loop through each record with the specific grouptype and display record (to_do_item) from database that match that grouptype
<?php if ( ! is_null($Get_Fault_Group_Display)): ?>
<?php if (count($Get_Fault_Group_Display)): ?>
<?php foreach ($Get_Fault_Group_Display as $result): ?>
<?php echo $result->Showgroup; ?>
<?php foreach ($get_fault_group_data as $key) :?>
<?php echo $key->to_do_item; ?>
<?php endforeach ?>
<?php endforeach ?>
<?php else: ?>
<?php endif ?>
My problem is only the last row is shown on all the grouptypes because the loop keeps overiding $data['get_fault_group_data'] with the new $getresults

Shouldn't you use the $data['get_fault_group_data'] as an array?
Controler:
$data['get_fault_group_data'][$key] = $getresults ;
View:
<?php if ( ! is_null($Get_Fault_Group_Display)): ?>
<?php if (count($Get_Fault_Group_Display)): ?>
<?php foreach ($Get_Fault_Group_Display as $i => $result): ?>
<?php echo $result->Showgroup; ?>
<?php foreach ($get_fault_group_data[$i] as $key) :?>
<?php echo $key->to_do_item; ?>
<?php endforeach ?>
<?php endforeach ?>
<?php else: ?>
<?php endif ?>

Related

php structured output result in foreach loop

What i did wrong?
Build array after query:
$arr = array();
while($r = $stmt->fetch(PDO::FETCH_ASSOC)) {
$arr[$r['team1']][$r['stadium']] = array($r['coach'] => $r['age']);
$arr[$r['team2']][$r['stadium']] = array($r['coach'] => $r['age']);
}
Showing data, HOW CAN i Put team 1 and team2? example:
<?php foreach($arr as $team => $stadiums): ?>
<div><?php echo $team;?> AND <?php echo $team;?></div> //----- HERE I NEED PUT TEAM 1 AND TEAM 2
<?php foreach($stadiums as $stadium => $coaches): ?>
<?php foreach($coaches as $choach => $ages): ?>
<?php foreach($stadiums as $stadium): ?>
<div><?php echo $stadium;?></div>
<?php endforeach; ?>
<?php endforeach; ?>
<?php endforeach; ?>
<?php endforeach; ?>
SO hard to understand, how can i write 2 $team in one iteration =)) What i do wrong?

Nesting Foreach loop inside Foreach loop using short_open_tags

I have the following foreach loop:
<?php
$fields = CFS()->get('list-item-field');
?>
<? foreach ($fields as $field) : ?>
<?= $field['list-item-title'] ?>
<? endforeach ?>
And I would like to add another foreach inside the loop, like so:
<?php
$fields = CFS()->get('item-field');
?>
<? foreach ($fields as $field) : ?>
<?= $field['list-item-title'] ?>
<?php
$values = CFS()->get('color');
?>
<? foreach ($values as $value => $label) : ?>
<? echo $value ; ?>
<? endforeach ?>
<? endforeach ?>
However this doesn't work, and I get the error:
Invalid Argument Supplied For Foreach()
Alright I needed to expirement a bit but I figured it out, I doubt this will be helpful to many but regardless here's what I needed to do:
<?php
$fields = CFS()->get('item-field');
?>
<? foreach ($fields as $field) : ?>
<?= $field['list-item-title'] ?>
<? foreach ($field['color'] as $colors => $label) :?>
<? echo $colors ; ?>
<? endforeach ?>
<? endforeach ?>
This post helped: http://customfieldsuite.com/forums/questions/925/loop-within-a-loop

Checking if a variable exists within foreach if statement

In the below code, when checking foreach(get_sub_field("items") as $x):
How do I check if $x['item_name']; is empty, do this...
<?php $food = 23; ?>
<?php while(has_sub_field("menu_items", $food)): ?>
<?php if(get_row_layout() == 'food_items'): ?>
<?php if (get_sub_field('category')){?>
<h2>
<?php the_sub_field('category');?>
</h2>
<?php } ?>
<div>
<?php if(get_sub_field("items")): ?>
<?php foreach(get_sub_field("items") as $x): ?>
<div> <?php echo $x['item_name']; ?> <?php echo $x['item_price']; ?>
<?php endforeach; ?>
<?php endif; ?>
</div>
</div>
<?php endif; ?>
<?php endwhile; ?>
You can try this :
<?php if (empty($x['item_name'])) : ?>
<p>It's empty</p>
<?php endif; ?>
try empty()
<?php echo (!empty($x['item_name']) ? $x['item_name'] : 'do this'); ?>
or
if(!empty($x['item_name'])) {
echo $x['item_name'];
}
else {
// do your stuff
}

Echo an Array Key.. how?

Im looking to echo the value of my array key out into the view for this controller. I cant quite figure it out. So for the first feed I want to echo mmafighting into the of the view. Then it would loop through the rest.
Here is my controller code:
function index()
{
$this->load->library('simplepie');
$feeds = array('mmafighting' => 'http://www.mmafighting.com/rss/current',
'bbc' => 'http://feeds.bbci.co.uk/news/world/us_and_canada/rss.xml',
'bloodyelbow' => 'http://www.bloodyelbow.com/rss',
'ufc' => 'http://www.ufc.com/rss/news',
'hackernews' => 'http://news.ycombinator.com/rss',
'msnbc' => 'http://www.msn.com/rss/news.aspx',
'msnbc2' => 'http://www.msn.com/rss/msnmoney.aspx',
'msnbc3' => 'http://www.msn.com/rss/msnshopping_top.aspx'
);
foreach ($feeds as $site=>$url)
{
$data['feed'][$site] = new SimplePie();
$data['feed'][$site]->set_feed_url($url);
$data['feed'][$site]->set_cache_location(APPPATH.'cache');
$data['feed'][$site]->set_cache_duration(300);
$data['feed'][$site]->init();
$data['feed'][$site]->handle_content_type();
}
$this->load->view('feedsview', $data);
}
Here is my view code:
<?php
$feedCount = 0;
$rowCount = 0;
?>
<?php foreach ($feed as $site): ?>
<?php if ($site->data): ?>
<div class="feed">
<h2><?php // echo original array key here ?></h2>
<ul>
<?php $items = $site->get_items(0, 5); ?>
<?php foreach ($items as $item): ?>
<li><?php echo $item->get_title(); ?></li>
<?php endforeach; ?>
</ul>
</div>
<?php endif; ?>
<?php $feedCount++; ?>
<?php if ($feedCount === 3) {
echo '<div style="clear:both;"> </div>';
$feedCount = 0;
$rowCount++;
if ($rowCount === 2) {
echo '<div style="width:1000px; margin-bottom:20px;height:100px; border:1px solid #252525; float:left;">images</div>';
}
} ?>
<?php endforeach; ?>
I'm not sure i understand but what about doing this :
<?php foreach ($feed as $key => $site): ?>
<?php if ($site->data): ?>
<div class="feed">
<h2><?php echo $key ?></h2>
<ul>

Remove "," at end of foreach loop

I've created a loop to list a set of meta values. I've been able to apply a class to the last item in the list, but I'd like to remove the "," at the end of the last value. Any help would be much appreciated.
<?php $count = count($subcategory); $num = 0; ?>
<?php foreach ($subcategory as $subcategory): ?>
<p
<?php if($num == $count-1){ ?>
class="subcategory-item subcategory-last-item inline-block"
<?php } ?>
class="inline-block subcategory-item"> <?php echo $subcategory;?>,</p>
<?php $num++ ?>
<?php endforeach; ?>
I may be taking an incorrect route by worrying about adding a class to the last item. If I can remove the "," from the last item I'll be happy.
Here's a quick rewrite which may lead you to a solution:
<?php $count = count($subcategories); $num = 0; ?>
<?php $classes = 'inline-block subcategory-item'; ?>
<?php foreach ($subcategories as $subcategory): ?>
<p class="<?=$classes.($num==$count-1?' subcategory-last-item':'')?>">
<?php echo $subcategory;?>
<?php if ($num<$count-1): ?>
,
<?php endif; ?>
</p>
<?php $num++ ?>
<?php endforeach; ?>

Categories