How to make 2 columns in loop data - php

I do foreach loop to output my data so my data will be like this
Dog
Cat
Mouse
Bird
Egg
Eagle
Fish
using this code
<div class="col-md-6">
<?php foreach ($treefields as $key=>$item): ?>
<?php if($key==0);?>
<?php if($item['title'] == $estate_data_option_1057): ?>
<div class="additional-amenities">
<span class="available"><i class="fa fa-check-square"></i></span><strong> <?php echo $options_name_1057; ?>:</strong><?php echo $estate_data_option_1057;?></span>
</div>
<?php if (count($item['childs_4']) > 0) foreach ($item['childs_4'] as $child): ?>
<div class="additional-amenities">
<span class="available"><i class="fa fa-check-square"></i></span><strong><?php _che($child['description']); ?></strong><span><?php echo $child['title']; ?></span>
</div>
<?php endforeach; ?>
<?php else: ?>
<?php ?>
<?php endif;?>
<?php endforeach; ?> </div></div>
How can i make it 2 columns like this?
- Dog - Egg
- Cat - Eagle
- Mouse - Fish
- Bird

First count the number of items in your array, then figure out what half of that would be and ceil it, so it's not a float. Then use that value in a conditional to echo your markup to break it into multiple columns.
For example, here is the technique boiled down to put the array into two different ul elements.
<ul>
<?php
$animals = array('Dog','Cat','Mouse','Bird','Egg','Eagle','Fish');
$numAnimals = count($animals);
$maxAnimalsPerColumn = ceil($numAnimals/2);
for($i=0; $i < $numAnimals; $i++) {
echo "<li>".$animals[$i]."</li>";
if ($i+1 == $maxAnimalsPerColumn ) {
echo "</ul><ul>";
}
}
?>
</ul>
$numAnimals would be 7
$maxAnimalsPerColumn would be 4 ($numAnimals divided by 2, ceil'd)
When the loop value (plus 1 since it starts at zero) matches $maxAnimalsPerColumn, it will echo a closing ul tag and a new one to start the second.
The resulting markup would be roughly:
<ul>
<li>Dog</li>
<li>Cat</li>
<li>Mouse</li>
<li>Bird</li>
</ul>
<ul>
<li>Egg</li>
<li>Eagle</li>
<li>Fish</li>
</ul>

Use a counter. Example:
$nbrOfColumns = 2;
$rowsPerColumn = ceil(count($treefields)/$nbrOfColumns);
$counter = 1;
<div class="col-md-6">
foreach ($treefields as $key=>$item){
if($counter === $rowsPerColumn){
echo "</div><div class="col-md-6">";
$counter = 0;
}
(generate your HTML here)
$counter++;
}
</div>

Related

Dynamically give list items <li> a unique class with PHP

I am dynamically populating a list based on what is in the database, and I would like each list item to have a unique class name - something simple, for example the first would have the class item-1, and the second would have the class item-2.
This is the php code I am using to create the list:
<?php
$metas=trim(get_post_meta($post->ID,'hike_meta',true),'');
$metas_arr=explode("\n",$metas);
$metas1=array_slice($metas_arr,0,3);
$metas2=array_slice($metas_arr,3,3);
?>
<div class="loca_meta">
<ul>
<?php foreach($metas1 as $meta){
list($k, $v) = explode('|', $meta);
echo "<li><span class='bold'>$k</span>:<br>$v</li>";
}?>
</ul>
</div>
<div class="loca_meta">
<ul>
<?php foreach($metas2 as $meta){
list($k, $v) = explode('|', $meta);
echo "<li><span class='bold'>$k</span>:<br>$v</li>";
}?>
</ul>
</div>
What extra code do I need, to give each list item a unique class? As a side note, I am very much a beginner when it comes to php, so please let me know if you need any more information.
You can add $counter variable to get the incremented value and assign it to class.
<div class="loca_meta">
<ul>
<?php $counter = 0; foreach($metas1 as $meta){
list($k, $v) = explode('|', $meta);
echo "<li><span class='bold <?php echo "item_".$counter ?>'>$k</span>:<br>$v</li>";
$counter++;
}?>
</ul>
</div>
<div class="loca_meta">
<ul>
<?php foreach($metas2 as $meta){
list($k, $v) = explode('|', $meta);
echo "<li><span class='bold <?php echo "item_".$counter ?>'>$k</span>:<br>$v</li>";
$counter++;
}?>
</ul>
</div>
You can use for loop:
<?php for($i = 0; $i < count($metas1); $i++){
list($k, $v) = explode('|', $metas[$i]);
echo "<li><span class="bold item-".$i+1.">$k</span>:<br>$v</li>";
}?>
Result will be:
<li><span class="bold item-1"><!-- content --></span>:<br><!-- content --></li>
<li><span class="bold item-2"><!-- content --></span>:<br><!-- content --></li>
<li><span class="bold item-3"><!-- content --></span>:<br><!-- content --></li>
and so on.

PHP loop rating bar error

I have a 5 point rating system I am trying to change up how it displays the rating.
So before it would be 5 dots all white if not rated, then color in the ratings blue.. I would display these dots via fontawesome glyphs. So it would output 5 of them.
But now i'm trying to change it where the 5 point rating would display a progress bar and the 5 points be used as percentages for the length of progress.
Sorry if i am vague, I can explain more if needed. It currently displays all the bars and rating values, but just once. So it's not functioning the way I want it to.
<?php if(count($languages) > 0) { ?>
<div class="col-md-6">
<ul class="no-bullets">
<?php foreach($languages as $index => $language) { ?>
<li>
<span class="skillset-title"><?= $language->title; ?> (<?= $language->endorsement; ?>)</span>
<span class="skillset-rating">
<?php
$levelpercentage = 0;
for($stars == 1; $stars <= 5; $stars++) {
if ($stars == 5):
echo $levelpercentage = 100;
elseif ($stars == 4):
echo $levelpercentage = 80;
elseif ($stars == 3):
echo $levelpercentage = 60;
elseif ($stars == 2):
echo $levelpercentage = 40;
elseif ($stars == 1):
echo $levelpercentage = 20;
endif;
?>
<div class="progress-bar blue stripes">
<span style="width: <?= ($language->level >= $stars) ? $levelpercentage : '0'; ?>%;"></span>
</div>
<?php } ?>
</span>
</li>
<?php if(ceil(count($languages) / 2) == $index + 1) { ?>
</ul>
</div>
<div class="col-md-6">
<ul class="no-bullets">
<?php } ?>
<?php } ?>
</ul>
</div>
<?php } else { ?>
<div class="alert alert-warning">
No languages were found!
</div>
<?php } ?>
It currently displays all the bars and rating values
That's because you assign and echo your bars and rating values in a for loop.
Your $stars variable is assigned every value from 1 to 5, displaying the bar at each step. Simply remove your for loop and it should work fine (provided you use an already defined $stars variable)
Bonus : Because I'm in a good mood, I have to tell you there's a simpler way to build your $levelpercentage variable than using an if...else for each value from 1 to 5, just use :
$levelpercentage = $stars * 20;
Thank you! That really helped me out. Was trying to simplify what I was trying to do. This is what I did and it seems to be working.
<?php if(count($languages) > 0) { ?>
<div class="col-md-6">
<ul class="no-bullets">
<?php foreach($languages as $index => $language) { $stars = 0; $stars <= 5; $stars++; ?>
<li>
<span class="skillset-title"><?= $language->title; ?> (<?= $language->endorsement; ?>)</span>
<span class="skillset-rating">
<div class="progress-bar blue stripes">
<span style="width: <?= ($language->level >= $stars) ? $language->level * 20 : 0; ?>%;"></span>
</div>
</span>
</li>
<?php if(ceil(count($languages) / 2) == $index + 1) { ?>
</ul>
</div>
<div class="col-md-6">
<ul class="no-bullets">
<?php } ?>
<?php } ?>
</ul>
</div>
<?php } else { ?>
<div class="alert alert-warning">
No languages were found!
</div>
<?php } ?>

Place DIV's in a containing DIV based on a numeric value

Ive got the follow PHP:
<div class="slide-background">
<div class="slide">
<?php foreach (array_chunk($items->submenu, $linkCount) as $items): ?>
<?php if (12 / $cols == 1):?>
<div class="col-md-12">
<?php else: ?>
<div class="col-md-<?php echo 12 / $cols; ?>">
<?php endif; ?>
<ul>
<?php foreach($items as $submenu): ?>
<?php echo $submenu; ?>
<?php endforeach; ?>
</ul>
</div>
<?php endforeach; ?>
</div>
<ul class="pager">
<li>prev</li>
<li>next</li>
</ul>
</div>
</div>
basically it calculates how many links to display and how many columns, but i now need to place the links in <div class="slide"></div>, but based on the columns.. so basically i need to say if $cols = 2 place two div's in a div and close.. so its basically how many every $cols it should place so many div's in that div..
Its Confusing for me to even explain.. I think Ive explained it rather well above.. If not place say so and ill try again..
Any Help Greatly Appreciated..
UPDATE:
thanks to Hans ive now have the following:
<?php $linksPerColumn = ceil($linkCount / $cols); $linkCounter = 0;?>
<div class="slide-background">
<div class="slide">
<div class="col-md-<?php echo 12 / $cols ?>">
<ul>
<?php foreach ($items->submenu as $link): ?>
<?php $linkCounter++;?>
<?php if($linkCounter % $linksPerColumn == 0):?>
</ul>
</div>
<div class="col-md-<?php echo 12 / $cols ?>">
<ul>
<?php endif; ?>
<?php echo $link; ?>
<?php endforeach; ?>
</ul>
</div>
</div>
<ul class="pager">
<li>prev</li>
<li>next</li>
</ul>
</div>
</div>
only problem is when there's only one column and i need 2 links and then for it to close the div and the ul and start new ones.. right now it does that except for everyone and not for every two links...
You could use modulus for this one. You should calculate how many items you need per column. And then create a close and open div for example:
<?
$linksPerColumn = 4;
if ($linkCount > 4){
$linksPerColumn = ceil ($linkCount / $amountOfColums);
}
$linkCounter = 0;
?>
<div class="slide-background">
<div class="slide">
<?
foreach ($links as $link)
{
$linkCounter++;
?>
// Do your HTML Here.
<?
if($linkCounter % $linksPerColumn = 0)
{
?>
</div>
<div class="slide">
<?
}
?>
</div>
</div>
// Rest of the HTML here.
I think this should do the trick for you.

create new ul after every 13 li elements

First of all, I don't know how to count in php, maybe someone could recommend me a good source to read;
Second, I'm not asking to solve this for me, but I just want a hint or simpler explanation that would make sense;
Here is my function:
<ul>
<?php foreach ($categories as $category) { ?>
<li>
<p><?php echo $category['name']; ?></p>
<?php if ($category['children']) { ?>
<div>
<?php for ($i = 0; $i < count($category['children']);) { ?>
<?php $j = $i + ceil(count($category['children']) / $category['column']); ?>
<?php for (; $i < $j; $i++) { ?>
<?php if (isset($category['children'][$i])) { ?>
<ul>
<li class="none"><h1><?php echo $category['children'][$i]['name']; ?></h1></li>
<?php if ($category['children'][$i]['children_level2']) { ?>
<?php for ($wi = 0; $wi < count($category['children'][$i]['children_level2']); $wi++) { ?>
<li>
<?php echo $category['children'][$i]['children_level2'][$wi]['name']; ?>
</li>
<?php } ?>
<?php } ?>
</ul>
<?php } ?>
<?php } ?>
<?php } ?>
</div>
<?php } ?>
</li>
<?php } ?>
</ul>
I want this part, every 13 li elements to create a new .ul. ./ul. tags
<?php if (isset($category['children'][$i])) { ?>
<ul>
<li class="none"><h1><?php echo $category['children'][$i]['name']; ?></h1></li>
<?php if ($category['children'][$i]['children_level2']) { ?>
<?php for ($wi = 0; $wi < count($category['children'][$i]['children_level2']); $wi++) { ?>
<li>
<?php echo $category['children'][$i]['children_level2'][$wi]['name']; ?>
</li>
<?php } ?>
<?php } ?>
</ul>
<ul><li>maximum of 13 elements</li></ul>
after 13 <li></li> elements create new <ul></ul> tags and put the 14 <li></li> element into the new <ul></ul> tag
I hope I explained what I want to do, for now I'll be waiting for your answers,
p.s. this is more for my learning skills then actual work, so thanks in advice
It's simple. Use condition
if ($wi && $wi % 13 == 0) {
print '</ul><ul>';
}
In order to count 13 elements, you can use the modulo operator %. The idea is that ($iw % 13) is equal to 0 if the content of $iw is a multiple of 13. (see more about the modulo operation)
With an if statement you can insert </ul><ul> when you need in your for loop.
There's two possibilities I can think of:
you could have a separate counter wich resets to 1 and outputs a <ul> every time it reaches 13
or you could check each time if $wi id divisible by 13:
<?php
for ($wi = 0; $wi < count($category['children'][$i]['children_level2']); $wi++) {
if ($wi && $wi % 13 == 0) {
// output <ul>
}
}
?>
If you're unsure what % means, here's the appropriate PHP manual page

How to show a single Item in cakePHP

I am using cake PHP and on my view/technical_slider/index.ctp is the following:
I am using a heavily modified version of this tutorial Featured Content Slider Using jQuery
<?php foreach ($technicalSlides as $technicalSlide):?>
<div id="nav-fragment-1" class="ui-tabs-panel">
<h2><?php echo $technicalSlide['TechnicalSlide']['title'];?></h2>
<p><?php echo $technicalSlide['TechnicalSlide']['description'] ; ?></p>
</div>
<?php endforeach; ?>
My question is: How can I display only certain id's or items and apply different class on the same div ? Because I only want record 1-3 skip 4-7 then show record 8-15?
I am applying different classes on them because they have different backgrounds, but share the same id.
You coud add a counter and then display only those you want.
<?php int $i = 0; ?>
<?php foreach ($technicalSlides as $technicalSlide):?>
<?php if ($i < 4 || $i > 7): ?>
<div id="nav-fragment-1" class="ui-tabs-panel">
<h2><?php echo $technicalSlide['TechnicalSlide']['title'];?></h2>
<p><?php echo $technicalSlide['TechnicalSlide']['description'] ; ?></p>
<?php endif; ?>
</div>
<?php endforeach; ?>
Although I would merge all classes and use attributes.
Add a counter variable (in this case $i), increment it ($i++) so it increases by one for every loop, and add your checks to an if() block to make sure you only write the ones you want to:
<?php
$i = 1;
foreach ($technicalSlides as $technicalSlide):
if($i < 4 || $i > 7) {
?>
<div id="nav-fragment-1" class="ui-tabs-panel">
<h2><?php echo $technicalSlide['TechnicalSlide']['title'];?></h2>
<p><?php echo $technicalSlide['TechnicalSlide']['description'] ; ?></p>
</div>
<?php
}
$i++;
endforeach;
?>

Categories