I got some projects that use a template layout/slider. The slider works by wrapping the first 4 projects in a div with class active and the rest inside divs without active but still grouped per 4.
I tried the following:
$tel = 1;
foreach($projectcr1 as $projects1) {
$projectimages = $projects1['images']; // Get image parameters of the article
$projectimgs = json_decode($projectimages); // Split the parameters apart
if($projectimgs->image_intro != ''){
$img = 'cms/'.$projectimgs->image_intro;
}else{
$img = 'cms/images/placeholder/placeholder.jpg';
}
if (strlen($projects1['introtext']) > 40){
$shortstrproj = substr($projects1['introtext'], 0, 40) . '...';
}else{
$shortstrproj = $projects1['introtext'];
}
if($projects1['id'] != ''){
if($tel == 1){
$projecten1 .= '<div class="carousel-item active">
<div class="row">';
}
//Here I need the first 4 projects ($projecten1 below is one project being looped)
$projecten1 .= '
<div class="col-xs-12 col-sm-6 col-lg-3 js-wpg-item" data-categories="'.$projects1['categorie_alias'].'">
<a class="card portfolio-grid__card js-wpg-card" href="project/'.$projects1['content_alias'].'.html">
<div class="imgwrapport">
<img width="360" height="180" src="'.$img.'" class="card-img-top portfolio-grid__card-img" alt="photo1" srcset="'.$img.' 360w, '.$img.' 300w, '.$img.' 768w, '.$img.' 540w, '.$img.' 830w"
sizes="(max-width: 360px) 100vw, 360px">
</div>
<div class="card-block portfolio-grid__card-block">
<h5 class="card-title ortfolio-grid__card-title">'.$projects1['title'].'</h5>
<p class="portfolio-grid__card-price">'.strip_tags($shortstrproj).'</p>
<div class="portfolio-grid__card-items">
<p class="portfolio-grid__card-item">
Bekijk project
</p>
</div>
</div>
</a>
</div>';
if(($tel % 4) == 0){
$projecten1 .= '</div></div><div class="carousel-item">
<div class="row">';
// Here I need the rest of the projects
$projecten1 .= '</div></div>';
}
$tel++;
}
}
echo $projecten1;
?>
If $tel = 1 wrap the div with active around the items. Then when $tel % 4 leaves 0 (4 divided by 4 leaves 0), close the divs and start new ones but this time without the active class.
This works fine, my only issue is how to seperate the data. How can I show the first 4 projects inside the first div, and the rest in the following divs without active class?
Example how I want the code in the end:
<div class="carousel-item active">
<div class="row">
project1
project2
project3
project4
</div>
</div>
<div class="carousel-item">
<div class="row">
project5
project6
project7
project8
</div>
</div>
<div class="carousel-item">
<div class="row">
project9
project10
project11
project12
</div>
</div>
try this Code . if u have questions about the code just post under it.
<?php
$tel = 1;
$next_div = true;
$foreach_ended = 0;
// 2 New vars they are needed for the right output down
foreach($projectcr1 as $projects1) {
$projectimages = $projects1['images']; // Get image parameters of the article
$projectimgs = json_decode($projectimages); // Split the parameters apart
if($projectimgs->image_intro != ''){
$img = 'cms/'.$projectimgs->image_intro;
}else{
$img = 'cms/images/placeholder/placeholder.jpg';
}
if (strlen($projects1['introtext']) > 40){
$shortstrproj = substr($projects1['introtext'], 0, 40) . '...';
}else{
$shortstrproj = $projects1['introtext'];
}
if($projects1['id'] != '')
{
$foreach_ended=0;
// set to 0 when a DIV is Open.
if($tel == 1&&$next_div==true){ /* set the first ACTIVE div with $next_div */
$projecten1 .= '<div class="carousel-item active"><div class="row">';
$next_div=false; /* set it to false so every new 4xDIV is not Active */
}
elseif($tel == 1&&$next_div!=true)
{
$projecten1 .= '<div class="carousel-item"><div class="row">';
}else{}
$projecten1 .= '
<div class="col-xs-12 col-sm-6 col-lg-3 js-wpg-item" data-categories="'.$projects1['categorie_alias'].'">
<a class="card portfolio-grid__card js-wpg-card" href="project/'.$projects1['content_alias'].'.html">
<div class="imgwrapport">
<img width="360" height="180" src="'.$img.'" class="card-img-top portfolio-grid__card-img" alt="photo1" srcset="'.$img.' 360w, '.$img.' 300w, '.$img.' 768w, '.$img.' 540w, '.$img.' 830w"
sizes="(max-width: 360px) 100vw, 360px">
</div>
<div class="card-block portfolio-grid__card-block">
<h5 class="card-title ortfolio-grid__card-title">'.$projects1['title'].'</h5>
<p class="portfolio-grid__card-price">'.strip_tags($shortstrproj).'</p>
<div class="portfolio-grid__card-items">
<p class="portfolio-grid__card-item">
Bekijk project
</p>
</div>
</div>
</a>
</div>';
if(($tel % 4) == 0){
$tel=1; /* reset Tel */
$projecten1 .= '</div></div>'; /* End DIV when 4x is reached */
$foreach_ended=1; /* set DIV to closed */
}
else
{
$tel++;
}
}
}
if($foreach_ended==0){$projecten1 .= '</div></div>';} /* when the Foreach enden and the LAST div had less then 4 items its not marked as CLOSED . so this IF closes the last open DIV */
echo $projecten1;
?>
Related
I have this piece of code regarding a carousel. I need the first card to have the "active" class but not the following ones or the carousel wont slide. How can i do that?
Here's my code...
$sql = "select wine.wine_name, wine.id, wine.wine_img,
region.region_name, winetype.winetype_name from wine, region, winetype
where wine.region_id = region.id and wine.winetype_id = winetype.id
and sponsored = 1;";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo '<div class="carousel-item col-md-4 active">
<div class="card align-items-center">
<img class="card-img-top img-fluid " src='.$row["wine_img"].' alt="Card image cap">
<div class="card-body">
<h4 class="card-title">'.$row["wine_name"].'</h4>
<p class="card-text">'.$row["winetype_name"].' - '.$row["region_name"].'</p>
</div>
</div>
</div>';
}
} else {
echo "ERRO!";
}
you can use a counter to set the active class.
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
$count_results = 0;
while($row = $result->fetch_assoc()) {
echo '<div class="carousel-item col-md-4 ' . ($count_results == 0 ? 'active' : '') . '">
<div class="card align-items-center">
<img class="card-img-top img-fluid " src='.$row["wine_img"].' alt="Card image cap">
<div class="card-body">
<h4 class="card-title">'.$row["wine_name"].'</h4>
<p class="card-text">'.$row["winetype_name"].' - '.$row["region_name"].'</p>
</div>
</div>
</div>';
$count_results++;
}
} else {
echo "ERRO!";
}
You could just have a field with the extra class part set to active and then reset it at the end of the loop...
// output data of each row
$class = ' active';
while($row = $result->fetch_assoc()) {
echo '<div class="carousel-item col-md-4' .$class. '">
<div class="card align-items-center">
<img class="card-img-top img-fluid " src='.$row["wine_img"].' alt="Card image cap">
<div class="card-body">
<h4 class="card-title">'.$row["wine_name"].'</h4>
<p class="card-text">'.$row["winetype_name"].' - '.$row["region_name"].'</p>
</div>
</div>
</div>';
$class = '';
}
This should do the trick:
while($row = $result->fetch_assoc()) {
$active = isset($active) ? '' : 'active';
echo '<div class="carousel-item col-md-4 ' . $active . '">
...
}
The variable $active will only be unset (undefined or null) on the first iteration. Then, on the other iterations, the variable will be defined and not null (since it's an empty string) so then we simply set it as an empty string again.
I would make a counter and simply check if you need to add the class. Of course, since you only need it once, you can use a boolean.
<?php
$sql = "select wine.wine_name, wine.id, wine.wine_img,
region.region_name, winetype.winetype_name from wine, region, winetype
where wine.region_id = region.id and wine.winetype_id = winetype.id
and sponsored = 1;";
$result = $conn->query($sql);
?>
<?php if ($result->num_rows > 0): ?>
<?php $first = true; ?>
<?php while ($row = $result->fetch_assoc()): ?>
<div class="carousel-item col-md-4 <?= $first ? 'active' : '' ?>">
<div class="card align-items-center">
<img class="card-img-top img-fluid " src=<?= $row["wine_img"] ?> alt="Card image cap">
<div class="card-body">
<h4 class="card-title"><?= $row["wine_name"] ?></h4>
<p class="card-text"><?= $row["winetype_name"] ?> - <?= $row["region_name"] ?></p>
</div>
</div>
</div>
<?php $first = false; ?>
<? endwhile; ?>
<?php else: ?>
<span>Error!</span>
<?php endif; ?>
I've also taken the liberty of taking all of your HTML out of the single echo. This is much cleaner :)
<div class="row">
<div class="col-md-10 col-md-offset-1">
<div class="row text-center">
<?php
if($_GET[id] == 0)
{
$querysb = mysql_query("SELECT * FROM services WHERE parentid !=0 order by parentid, cid ");
}
else
{
$querysb = mysql_query("SELECT * FROM services WHERE parentid='".$_GET[id]."'");
}
while($rowsb = mysql_fetch_assoc($querysb))
{
if($val == '6' || $val =='10'){
$classname = 'whitebg';
} else {
$classname = 'bg-blue co-white';
}
?>
<div class="col-md-4 mrgnBtm15">
<div class="<?php echo $classname;?> padding30" style="min-height: 250px">
<h3 class="service-heading">
<?php echo $rowsb['cname'];?>
</h3>
<h4>
RS <?php echo $rowsb['price'];?><br>
</h4>
<div class="mrgnTop15 clearfix"></div>
<a class="btn bg-orange co-white" href="<?php echo MYWEBSITE;?>servicedetail/<?php echo to_prety_url($rowsb['cname']).'-'.$rowsb['cid'];?>.html">
<font style="size:14px; color:#000; font-weight:bolder;font-family: "Open Sans";">Register</font></a>
</div>
</div>
<?php } ?>
</div>
</div>
</div>
I am working on a dynamic website. here is the code of a particular page. In this page there is a div section with class="col md-4". If the number of content panel in that division appears to 5 or 7 in that case I want only the last panel (i.e 5th and 7th) to be in full column (col-12). What should I do?
Since you are using col-md-4, 3 divs will be shown each row. So I think what you are looking for is a way to make the last div full width if its going to be alone in its row. In that case, you will need to make it col-md-12 on 4th (not 5th) and 7th and 10th records and so on. This is exactly what the following code does,
I think what you want to do is show the
<?php
if($_GET[id] == 0)
{
$querysb=mysql_query("SELECT * FROM services WHERE parentid !=0 order by parentid, cid ");
}
else
{
$querysb=mysql_query("SELECT * FROM services WHERE parentid='".$_GET[id]."'");
}
$number_of_records = mysql_num_rows($querysb);
$counter = 1;
while($rowsb=mysql_fetch_assoc($querysb))
{
if($val == '6' || $val =='10'){
$classname= 'whitebg';
}else{
$classname= 'bg-blue co-white';
}
//if($number_of_records %3 ==1 && $counter == $number_of_records)
if(($number_of_records == 5 || $number_of_records == 7) && $counter == $number_of_records)
$col_class = "col-md-12";
else
$col_class = "col-md-4";
?>
<div class="<?php echo $col_class;?> mrgnBtm15">
<div class="<?php echo $classname;?> padding30" style="min-height: 250px">
<h3 class="service-heading">
<?php echo $rowsb['cname'];?>
</h3>
<h4>
RS <?php echo $rowsb['price'];?><br>
</h4>
<div class="mrgnTop15 clearfix"></div>
<a class="btn bg-orange co-white" href="<?php echo MYWEBSITE;?>servicedetail/<?php echo to_prety_url($rowsb['cname']).'-'.$rowsb['cid'];?>.html">
<font style="size:14px; color:#000; font-weight:bolder;font-family: "Open Sans";">Register</font></a>
</div>
</div>
<?php
$counter++;
} ?>
</div>
</div>
</div>
I got an array with facebook posts, but I have a limit on the number of posts AND a check which checks if the message isn't empty and if the post was made by the page owner.
How can I filter out those results from the first array, and build a new array from that data? Because limiting the first array is not an option, it also counts the wrong messages (by other than page owner, edited profile pictures etc).
My code now (relevant parts):
$feedarray = json_decode($json_object);
$f = 0;
foreach ( $feedarray->data as $key => $feed_data )
{
if($feed_data->full_picture != ''){
$fbimage = $feed_data->full_picture;
}else{
$fbimage = 'cms/images/placeholder/placeholder.jpg';
}
if($feed_data->message > 270){
$shortstrfb = substr($feed_data->message, 0, 270) . '...';
}else{
$shortstrfb = $feed_data->message;
}
if($feed_data->message != ''){
$f++;
}
if($f > 2){
break;
}
if($feed_data->message != '' && $feed_data->from->name == 'page owner'){
$facebookfeed .= '
<div class="col-lg-4 marg-lg-b30">
<div class=" widget">
<div class="card latest-news latest-news--block">
<a href="'.$feed_data->permalink_url.'" class="latest-news__image">
<img src="'.$fbimage.'" width="360" height="204" srcset="'.$fbimage.' 360w, '.$fbimage.' 830w" sizes="(min-width: 992px) 360px, calc(100vw - 30px)" class="card-img-top " alt="nieuws">
</a>
<div class="card-block latest-news__content js-col-height-news">
<time class="latest-news__date">'.date("d-m-Y",strtotime($feed_data->updated_time)).'</time>
<h4 class="card-title latest-news__title">
<i class="fa fa-facebook-official" aria-hidden="true"></i> Bekijk
</h4>
<div class="card-text latest-news__text">
'.$shortstrfb.'
</div>
</div>
</div>
</div>
</div>';
}
}
echo $facebookfeed;
As you can see I check all those things on $feedarray while I probably need to check on it, and from the results build a new array which I can use like I want. What is a good way to approach this?
You could build a new array and append your loop result in it and at the end check its content with print_r() or var_dump() :
$new_array = array();
foreach ( $feedarray->data as $key => $feed_data )
{
if($feed_data->full_picture != ''){
$fbimage = $feed_data->full_picture;
}else{
$fbimage = 'cms/images/placeholder/placeholder.jpg';
}
if($feed_data->message > 270){
$shortstrfb = substr($feed_data->message, 0, 270) . '...';
}else{
$shortstrfb = $feed_data->message;
}
if($feed_data->message != ''){
$f++;
}
if($f > 2){
break;
}
if($feed_data->message != '' && $feed_data->from->name == 'page owner'){
$facebookfeed .= '
<div class="col-lg-4 marg-lg-b30">
<div class=" widget">
<div class="card latest-news latest-news--block">
<a href="'.$feed_data->permalink_url.'" class="latest-news__image">
<img src="'.$fbimage.'" width="360" height="204" srcset="'.$fbimage.' 360w, '.$fbimage.' 830w" sizes="(min-width: 992px) 360px, calc(100vw - 30px)" class="card-img-top " alt="nieuws">
</a>
<div class="card-block latest-news__content js-col-height-news">
<time class="latest-news__date">'.date("d-m-Y",strtotime($feed_data->updated_time)).'</time>
<h4 class="card-title latest-news__title">
<i class="fa fa-facebook-official" aria-hidden="true"></i> Bekijk
</h4>
<div class="card-text latest-news__text">
'.$shortstrfb.'
</div>
</div>
</div>
</div>
</div>';
}
// build the new array :
$new_array[] = array(
'fbimage' => $fbimage,
'shortstrfb' => $shortstrfb,
'facebookfeed' => $facebookfeed
);
}
print_r($new_array);
Hope it helps.
I got a project page that echoes multiple projects in a foreach loop, and every 4 results it starts a new row on the page.
However I got an element that always needs to be shown as the last result. How can I achieve that?
My foreach loop:
<?
$tel = 1;
foreach ($projectcr as $project) {
$article_images = $project['images']; // Get image parameters of the article
$pictures = json_decode($article_images); // Split the parameters apart
$introtext = $project['introtext'];
if ($project['title'] != '') {
if($tel == 1) {
echo '<div class="row">';
}
echo '
<div class="col-xs-12 col-sm-6 col-lg-3 js-wpg-item" data-categories="gardening">
<a class="card portfolio-grid__card js-wpg-card" href="project/'.$project['alias'].'.html">
<img src="cms/'.$pictures->{'image_intro'}.'" class="card-img-top portfolio-grid__card-img wp-post-image" alt="19" height="240" width="360">
<div class="card-block portfolio-grid__card-block">
<h5>'.$project['title'].'</h5>
<p>'.$project['created'].'</p>
</div>
</a>
</div>'
;
if(($tel % 4) == 0){
echo '</div> <div class="row">';
}
$tel++;
}
}
if(($tel % 4) != 0){
echo '</div>';
}
?>
The element I want to be shown as last block:
<div class="row">
<div class="col-xs-12 col-sm-6 col-lg-3 js-wpg-item" data-categories="">
<div class="portfolio-grid__card portfolio-grid__card--dummy js-wpg-card" href="projects/">
<div class="portfolio-grid__card-block text-center">
<span class="fa fa-cloud-upload fa-2x"></span>
<h5>Uw volgende project hier?</h5>
<p class="portfolio-grid__card-text">
NEEM CONTACT OP
</p>
</div>
</div>
</div><!-- /.col -->
</div><!-- /.row -->
This has the following result:
The element is now shown outside the row, while I need it to be inside the row (but only the last one).
Change your last if condition just after ending foreach
if(($tel % 4) != 0){
//echo div.col-... inside .row started in loop
echo '<div class="col-xs-12 col-sm-6 col-lg-3">always to be shown at last</div>';
//end .row
echo '</div>';
}
Add the content you want to show at the end of the loop. Don't check the condition and add the closing tag for row div, because it will fail to add the close tag if modulus of $tel is equal to zero. Your last content should be shown in new row but it will not close the tag.
$last = <div class="col-xs-12 col-sm-6 col-lg-3 js-wpg-item" data-categories="">
<div class="portfolio-grid__card portfolio-grid__card--dummy js-wpg-card" href="projects/">
<div class="portfolio-grid__card-block text-center">
<span class="fa fa-cloud-upload fa-2x"></span>
<h5>Uw volgende project hier?</h5>
<p class="portfolio-grid__card-text">
NEEM CONTACT OP
</p>
</div>
</div>
</div>
foreach($projectcr as $project) {
...//your code
...
}
echo $last. '</div>';
There are multiple ways to achieve that but can't you just add a element to your array?
$p = [];
$p['title'] = 'Uw volgende project hier?';
$p['created'] = 'NEEM CONTACT OP'
array_push($projectcr, $p);
A other way to do it is to check if its the last element in the array
$lastElement = end($array);
// you code
if($project == $lastElement){
//add the static block
}
I have a lot of images in a DB and I want to display it in a bootstrap carousel using php.
[Problem]
I am rookie with php so I hit the wall. Let me explain with code.
<div class="carousel-inner">
<div class="item active">
<div class="row">
<div class="col-md-3"><img src="http://placehold.it/250x250" alt="Image" style="max-width:100%;"></div>
<div class="col-md-3"><img src="http://placehold.it/250x250" alt="Image" style="max-width:100%;"></div>
<div class="col-md-3"><img src="http://placehold.it/250x250" alt="Image" style="max-width:100%;"></div>
<div class="col-md-3"><img src="http://placehold.it/250x250" alt="Image" style="max-width:100%;"></div>
</div><!--.row-->
</div><!--.item-->
<?php
$pdo = connect();
// display the list of all members in table view
$sql = "SELECT * FROM filmovi";
$query = $pdo->prepare($sql);
$query->execute();
$list = $query->fetchAll();
?>
<div class="item">
<div class="row">
<?php foreach($list as $rs) { ?>
<div class="col-md-3"><img src="assets/img/movies/<?php echo $rs['slika'] ?>" alt="Image" style="max-width:100%;"></div>
<?php } ?>
</div><!--.row-->
</div><!--.item-->
</div>
As you can see, carousel shows 4 images at once and another 4 and so on. In a foreach loop as it is now I get all my images at once and item active is empty.
I need to get 4 by 4 images from the DB to carousel but don't know which way to go.
You need to specify a counter that will check after each 4 iterations, to create a new <div class="item">...</div> element.
<?php
$counter = 0; //Set the counter to zero
foreach ($list as $single_list){
if($counter % 4 == 0) { // On every fourth iteration create a new item and row DIV
echo '<div class="item"><div class="row">';
}
... your code to output the images ...
if($counter % 4 == 0) {
echo '</div></div>'; // Close the row and item DIV
}
$counter++;
}
If you have problems implementing the code, or you don't understand what I've done here, don't hesitate to ask.