Bootstrap columns in loop with php - php

I'm trying to show my news block with bootstrap4 as
<div class="col-md-3">
<div class="card mb-4">
<img src="$arItem["DETAIL_PICTURE"]["SRC"]"
</div>
<div class="card mb-4">
<img src="$arItem["DETAIL_PICTURE"]["SRC"]"
</div>
</div>
<div class="col-md-3">
<div class="card mb-4">
<img src="$arItem["DETAIL_PICTURE"]["SRC"]"
</div>
<div class="card mb-4">
<img src="$arItem["DETAIL_PICTURE"]["SRC"]"
</div>
</div>
<div class="col-md-6">
<div class="card mb-big">
<img src="$arItem["DETAIL_PICTURE"]["SRC"]"
</div>
</div>
I have an array with results of database query in
$arResult["ITEMS"]
I want to do this with loop by
<? foreach ($arResult["ITEMS"] as $arItem): ?>
<?endforeach?>
I know i need to use something like
$i=0
if (i===2){
} else if ($k === 4) {
} else {}
$i++;
but how can i remove col-md-3 from loop.
<?php
$i=0
foreach ($arResult["ITEMS"] as $arItem): ?>
<div class="col-md-3">
<div class="card mb-4"></div>
</div>
if (i===2){
<div class="col-md-3">
<div class="card mb-4"></div>
</div>
}
if (i===4){
<div class="col-md-6">
<div class="card mb-4"></div>
</div>
}
I'm afraid I'm a little confused. How to show it in one loop?

Assuming you have 5 elements in the array, you could use %2 (modulo) to write <div class="col-md-3"> and check if it's the last element to change col-md-3 to col-md-6 and mb-4 to mb-big:
if (!empty($arResult["ITEMS"])) {
foreach ($arResult["ITEMS"] as $k => $item) {
$is_last = $k == count($arResult["ITEMS"]) - 1;
if ($k%2 == 0) {
echo '<div class="'.($is_last?'col-md-6':'col-md-3').'">';
}
echo' <div class="card '.($is_last?'mb-big':'mb-4').'"></div>';
if ($k%2 == 1) echo '</div>';
}
echo '</div>';
}
Will output (reformatted):
<div class="col-md-3">
<div class="card mb-4"></div>
<div class="card mb-4"></div>
</div>
<div class="col-md-3">
<div class="card mb-4"></div>
<div class="card mb-4"></div>
</div>
<div class="col-md-6">
<div class="card mb-big"></div>
</div>

Related

how to hide a div in first loop and keep showing in others

i have a php loop in my view, that i am showing data from database. i want to disable one div that has citizenship class in first loop and in next all loops it should show, how to do this?
<?php
$i = 1;
foreach($appdetails2 as $appdetails2n){ ?>
<div class="row">
<div class="col-md-4 col-xs-5 text-md-right text-sm-right" data-xtr-key="Passport number">Passport Expiry</div>
<div class="col-md-7 col-xs-7"><strong><?php echo $appdetails2n['ped'];?></strong></div>
</div>
<!-- to be hidden in first loop-->
<div class="row citizenship">
<div class="col-md-4 col-xs-5 text-md-right text-sm-right" data-xtr-key="Citizenship">Citizenship</div>
<div class="col-md-7 col-xs-7"><strong><?php echo $appdetails2n['citizenship'];?></strong></div>
</div>
</div>
<br>
<?php } ?>
i have tried this but its not working.
<div class="row citizenship" <?php if ($appdetails2n['citizenship']===NULL){?>style="display:none"<?php } ?>>
<div class="col-md-4 col-xs-5 text-md-right text-sm-right" data-xtr-key="Citizenship">Citizenship</div>
<div class="col-md-7 col-xs-7"><strong><?php echo $appdetails2n['citizenship'];?></strong></div>
</div>
TRY
<?php
$i =0;
foreach($appdetails2 as $appdetails2n) { ?>
<div class="row">
<div class="col-md-4 col-xs-5 text-md-right text-sm-right" data-xtr-key="Passport number">Passport Expiry</div>
<div class="col-md-7 col-xs-7"><strong><?php echo $appdetails2n['ped'];?></strong></div>
</div>
<!-- to be hidden in first loop-->
<?php
if($i>0)
{ ?>
<div class="row citizenship">
<div class="col-md-4 col-xs-5 text-md-right text-sm-right" data-xtr-key="Citizenship">Citizenship</div>
<div class="col-md-7 col-xs-7"><strong><?php echo $appdetails2n['citizenship'];?></strong>
</div>
</div>
</div>
<?php } ?>
<br>
<?php $i=$i+1; } ?>
You can try this:
<?php
$i = 1;
foreach($appdetails2 as $appdetails2n) { ?>
<div class="row">
<div class="col-md-4 col-xs-5 text-md-right text-sm-right" data-xtr-key="Passport number">Passport Expiry</div>
<div class="col-md-7 col-xs-7"><strong><?php echo $appdetails2n['ped'];?></strong></div>
</div>
<!-- to be hidden in first loop-->
<?php if($i !== 1) { ?>
<div class="row citizenship">
<div class="col-md-4 col-xs-5 text-md-right text-sm-right" data-xtr-key="Citizenship">Citizenship</div>
<div class="col-md-7 col-xs-7"><strong><?php echo $appdetails2n['citizenship'];?></strong></div>
</div>
<?php } ?>
<br>
<?php $i++; } ?>
<?php
$first_key = array_key_first($appdetails2);
foreach($appdetails2 as $key => $appdetails2n) { ?>
<div class="row">
<div class="col-md-4 col-xs-5 text-md-right text-sm-right" data-xtr-key="Passport number">Passport Expiry</div>
<div class="col-md-7 col-xs-7"><strong><?php echo $appdetails2n['ped'];?></strong></div>
</div>
<!-- to be hidden in first loop-->
<?php if($key != $first_key) { ?>
<div class="row citizenship">
<div class="col-md-4 col-xs-5 text-md-right text-sm-right" data-xtr-key="Citizenship">Citizenship</div>
<div class="col-md-7 col-xs-7"><strong><?php echo $appdetails2n['citizenship'];?></strong></div>
</div>
<?php } ?>
<?php } ?>
<br>
You can also set display none with that condition!

Increase loop by two and loop into loop

I have an HTML markup like bellow-
<div class="f_product_slider slick">
<div class="row slider_item">
<div class="col-lg-5 col-sm-6">
<div class="item">
<img src="image/bike/cycle_2.png" alt="">
<div class="content text-right">
<h6>Specialized Sirrus Carbon - 2018</h6>
<p>Slayer Bike Expert</p>
</div>
</div>
</div>
<div class="col-lg-5 col-sm-6">
<div class="item item_two">
<img src="image/bike/cycle_2.png" alt="">
<div class="content text-right">
<h6>Specialized Sirrus Carbon - 2018</h6>
<p>Slayer Bike Expert</p>
</div>
</div>
</div>
</div>
<div class="row slider_item">
<div class="col-lg-5 col-sm-6">
<div class="item">
<img src="image/bike/cycle_2.png" alt="">
<div class="content text-right">
<h6>Specialized Sirrus Carbon - 2018</h6>
<p>Slayer Bike Expert</p>
</div>
</div>
</div>
<div class="col-lg-5 col-sm-6">
<div class="item item_two">
<img src="image/bike/cycle_2.png" alt="">
<div class="content text-right">
<h6>Specialized Sirrus Carbon - 2018</h6>
<p>Slayer Bike Expert</p>
</div>
</div>
</div>
</div>
</div>
Here, every row items containing two items. That's mean, every two loop items rendered in every step of the loop.
I'm giving here a visual example of this loop-
How can I make it possible with PHP while loop?
Please, don't hesitate to ask me for more details if you get confused by the question.
With for:
<div class="f_product_slider slick">
<?php for($slideCounter = 0; $slideCounter < 2; $slideCounter++) { ?>
<div class="row slider_item">
<?php for($colCounter = 0; $colCounter < 2; $colCounter++) { ?>
<div class="col-lg-5 col-sm-6">
<div class="item">
<img src="image/bike/cycle_2.png" alt="">
<div class="content text-right">
<h6>Specialized Sirrus Carbon - 2018</h6>
<p>Slayer Bike Expert</p>
</div>
</div>
</div>
<?php } ?>
</div>
<?php } ?>
</div>
With while:
<div class="f_product_slider slick">
<?php
$slideCounter = 0;
while($slideCounter < 2) {
$slideCounter++ ?>
<div class="row slider_item">
<?php
$colCounter = 0;
while($colCounter < 2) {
$colCounter++ ?>
<div class="col-lg-5 col-sm-6">
<div class="item">
<img src="image/bike/cycle_2.png" alt="">
<div class="content text-right">
<h6>Specialized Sirrus Carbon - 2018</h6>
<p>Slayer Bike Expert</p>
</div>
</div>
</div>
<?php } ?>
</div>
<?php } ?>
</div>

Move the widget to the right side of the web page using bootstrap 4

I want the information tag to be at the right side of the web page. Rest of the things are in the middle of the page. While I am not able to move the information to the right side. Also, because of it the footer disappeared. While I remove the information code, the footer works. Please help me right-align the information widget and also have the footer too.
<div class="container">
<div class="card border-light mb-3 text-center">
<p class="card-header">Videos</p>
</div>
<div id="userSuccess" class="hide" role="alert">
<div id="successUserContent"></div>
</div>
<div class="row" id="userVid">
<?php
$allVid = Schema::getAll();
for ($i = 0; $i < count($allVid); $i++) {
echo <<<HTML
<div class="col-lg-4 col-md-6 mb-4">
<div class="card h-100">
<div class="card-body">
<img class="card-img-top" src="http://placehold.it/700x400" alt="">
<h4 class="card-title">{$allVid[$i]->getTitle()} </h4>
</div>
</div>
</div>
HTML;
}
?>
</div>
<div class="row">
<div class="col-md-4 order-md-2 mb-4">
<div class="card my-4">
<h5 class="card-header">Information</h5>
<div class="card-body">
<div class="row">
<ul class="list-unstyled mb-0">
<li>
...
</li>
</ul>
</div>
</div>
</div>
</div>
</div>
</div>
<?php
require_once './page/footer.php';
?>
For right align, you'll want to add the class of "justify-content-end" after your "row" class.
<div class="row justify-content-end"><!-- ADDED HERE-->
<div class="col-md-4 order-md-2 mb-4">
<div class="card my-4">
<h5 class="card-header">Information</h5>
<!-- REST OF YOUR CODE-->
For your footer, you'll need to wrap your URL in parenthesis.
<?php require_once('/yourdirectory/yourfooter.php'); ?>

Inserting image array into complex structure using HTML

I have an array image: lstImageSlider.
I need a loop to foreach that array and insert it into the HTML page.
The HTML code from a theme has structure that is difficult to insert.
This is because it has slider__item--0 - 1 - 2 - 3. Loop this.
I can set this code to foreach $lstImageSlider as $item.
``````````````````````
` thumbnail `
``````````````````````
` image 1 ` image 2 `
``````````````````````
HTML code:
<div class="property__ribon">transaction-related</div>
<div id="properties-thumbs" class="slider slider--small js-slider-thumbs">
<div class="slider__block js-slick-slider">
<div class="slider__item--0"><span>Awesome Kitchen!!!</span></div>
<div class="slider__item--1"><span>2</span></div>
<div class="slider__item--2"><span>3</span></div>
<div class="slider__item--3"><span>Ok</span></div>
<div class="slider__item--0"><span>5</span></div>
<div class="slider__item--1"><span>6</span></div>
<div class="slider__item--2"><span>7</span></div>
<div class="slider__item--3"><span>8</span></div>
<div class="slider__item--0"><span>The end</span></div>
</div>
</div>
<div class="slider slider--thumbs">
<div class="slider__wrap">
<div class="slider__block js-slick-slider">
<div data-slide-rel='0' class="slider__item--0">
<div class="slider__img"><img src="assets/img/lazy-image.jpg" alt=""></div>
</div>
<div data-slide-rel='1' class="slider__item--1">
<div class="slider__img"><img src="assets/img/lazy-image.jpg" alt=""></div>
</div>
<div data-slide-rel='2' class="slider__item--2">
<div class="slider__img"><img src="assets/img/lazy-image.jpg" alt=""></div>
</div>
<div data-slide-rel='3' class="slider__item--3">
<div class="slider__img"><img src="assets/img/lazy-image.jpg" alt=""></div>
</div>
<div data-slide-rel='4' class="slider__item--0">
<div class="slider__img"><img src="assets/img/lazy-image.jpg" alt=""></div>
</div>
<div data-slide-rel='5' class="slider__item--1">
<div class="slider__img"><img src="assets/img/lazy-image.jpg" alt=""></div>
</div>
<div data-slide-rel='6' class="slider__item--2">
<div class="slider__img"><img src="assets/img/lazy-image.jpg" alt=""></div>
</div>
<div data-slide-rel='7' class="slider__item--3">
<div class="slider__img"><img src="assets/img/lazy-image.jpg" alt=""></div>
</div>
<div data-slide-rel='8' class="slider__item--0">
<div class="slider__img"><img src="assets/img/lazy-image.jpg" alt=""></div>
</div>
</div>
</div>
</div>
</div>
i am not sure which you want , but hope it will help you, create counter and if counter >= 3 then set 0
<div class="property__ribon">transaction-related</div>
<div id="properties-thumbs" class="slider slider--small js-slider-thumbs">
<div class="slider__block js-slick-slider">
<?php
$first_counter = 0;
foreach ($lstImageSlider as $item) {
?>
<div class="slider__item--<?=$first_counter?>"><a href="src/image.jpg" data-gallery-index='<?=$first_counter?>'><img
src="assets/img/lazy-image.jpg" alt=""><span>Awesome Kitchen!!!</span></a></div>
<?php
$first_counter++;
if ($first_counter >= 3) {
$first_counter = 0;
}
}
?>
</div>
</div>
<div class="slider slider--thumbs">
<div class="slider__wrap">
<div class="slider__block js-slick-slider">
<?php
$counter = 0;
foreach ($lstImageSlider as $item) {
?>
<div data-slide-rel='<?= $counter ?>' class="slider__item--<?= $counter ?>">
<div class="slider__img"><img src="assets/img/lazy-image.jpg" alt=""></div>
</div>
<?php
$counter++;
if ($counter >= 3) {
$counter = 0;
}
}
?>
</div>
</div>
</div>
hope it will help you, any confusion then inform me
It's not clear what you're doing with the items, I assume you're updating the document with js? Or you could output your lines of HTML within this loop. Anyway, this is how you can loop over your items and get the correct classname at the same time:
for($i=0; $i < count($lstImageSlider); $i++){
$j = $i % 3;
$item = $lstImageSlider[$i];
$classname = "slider__item--".$j;
}

CakePHP: passing a variable to a view

I basically want to echo the number of articles on the articles index template page. I have a method in the controller. How do I echo the result of that method in the index page?
Controller
public function articleCount()
{
$article_count = $this->Articles->find('all')->count();
$this->set('article_count',$article_count );
}
index
<div class="col-xs-12 col-md-6 col-lg-3 widget">
<div class="panel panel-red panel-widget">
<div class="row no-padding">
<div class="col-sm-3 col-lg-5 widget-left">
</div>
<div class="col-sm-9 col-lg-7 widget-right">
<div class="large"><?= echo article count here! ?></div>
<div class="text-muted">Categories</div>
</div>
</div>
</div>
</div>
Im fairly new to cakephp and would appreciate any help you can give
The code you used needs to be in the index action, in the Controller:
public function index()
{
// ...
$article_count = $this->Articles->find('all')->count();
$this->set('article_count',$article_count );
// ...
}
Once you've done that, you can use the $article_count variable:
<div class="col-xs-12 col-md-6 col-lg-3 widget">
<div class="panel panel-red panel-widget">
<div class="row no-padding">
<div class="col-sm-3 col-lg-5 widget-left">
</div>
<div class="col-sm-9 col-lg-7 widget-right">
<div class="large"><?= echo $article_count ?></div>
<div class="text-muted">Categories</div>
</div>
</div>
</div>
</div>

Categories