I want to display data use card-deck. So every three data from database, it will make a new line. So, one line just have 3 datas, how to fix it?
Here my code, it display one line with all data from database and how to fix it?
<?php for($i=0; $i<count($data); $i++){
if($i % 3 == 0){?>
<div class="card-deck ml-2 mr-2 mb-3">
<?php $no=1; foreach ($data as $d ){?>
<div class="card border-dark">
<img class="card-img-top" src="<?php echo base_url('assets/koceng.jpeg')?>" alt="Card image cap">
<div class="card-body">
<h5 class="card-title">Card title</h5>
<p class="card-text"><?php echo $no++ ?>
<?php echo $d->nim ?>
<?php echo $d->nama ?>
<?php echo $d->kelas ?>
<?php echo $d->nama_jurusan ?>
<?php echo $d->fakultas ?></p>
<p class="card-text"><small class="text-muted">Last updated 3 mins ago</small></p>
</div>
</div>
<?php }?>
</div>
<?php }}?>
Thankyou.
Take a look at this tutorial.
<?php $i = 0;foreach ($data as $d ){?> <!-- i is for count number of cards -->
<?php if($i%3 == 0){ ?> <!-- printing card-deck per 3 cards -->
<div class="card-deck ml-2 mr-2 mb-3">
<?php } ?>
<div class="card border-dark">
<img class="card-img-top" src="<?php echo base_url('assets/koceng.jpeg')?>" alt="Card image cap">
<div class="card-body">
<h5 class="card-title">Card title</h5>
<p class="card-text"><?php echo $no++ ?> <!-- i don't know why $no is used for -->
<?php echo $d->nim ?>
<?php echo $d->nama ?>
<?php echo $d->kelas ?>
<?php echo $d->nama_jurusan ?>
<?php echo $d->fakultas; $i++;?></p> <!-- increment i by one per card -->
<p class="card-text"><small class="text-muted">Last updated 3 mins ago</small></p>
</div>
</div>
<?php if($i%3 == 0){ ?>
</div>
<?php } } ?>
Related
I have coded this image slider that moves the image and it's details using the cursor.
I want to fetch data from database and display 4 books images with their details in each row I have tried this code but all the data have displayed in one row only.
Also if this does not take much of your time I want to make only 7 rows per page (28 books) and add numbers at the bottom of the page to move to the next 7 rows.
this is my try :
<html>
<body>
<div class="popular_causes_area section_padding">
<div class="container"> <div class="row">
<div class="col-lg-12">
<div class="causes_active owl-carousel">
<?php
try{
require('connection.php');
$sql1="select * from books ";
$rs=$db->query($sql1);
$db =null;
} catch(PDOException $ex) {
die ("Error Message ".$ex->getMessage()); }
$i=0;
while ($books = $rs->fetch()){
$i++;
$bpic = $books[2];
$btitle= $books[3];
$bq= $books[4];
$bstatus = $books[5];
$bdesc = $books[6];
$bnumber = $books[7];
$bcity = $books[8];
if ($i % 3 == 1) {
echo "<li>"; ?>
<div class="single_cause">
<div class="thumb">
<?php
if ($bpic == "" ) {
echo "<img src='img/default-image.png'
height=450px'>";
} else {
echo "<img src='img/$bpic' placeholder='nooooo'
height=450px alt=''>";
}?>
</div>
<div class="causes_content">
<div class="balance d-flex justify-content-between align-items-center">
<span>Tell:<?php echo $bnumber; ?> </span>
<span><?php echo $bcity; ?> </span>
</div>
<h4><?php echo $btitle; ?> </h4>
<p><?php echo $bdesc; echo "<br>
condation:".$bstatus; ?> </p>
</div>
</div>
</li>
<?php
} ////end of first if
if ($i % 3 == 0) {
echo "<li>"; ?>
<div class="single_cause">
<div class="thumb">
<?php
if ($bpic == "" ) {
echo "<img src='img/default-image.png' height=450px'>";
} else {
echo "<img src='img/$bpic' placeholder='nooooo' height=450px alt=''>"; ?>
</div>
<div class="causes_content">
<div class="balance d-flex justify-content-between align-items-center">
<span>Tell:<?php echo $bnumber; ?> </span>
<span><?php echo $bcity; ?> </span>
</div>
<h4><?php echo $btitle; ?> </h4>
<p><?php echo $bdesc; echo "<br>
condation:".$bstatus; ?> </p>
</div>
</div>
</li>
<?php
} //end of second if
} // end of while ?>
</div>
</div>
</div>
</div>
</div>
</body>
</html>
This image shows what I got (all the data in one row instead of 4 books per row !):
image slider code :
<div class="popular_causes_area section_padding">
<div class="container">
<div class="row">
<div class="col-lg-12">
<div class="causes_active owl-carousel">
<div class="single_cause">
<div class="thumb">
<?php
if ($bpic == "" ) {
echo "<img src='img/default-image.png' height=450px'>";
} else {
echo "<img src='img/$bpic' placeholder='nooooo' height=450px alt=''>";
}
?>
</div>
<div class="causes_content">
<div class="balance d-flex justify-content-between align-items-center">
<span>Tell:<?php echo $bnumber; ?> </span>
<span><?php echo $bcity; ?> </span>
</div>
<h4><?php echo $btitle; ?> </h4>
<p><?php echo $bdesc; echo "<br> condation:".$bstatus; ?> </p>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
I'm using data from MySQL database and display looks like this:
I want 3 articles in one line. The pictures below are made with HTML and CSS.
This is my code:
while($row=mysqli_fetch_array($result))
{?>
<div class="container">
<div class="row padding">
<div class="col-md-4 col-xs-12">
<div class="card">
<img class="card-img-top" src="<?php echo UPLPATH . $row['slika']; ?>">
<div class="card-body">
<h6 class="card-title">« <?php echo $row['naslov']; ?> »</h4>
</div>
<p class="ispod"><?php echo $row['datum'];?> </p>
</div>
</div>
</div>
</div>
<?php }?>
This seems to be a nice point in your project to implement a template engine. There a couple out there. I really liked working with typo3 fluid, but back to your question...
For your existing code, you can end your php code, use html and just echo the php variables you need.
<?php
// ... some php stuff
?>
<div class="container">
/* some html stuff * /
<img class="malaSlika" src="<?php echo UPLPATH . $row['slika']; ?>">
/* some more html stuff */
/* <?= is short for <?php echo
<img class="malaSlika" src="<?= UPLPATH . $row['slika']; ?>">
</div>
<?php
// ... some more php stuff
As said, maybe a template engine would help this even further.
Edit: To add div class="row" it could be something like this.
<?php ... ?>
<div class="container">
<?php
$i = 0;
while($row=mysqli_fetch_array($result)) :
if($i % 3 == 0) : ?>
<div class="row padding">
<?php endif; ?>
<div class="col-md-4 col-xs-12">
<div class="card">
<img class="card-img-top" src="<?php echo UPLPATH . $row['slika']; ?>">
<div class="card-body">
<h6 class="card-title">« <?php echo $row['naslov']; ?> »</h4>
</div>
<p class="ispod"><?php echo $row['datum'];?> </p>
</div>
</div>
<?php if($i % 3 == 0) : ?>
</div>
<?php endif; ?>
<?php
$i++;
endwhile;
?>
</div>
It will always get ugly at some point I think. This is a quick solution. the modulo (%) devides by the number and returns the rest value. so $i modulo 3 return 0 at 0, 3, 6, 9, 12 etc. Meaning it will always add the "row padding" div.
Here's the div that manages the look of each item in the loop:
<div style="float:left;width:30%;padding:30px;height:auto;">
<img src="<?php echo $r['image']['sizes']['team']; ?>" style="max-width:750px !important;width:100%;">
<h3>
<?php echo $r['name']; ?>
</h3>
<div class="position">
<?php echo $r['position']; ?>
</div>
<?php echo $r['intro_text']; ?>
<?php if ($r['learn_more_text']) { ?>
Learn More
<?php } ?>
<?php if ($r['learn_more_text']) { ?>
<div id="<?php echo sanitize_title($r['name']); ?>" class="reveal-modal medium" data-reveal aria-labelledby="modalTitle" aria-hidden="true" role="dialog">
<h3><?php echo $r['name']; ?></h3>
<div class="position">
<?php echo $r['position']; ?>
</div>
<div class="content">
<?php echo $r['learn_more_text']; ?>
</div>
<a class="close-reveal-modal" aria-label="Close">×</a>
</div>
</div>
If I set the height to a fixed number (eg: 450x), the images are aligned in a row. But that will make the page non mobile responsive. If I set the height to auto or to a percentage like 30%, the images become mobile responsive but they don't align. I get 2 images that separate into different rows.
Also, I got 2 sections of image. One is called Our Leadership and the other is called Our Team. The section at the bottom is aligned correctly and is contained in a div. The div above it refuses to be contained in a div. If I wrap it in a div, Only the first item wraps itself in the div. I've no idea why.
eg:
<center>
<div>
<div>first item here</div>
</div>
<div>second item here</div>
<div>third item here</div>
<div>fourth item here</div>
Here's the complete code for both sections:
<center><h2>Our Leadership</h2></center>
<center><div>
<?php
$people = get_field('people');
$a = 0;
foreach ($people as $r) {
$a++;
?>
<div style="float:left;width:30%;padding:30px;height:auto;">
<img src="<?php echo $r['image']['sizes']['team']; ?>" style="max-width:750px !important;width:100%;">
<h3>
<?php echo $r['name']; ?>
</h3>
<div class="position">
<?php echo $r['position']; ?>
</div>
<?php echo $r['intro_text']; ?>
<?php if ($r['learn_more_text']) { ?>
Learn More
<?php } ?>
<?php if ($r['learn_more_text']) { ?>
<div id="<?php echo sanitize_title($r['name']); ?>" class="reveal-modal medium" data-reveal aria-labelledby="modalTitle" aria-hidden="true" role="dialog">
<h3><?php echo $r['name']; ?></h3>
<div class="position">
<?php echo $r['position']; ?>
</div>
<div class="content">
<?php echo $r['learn_more_text']; ?>
</div>
<a class="close-reveal-modal" aria-label="Close">×</a>
</div>
</div>
<?php } ?>
</div>
<?php } ?>
</div></center>
<div style="clear:both;"></div>
<center><h2>Our Team</h2></center>
<center><div>
<?php
$people2 = get_field('people_bottomsection');
$a = 0;
foreach ($people2 as $r) {
$a++;
?>
<div style="float:left;width:30%;padding:30px;height:auto;">
<img src="<?php echo $r['image']['sizes']['team']; ?>" style="max-width:750px !important;width:100%;">
<h3>
<?php echo $r['name']; ?>
</h3>
<div class="position">
<?php echo $r['position']; ?>
</div>
<?php echo $r['intro_text']; ?>
<?php if ($r['learn_more_text']) { ?>
Learn More
<?php } ?>
<?php if ($r['learn_more_text']) { ?>
<div id="<?php echo sanitize_title($r['name']); ?>" class="reveal-modal medium" data-reveal aria-labelledby="modalTitle" aria-hidden="true" role="dialog">
<h3><?php echo $r['name']; ?></h3>
<div class="position">
<?php echo $r['position']; ?>
</div>
<div class="content">
<?php echo $r['learn_more_text']; ?>
</div>
<a class="close-reveal-modal" aria-label="Close">×</a>
</div>
</div>
<?php } ?>
</div>
<?php } ?>
</div></center>
<div style="clear:both;"></div>
Check out the source code at: http://www.equitasmg.com/who-we-are-2/
it's because the items in the loop are having different height. So, try adding a constant height to each item in the loop.
Eg:
<div style="float:left;width:30%;padding:30px;min-height:550px;"></div>
I have this following snippet:
<?php
if($people->num_rows >= 1) {
while($person = $people->fetch_object()) {
echo '
<div class="col-lg-4 col-md-4 col-sm-4">
<div class="card card-user">
<div class="content">
<div class="author">
<a href="'.$system->getDomain()?>/user/<?=$person->id.'">
<img class="avatar" src="'.$system->getProfilePicture($person).'">
<h4 class="title">'.$system->getFirstName($person->full_name).', '.$person->age.'</h4>
</a>
</div>
<p class="text-center text-muted">
';
echo $person->city.$system->ifComma($person->city); echo ' '.$person->country.'
</p>
</div>
</div>
</div>
';
}
} else {
?>
however next to the actual age (after the closing H4 tag) I'd like to add a further function which shows the current online status of the user.
The code for this would be as follows:
<?php if($system->isOnline($profile->last_active)) { echo '<i class="online-status online"></i>'; } else { echo '<i class="online-status offline"></i>'; } ?>
What is the best way to combine this snippet (online status) with the statement above?
Some expert help would be greatly appreciated
Separate your HTML and PHP code and do it like the below. Your requirement is done using a single line ternary operator.
<?php
if($people->num_rows >= 1) {
while($person = $people->fetch_object()) { ?>
<div class="col-lg-4 col-md-4 col-sm-4">
<div class="card card-user">
<div class="content">
<div class="author">
<a href="<?php echo $system->getDomain().'/user/'.$person->id;?>">
<img class="avatar" src="<?php echo $system->getProfilePicture($person);?>">
<h4 class="title"><?php echo $system->getFirstName($person->full_name).', '.$person->age;?></h4>
<?php echo ($system->isOnline($profile->last_active)) ? '<i class="online-status online"></i>' : '<i class="online-status offline"></i>'; ?>
</a>
</div>
<p class="text-center text-muted">
<?php echo $person->city.$system->ifComma($person->city); echo ' '.$person->country;?>
</p>
</div>
</div>
</div>
<?php
}
}
?>
I am working on an application where i have a slider made in bootstrap but in slider there are two images in one slide show instead of one, so i have wrote a simple select query to get record but the problem is that it showing same images on boxes, i dont know how i can get next data in one loop
here is the code
<div class="carousel-inner">
<?php if($blog) : $counter = 0; foreach($blog as $blogs) : $counter++; ?>
<?php if($counter === 1) : ?>
<div class="item active">
<?php else : ?>
<div class="item">
<?php endif; ?>
<div class="row">
<div class="col-md-6">
<div class="panel">
<div class="panel-heading">
<h3>
<?php echo substr($blogs -> heading, 0, 30); ?> <br> <br> <br>
<p> <?php echo substr($blogs -> description, 0, 300); ?> <br><br>
Read the full story </p><br>
<img src="<?php echo base_url().$blogs -> image; ?>">
</h3>
</div>
</div>
</div>
<div class="col-md-6">
<div class="panel">
<div class="panel-heading">
<h3>
<?php echo substr($blogs -> heading, 0, 30); ?> <br> <br> <br>
<p> <?php echo substr($blogs -> description, 0, 300); ?> <br><br>
Read the full story </p><br>
<img src="<?php echo base_url().$blogs -> image; ?>">
</h3>
</div>
</div>
</div>
</div><!--.row-->
</div><!--.item-->
<?php endforeach; endif; ?>
</div><!--.carousel-inner-->
<a data-slide="prev" href="#blog" class="left carousel-control">‹</a>
<a data-slide="next" href="#blog" class="right carousel-control">›</a>
</div><!--.Carousel-->
the above is the result but i want the second image to be the next data from db
try this:
<div class="carousel-inner">
<?php if($blog) : $counter = 0; foreach($blog as $blogs) : $counter++; ?>
<?php if($counter === 1) : ?>
<div class="item active">
<?php else : ?>
<div class="item">
<?php endif; ?>
<div class="row">
<div class="col-md-6">
<div class="panel">
<div class="panel-heading">
<h3>
<?php echo substr($blogs -> heading, 0, 30); ?> <br> <br> <br>
<p> <?php echo substr($blogs -> description, 0, 300); ?> <br><br>
Read the full story </p><br>
<img src="<?php echo base_url().$blogs -> image; ?>">
</h3>
</div>
</div>
</div>
<?php endforeach; endif; ?>
What outputs if you post this?