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;
}
Related
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>
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>
I am using simple_html_dome and I want to grab experience, education, and title.
By my code, I am getting (Call to a member function find() on null in ) error message but title is showing error is ($notrmjobs = $item->find('div[class=norm-jobs-wrapper]',0);) here.
What is the core rules to catch HTML tag by find method?
<div class="boxed">
<div class="row">
<div class="col-md-12">
<div class="norm-jobs-wrapper" onclick="DivOpen('id=734675&fcatId=1&ln=1');">
<div class="row">
<div class="col-sm-3 col-sm-push-3">
<!--<div class="row">
<div class="col-sm-12">
<div class="comp_logo"><img src="images/38951.jpg" alt=""></div>
</div>
</div>-->
</div>
<div class="col-sm-9 col-sm-pull-9">
<div class="row">
<div class="col-sm-12">
<div class="comp-name-text">GBA Techno Pvt. Ltd.</div>
</div>
<div class="col-sm-12">
<div class="job-title-text">
<a onclick="clickJObTitle()" target="_blank" href="jobdetails.asp?id=734675&fcatId=1&ln=1">
Sr. Executive, Accounts
</a>
</div>
</div>
<div class="col-sm-12">
<div class="edu-text">
<div class="row">
<div class="col-sm-2">
<div class="edu-text-s">
Education:
</div>
</div>
<div class="col-sm-10">
<div class="edu-text-d">
Masters/ MBA in Accounts or Finance from any reputed university
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<div class="col-sm-12">
<div class="row">
<div class="col-sm-9">
<div class="exp-text">
<div class="row">
<div class="col-sm-2">
<div class="exp-text-s">Experience:</div>
</div>
<div class="col-sm-10">
<div class="exp-text-d">
At least 3 year(s)
</div>
</div>
</div>
</div>
</div>
<div class="col-sm-3">
<div class="dead-text">
<div class="dead-text-s">Deadline: </div>
<div class="dead-text-d">
<strong>Dec 13, </strong>2017
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<?php
include 'simple_html_dom.php';
$html = file_get_html('http://jobs.bdjobs.com/jobsearch.asp?fcatId=1&icatId=');
$boxed = $html->find('div[class=boxed]',0);
$raw = $boxed->find('div[class=row]');
foreach($raw as $key => $loop){
$item = $loop->find('div[class=col-md-12]',0);
$notrmjobs = $item->find('div[class=norm-jobs-wrapper]',0);
$raw = $notrmjobs->find('div[class=row]',0);
$sdivice = $raw->find('div[class=col-sm-9 col-sm-pull-9]',0);
$sraw = $sdivice->find('div[class=row]',0);
$asraw = $sraw->find('div[class=col-sm-12]',0);
$title = $asraw->find('div[class=comp-name-text]',0)->plaintext;
echo $title;
}
?>
i using slider fetch MYSQL records i using while function i need only active class in first item only. i need only active class in loop first element.how to solve this problem. i using this code but can't change the item.
mycode
<?php
$qry = MYSQL_QUERY("SELECT * FROM `post` WHERE `post_type`='events' AND `post_event_start_date` <= CURDATE() ORDER BY `post_event_start_date`");
?>
<?php while( $row = MYSQL_FETCH_OBJECT($qry) ){ ?>
<div class="item <?php if($i==0) { ?> active<?php } ?>">
<ul class="thumbnails">
<li class="col-xs-12 col-lg-12 col-md-12 col-sm-12">
<div class="fff">
<div class="thumbnail">
<img src="administrator/slide_name/large/slide_1.jpg" alt="">
</div>
<div class="col-lg-12 well">
<div class="form-group">
<h3><?php print_r($row->post_title); ?></h3>
</div>
<div class="form-group">
<h4><?php print_r($row->post_url); ?></h4>
<h5></h5>
</div>
<div class="form-group">
<div class="row">
<div class="col-lg-6 col-md-6 col-sm-12 col-xs-12">
<center><p>Start Date & Time</p><?php print_r($row->post_event_start_date); ?></center>
</div>
<div class="col-lg-6 col-md-6 col-sm-12 col-xs-12">
<center><p>Start End & Time</p><?php print_r($row->post_event_end_date); ?></center>
</div>
</div>
</div>
<div class="form-group">
<p>Location:</p>
<p><?php print_r($row->post_maplocation); ?></p>
</div>
<div class="form-group">
<p>Description: </p>
<p><?php print_r($row->post_desc); ?></p>
</div>
<div class="row pull-right">
<a class="btn btn-mini" href="#">» Read More</a>
<a class="btn btn-mini" href="#">» Share it</a>
</div>
</div>
</div>
</li>
</ul>
</div>
<?php } ?>
Replace
<?php while( $row = MYSQL_FETCH_OBJECT($qry) ){ ?>
<div class="item <?php if($i==0) { ?> active<?php } ?>">
With
<?php
$i=0;
while( $row = MYSQL_FETCH_OBJECT($qry) ){ ?>
<div class="item <?php if($i==0) {
$i=1;
echo 'active'; } ?>">
Try this.
$qry = MYSQL_QUERY("SELECT * FROM 'post' WHERE 'post_type'='events' AND 'post_event_start_date' <= CURDATE() ORDER BY 'post_event_start_date'");
$first_item = true;?>
<?php while( $row = MYSQL_FETCH_OBJECT($qry) ){ ?>
<div class="item <?php echo $first_item? 'active': ''; ?>"><?php $first_item = false; ?>
<ul class="thumbnails">
<li class="col-xs-12 col-lg-12 col-md-12 col-sm-12">
<div class="fff">
<div class="thumbnail">
<img src="administrator/slide_name/large/slide_1.jpg" alt="">
</div>
<div class="col-lg-12 well">
<div class="form-group">
<h3><?php print_r($row->post_title); ?></h3>
</div>
<div class="form-group">
<h4><?php print_r($row->post_url); ?></h4>
<h5></h5>
</div>
<div class="form-group">
<div class="row">
<div class="col-lg-6 col-md-6 col-sm-12 col-xs-12">
<center><p>Start Date & Time</p><?php print_r($row->post_event_start_date); ?></center>
</div>
<div class="col-lg-6 col-md-6 col-sm-12 col-xs-12">
<center><p>Start End & Time</p><?php print_r($row->post_event_end_date); ?></center>
</div>
</div>
</div>
<div class="form-group">
<p>Location:</p>
<p><?php print_r($row->post_maplocation); ?></p>
</div>
<div class="form-group">
<p>Description: </p>
<p><?php print_r($row->post_desc); ?></p>
</div>
<div class="row pull-right">
<a class="btn btn-mini" href="#">» Read More</a>
<a class="btn btn-mini" href="#">» Share it</a>
</div>
</div>
</div>
</li>
</ul>
</div>
<?php } ?>`
Define $i = 0 and make it $i++
Try below code
<?php
$qry = MYSQL_QUERY("SELECT * FROM `post` WHERE `post_type`='events' AND `post_event_start_date` <= CURDATE() ORDER BY `post_event_start_date`");
?>
<?php
$i = 0;
while( $row = MYSQL_FETCH_OBJECT($qry) ){ ?>
<div class="item <?php if($i==0) { ?> active<?php } ?>">
<ul class="thumbnails">
<li class="col-xs-12 col-lg-12 col-md-12 col-sm-12">
<div class="fff">
<div class="thumbnail">
<img src="administrator/slide_name/large/slide_1.jpg" alt="">
</div>
<div class="col-lg-12 well">
<div class="form-group">
<h3><?php print_r($row->post_title); ?></h3>
</div>
<div class="form-group">
<h4><?php print_r($row->post_url); ?></h4>
<h5></h5>
</div>
<div class="form-group">
<div class="row">
<div class="col-lg-6 col-md-6 col-sm-12 col-xs-12">
<center><p>Start Date & Time</p><?php print_r($row->post_event_start_date); ?></center>
</div>
<div class="col-lg-6 col-md-6 col-sm-12 col-xs-12">
<center><p>Start End & Time</p><?php print_r($row->post_event_end_date); ?></center>
</div>
</div>
</div>
<div class="form-group">
<p>Location:</p>
<p><?php print_r($row->post_maplocation); ?></p>
</div>
<div class="form-group">
<p>Description: </p>
<p><?php print_r($row->post_desc); ?></p>
</div>
<div class="row pull-right">
<a class="btn btn-mini" href="#">» Read More</a>
<a class="btn btn-mini" href="#">» Share it</a>
</div>
</div>
</div>
</li>
</ul>
</div>
<?php
$i++;
} ?>
<?php $i = 0 ;
$qry = MYSQL_QUERY("SELECT * FROM `post` WHERE `post_type`='events' AND `post_event_start_date` <= CURDATE() ORDER BY `post_event_start_date`");
?>
<?php while( $row = MYSQL_FETCH_OBJECT($qry) ){ ?>
<div class="item <?php if($i==0) { ?> active<?php } ?>">
Your other code
php $i++; } ?>
Ok, may be its really simple. but, I can't figure it out how to do it. What I want is to query one table and show results in two divs on the page. What I mean is that I have some html like this:
<div class="row">
<div class="small">
//
</div>
<div class="large">
//
</div>
<div class="small">
//
</div>
<div class="small">
//
</div>
<div class="small">
//
</div>
<div class="large">
//
</div>
</div>
and the query like this
$stmt = $pdo->prepare("SELECT * FROM cars WHERE cars_cat = ? ORDER BY car_id DESC");
$stmt->execute(array($cat_id));
$data = $stmt->fetchAll();
foreach($data as $row) {
}
The database table is simple table with id, name, image, cars_cat.
UPDATE:
This is the structure of the gallery
<div class="col-sm-4 col-md-4 col-lg-4 nopadding"> <!-- First Column -->
<div class="small">
</div>
<div class="large">
</div>
<div class="small">
</div>
<div class="small">
</div>
</div> <!-- End First Column -->
<div class="col-sm-4 col-md-4 col-lg-4 nopadding"> <!-- Second Column -->
<div class="small">
</div>
<div class="small">
</div>
<div class="large">
</div>
<div class="small">
</div>
</div> <!-- End Second Column -->
<div class="col-sm-4 col-md-4 col-lg-4 nopadding"> <!-- Third Column -->
<div class="large">
</div>
<div class="small">
</div>
<div class="large">
//
</div>
</div> <!-- End Third Column -->
Try this way
<style>
.row {
-webkit-column-width:400px;
-moz-column-width:400px;
column-width:400px;
-webkit-column-gap:5px;
-moz-column-gap:5px;
column-gap:5px;
}
.small-box{
display:inline-block;
margin:0 0 5px 0;
padding:10px;
color:white;
}
.img-responsive{
width:100%;
height:auto;
}
</style>
<div class="row">
<div class="small-box">
<img src="http://aneemals.com/wp-content/uploads/2013/04/photos-of-animals-that-know-what-love-is-3.jpg" alt="img" class="img-responsive">
</div>
<div class="small-box">
<img src="http://www.softstuffcreations.com/refresh/data/zoom_image/1772-PandaBear_resized.jpg" alt="img" class="img-responsive">
</div>
<div class="small-box">
<img src="http://images6.fanpop.com/image/photos/35600000/wild-animals-animals-of-the-world-35665506-800-533.jpg" alt="img" class="img-responsive">
</div>
<div class="small-box">
<img src="http://nice-animals.com/wp-content/uploads/2013/10/facts-of-love-between-animals-08.jpg" alt="img" class="img-responsive">
</div>
<div class="small-box">
<img src="http://www.kenya.com/wp-content/uploads/2013/03/Mt-Kenya-Animals.jpg" alt="img" class="img-responsive">
</div>
<div class="small-box">
<img src="http://www.softstuffcreations.com/refresh/data/zoom_image/1772-PandaBear_resized.jpg" alt="img" class="img-responsive">
</div>
<div class="small-box">
<img src="http://fc09.deviantart.net/fs71/i/2012/364/4/3/animals___lion_9_by_moonsongstock-d5pr9za.jpg" alt="img" class="img-responsive">
</div>
</div>
Means basically what you need to update is to update code this way
<div class="row">
<?php
foreach($data as $row) {
echo '<div class="small-box"> your image </div>';
}
?>
</div>
<div class="row">
<?php
foreach($data as $row) {
if($row->div_type == 0){
echo '<div class="small"> your data </div>';
}else{
echo '<div class="large"> your data </div>';
}
}
?>
</div>