i want to change the background of the card div using PHP - php

I used the for loop to get the data dynamically from the database and displayed ,Now I have to display those cards in different colors using Php No javascript is needed
<div class="row gutter-xs">
<?php
foreach($dispcnt as $object)
{
?><div class="col-xs-3 col-md-3">
<div class="card bg-info"> // card
<div class="card-values">
<div class="p-x">
<small><?php echo $object->qa;?> </small>
<h3 class="card-title-l"><?php echo $object->cnt;?> Counts</h3>
</div>
</div>
</div>
</div> // code of card ends here
<?php }?>
</div>
I want to change the color of the cards in PHP
I am excepting the cards to look like this

Solution A with CSS only
Actually, you don't need any PHP to do that, you could do it only with some CSS rules with the help of the :nth-child() selector.
Here's a little snippet for demo if you have 4 colors always repeating:
.container {
display: flex;
flex-wrap: wrap;
}
.card {
width: 10em;
height: 4em;
margin: .5em;
padding: .5em;
color: white;
background: #0298cf;
}
.card:nth-child(4n + 1) {
background: #0298cf;
}
.card:nth-child(4n + 2) {
background: #9b479f;
}
.card:nth-child(4n + 3) {
background: #4e484e;
}
.card:nth-child(4n + 4) {
background: #f70d1a;
}
<div class="container">
<div class="card bg-info">
<div class="card-values">
<div class="p-x">
<small>Small 1</small>
<h3>1 Counts</h3>
</div>
</div>
</div>
<div class="card bg-info">
<div class="card-values">
<div class="p-x">
<small>Small 2</small>
<h3>2 Counts</h3>
</div>
</div>
</div>
<div class="card bg-info">
<div class="card-values">
<div class="p-x">
<small>Small 3</small>
<h3>3 Counts</h3>
</div>
</div>
</div>
<div class="card bg-info">
<div class="card-values">
<div class="p-x">
<small>Small 4</small>
<h3>4 Counts</h3>
</div>
</div>
</div>
<div class="card bg-info">
<div class="card-values">
<div class="p-x">
<small>Small 5</small>
<h3>5 Counts</h3>
</div>
</div>
</div>
<div class="card bg-info">
<div class="card-values">
<div class="p-x">
<small>Small 6</small>
<h3>6 Counts</h3>
</div>
</div>
</div>
<div class="card bg-info">
<div class="card-values">
<div class="p-x">
<small>Small 7</small>
<h3>7 Counts</h3>
</div>
</div>
</div>
<div class="card bg-info">
<div class="card-values">
<div class="p-x">
<small>Small 8</small>
<h3>8 Counts</h3>
</div>
</div>
</div>
<div class="card bg-info">
<div class="card-values">
<div class="p-x">
<small>Small 9</small>
<h3>9 Counts</h3>
</div>
</div>
</div>
</div>
If you want 6 colors then you would replace 4n by 6n and add the 2 other colors.
In your case, you'll have to put it on the first <div> which is in the loop, so the div directly child of the row:
.row > :nth-child(4n + 1) {
background: #0298cf;
}
.row > :nth-child(4n + 2) {
background: #9b479f;
}
.row > :nth-child(4n + 3) {
background: #4e484e;
}
.row > :nth-child(4n + 4) {
background: #f70d1a;
}
Solution B with PHP to generate a CSS class
By the way, your PHP should be in lowercase. You could also print a color class and increment it like this:
<div class="row gutter-XS">
<?php
$color = 1;
foreach ($dispcnt as $object) {
$color_class = "bg-color-$color";
if ($color < 4) {
$color++;
} else {
$color = 1;
}
?>
<div class="col-xs-3 col-md-3">
<div class="card <?php echo $color_class; ?>">
<div class="card-values">
<div class="p-x">
<small><?php echo $object->QA; ?></small>
<h3 class="card-title-l"><?php echo $object->CNT; ?> Counts</h3>
</div>
</div>
</div>
</div>
<?php } ?>
</div>
You'll then have the possibility to have CSS rules such as:
.card.bg-color-1 {
background: #0298cf;
}
.card.bg-color-2 {
background: #9b479f;
}
.card.bg-color-3 {
background: #4e484e;
}
.card.bg-color-4 {
background: #f70d1a;
}

Here is a PHP-only solution. Just set an array with the desired colors and use modulo to cycle through them as you iterate over the cards. Put a style attribute on the card to set the background color.
<div class="row gutter-XS">
<?php
$colors = [
'#0298cf',
'#9b479f',
'#4e484e',
'#f70d1a',
];
$count = count($colors);
$i = 0; // increment this counter at the end of the foreach loop
foreach ($dispcnt as $object)
{
?><div class="col-xs-3 col-md-3">
<div class="card BG-info" style="background:<?php echo $colors[($i % $count)]; ?>"> <!-- card -->
<div class="card-values">
<div class="p-x">
<small><?php echo $object->QA;?> </small>
<h3 class="card-title-l"><?php echo $object->CNT;?> Counts</h3>
</div>
</div>
</div>
</div> <!-- code of card ends here -->
<?php $i++;}?>
</div>

Related

How do I make the card align on the same row?

I'm trying to display MySQL query in each card but how do I print the cards inline? It is now printing below one another but I want them to be on the same row horizontally.
<?php
$output="";
while($row = mysqli_fetch_array($result))
{
$id = $row['courseID'];
$name = $row['courseName'];
$category = $row['category'];
$description = $row['description'];
$output.=
"
<div class='col-sm-12'>
<div class='col-md-4 mb-5'>
<div class='card h-100'>
<div class='card-body'>
<h4 class='card-title'>$name</h4>
<p class='card-text'>$id</p>
<p class='card-text'>$description</p>
</div>
<div class='card-footer'><a href='#' class='btn btn-primary'>Assign</a></div>
</div>
</div>";
}
$output .= "</div></div>";
echo $output;
?>
just move your while into this :
<div class="container">
<div class="row">
</div>
</div>
<link href="https://cdn.jsdelivr.net/npm/bootstrap#5.1.1/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-F3w7mX95PdgyTmZZMECAngseQB83DfGTowi0iMjiWaeVhAn4FJkqJByhZMI3AhiU" crossorigin="anonymous">
<!-- Option 1: Bootstrap Bundle with Popper -->
<script src="https://cdn.jsdelivr.net/npm/bootstrap#5.1.1/dist/js/bootstrap.bundle.min.js" integrity="sha384-/bQdsTh/da6pkI1MST/rWKFNjaCP5gBSY4sEBT38Q/9RBh9AH40zEOg7Hlq2THRZ" crossorigin="anonymous"></script>
<div class="container">
<div class="row">
<!--from here-->
<div class="col-lg-4 col-md-6 col-sm-12 mb-5 mt-5">
<div class="card">
<div class="card-body">
<div class="card-title">
<h1>title</h1>
</div>
<div class="card-text">
<h5>body</h5>
<a class="btn btn-outline-primary rounded-0 float-end" href="" name="readmore">Read more</a>
</div>
</div>
</div>
</div>
<!--to here-->
</div>
</div>
duplicate the code between (from here) and (to here)
here is a for loop example I used a while ago:
<div class="container">
<div class="row">
<?php for ($i=0; $i < count($user_elements[0]) ; $i++):?>
<div class="col-lg-4 col-md-6 col-sm-12 mb-5 mt-5">
<div class="card">
<img class="card-img-top" src="<?php if(isset($user_elements)){echo "../private/uploads/".$thumbnail[$i];}?>">
<div class="card-body">
<div class="card-title">
<h1><?php echo $user_elements[4][$i];?></h1>
</div>
<div class="card-text">
<h5><?php echo $user_elements[5][$i];?></h5>
<a class="btn btn-outline-primary rounded-0 float-end" href="readmore.php?Post=<?php echo $user_elements[0][$i];?>" name="readmore">Read more</a>
</div>
</div>
</div>
</div>
<?php endfor?>
</div>
</div>
create a parent div with container as a class add one more div to that parent div with row as a class then add your cards in that row classed div.
ex:
<div class="container">
<div class="row">
//your cards div
</div>
</div>
Use this snippet. Grid can be better with grid-gap.
<!DOCTYPE html>
<html>
<head>
<style>
.grid-container {
display: grid;
grid-template-columns: repeat(3, 1fr);
grid-column-gap: 0.5rem;
grid-row-gap: 0.5rem;
padding: 10px;
}
.grid-item {
background-color: #999;
border: 1px solid #fff;
padding: 20px;
font-size: 30px;
text-align: center;
}
</style>
</head>
<body>
<div class="grid-container">
<div class="grid-item">1</div>
<div class="grid-item">2</div>
<div class="grid-item">3</div>
<div class="grid-item">4</div>
<div class="grid-item">5</div>
<div class="grid-item">6</div>
<div class="grid-item">7</div>
<div class="grid-item">8</div>
<div class="grid-item">9</div>
</div>
</body>
</html>
You can make a new div before your while loop and join the
As you can see below, you will make a new div named cards
<?php
$output="<div class='cards' style='white-space:nowrap'>";
while($row = mysqli_fetch_array($result))
{
$id = $row['courseID'];
$name = $row['courseName'];
$category = $row['category'];
$description = $row['description'];
$output.=
"
<div class='col-sm-12' style='display:inline-block'>
<div class='col-md-4 mb-5'>
<div class='card h-100'>
<div class='card-body'>
<h4 class='card-title'>$name</h4>
<p class='card-text'>$id</p>
<p class='card-text'>$description</p>
</div>
<div class='card-footer'><a href='#' class='btn btn-primary'>Assign</a></div>
</div>
</div>";
}
$output .= "</div></div>";
echo $output;
?>
I hope by using this code the cards will align in same line.

Div layout changing after video inserted

I've been trying to create a simple bulletin board in which I can view different announcements, images, and videos. I have this UI bug in DIV whenever i'm trying to display video content at 100% width and height.
I wan't them to move up or stay on their position even if i inserted a long message or video.
This is the sample image of the one i'm talking about
as you see I want them to move up as they basically moved down after the video was inserted.
Here's the code i used.
For the grid css i used this
<style>
.grid-container {
display: grid;
height: 100%;
grid-template-columns: 1fr 1.75fr 1fr;
grid-template-rows: 0.75fr 1fr 1fr 1fr 1fr;
grid-template-areas: "Logo . ." ". videos ." ". videos ." ". . ." ". . .";
}
.videos {
grid-area: videos;
}
.Logo {
grid-area: Logo;
}
.Welcome Text {
grid-area: 1 / 2 / 1 / 2;
}
.Calendar {
grid-area: 1 / 3 / 3 / 3;
}
.Posts {
grid-area: 2 / 1 / 6 / 1;
}
.Schedule {
grid-area: 3 / 3 / 6 / 3;
}
.Images {
grid-area: 4 / 2 / 6 / 2;
}
and to display the whole content I used this one
<div class="grid-container">
<div class="videos">
<div class="row">
<div class="col-md-12" align="center">
<div class="card border-success">
<div class="card-header bg-theme"><h4 style="color: white">Video Section</h4></div>
<div class="card-body">
<div>
<?php
$value = custom_query("SELECT * FROM tbl_upload_video ORDER by RAND() DESC LIMIT 1");
if($value->rowCount()>0)
{
while($row=$value->fetch(PDO::FETCH_ASSOC))
{
$id = $row['id'];
?>
<?php
$img = substr($row['video_path'], 6); ?>
<video id="videos" autoplay>
<source src="<?= $img ?>" type="video/mp4">
</video>
<?php }} ?>
</div>
</div>
</div>
</div>
</div>
</div>
<div class="Logo" style="max-resolution: -50px;">
<div class="col-md-12" align="center">
<div class="card card-accent-theme ">
<div class="card-body">
<h3>INFO BOARD</h3>
</div>
</div>
</div>
</div>
<div class="Welcome Text">
<div class="col-md-12" align="center">
<div class="card card-accent-theme ">
<div class="card-body">
<h3 style="text-align: center">Welcome Students!!!!</h3>
</div>
</div>
</div>
</div>
<div class="Calendar">
<div class="col-md-12" align="center">
<div class="card card-accent-theme ">
<div class="card-body">
<h3 style="text-align: center">Calendar and other things</h3>
</div>
</div>
</div>
</div>
<div class="Posts">
<div class="col-md-12" align="center">
<div class="card border-success">
<div class="card-header bg-theme"><h4 style="color: white">Posts</h4></div>
<div class="card-body">
<?php
$dn = datenow();
$value = custom_query("SELECT * FROM tbl_post WHERE date2 >= '$dn' AND type = 'Announcement' and status = 'Active' ORDER by id DESC");
if($value->rowCount()>0)
{
while($row=$value->fetch(PDO::FETCH_ASSOC))
{
$id = $row['id'];
$d2 = $row['date2'];
$d = $row['date'];
?>
<div class="card mb-3" style="max-width: 50rem;">
<div class="card-header bg-warning"><?= $row['title']; ?></div>
<div class="card-body">
<?= $row['content']; ?>
</div>
</div>
<?php }} else {
echo "<h3>No Content Available</h3>";
} ?>
</div>
</div>
</div>
</div>
<div class="Schedule">
<div class="col-md-12" align="center">
<div class="card border-warning">
<div class="card-header bg-theme"><h4 style="color: white">Schedules</h4></div>
<div class="card-body">
<?php
$dn = datenow();
$value = custom_query("SELECT * FROM tbl_post WHERE date2 >= '$dn' AND type = 'Schedule' and status = 'Active' ORDER by id DESC");
if($value->rowCount()>0)
{
while($row=$value->fetch(PDO::FETCH_ASSOC))
{
$id = $row['id'];
$d2 = $row['date2'];
$d = $row['date'];
?>
<div class="card mb-3" style="max-width: 50rem;">
<div class="card-header bg-success"><?= $row['title']; ?></div>
<div class="card-body">
<?= $row['content']; ?>
</div>
</div>
<?php }} else {
echo "<h3>No Content Available</h3>";
} ?>
</div>
</div>
</div>
</div>
<div class="Images">
<div class="col-md-12" align="center">
<div class="card card-accent-theme ">
<div class="card-body">
<h3 style="text-align: center">Images</h3>
</div>
</div>
</div>
</div>
Thank you so much :)
I already fixed this one. Instead of using CSS GRID I revert back to bootstrap grid using div and col

SAMP PHP API Integration

I want to show the number of players in the server instead of that UNKNOWN thing, I haven't enabled the API yet, can I get help that, I want to integrate the progress bar to the total players in game like
if players are 50/100
= 50% of the bar will be filled.
Like the bar should work as the api updates.
<div class="is-clearfix"></div>
<section class="section dark-grey has-text-centered">
<div class="container">
<div class="heading">
<h1 class="title">Our Servers</h1>
<br>
</div>
<div class="columns is-gapless">
<div class="column">
<div class="card card-server">
<div class="card-image">
<figure class="image is-2by1">
<img src="./OutBreak Gaming_files/sfcnr_card1.png">
</figure></div> <div class="card-content">
<div class="title" style="font-size: 1.25em; color: rgb(119, 119, 119); margin-top: 0.5em;">87.98.241.207:7775</div>
<div class="players is-marginless">
<div class="content playersOnline">
Unknown PLAYERS ONLINE
</div>
<progress value="0" max="100" class="progress is-success"></progress>
<br></div> <a class="button is-info is-medium is-fullwidth">Play Now</a>
</div>
</div>
</div>
</div>
</div>
</section>
</div>
Just use the SACNR monitor API, and
<div class="columns is-gapless">
<div class="column"><div class="card card-server">
<div class="card-image">
<figure class="image is-2by1">
<img src="./OutBreak Gaming_files/sfcnr_card1.png"></figure></div>
<div class="card-content">
<div class="title" style="font-size: 1.25em; color: rgb(119, 119, 119); margin-top: 0.5em;">87.98.241.207:7775</div> Hostname:
<div class="hosting"></div>Gamemode:
<div class="game-mode"></div>Map:
<div class="language"></div> Players:
<div class="players is-marginless"><div class="content playersOnline">
FETCHING PLAYERS ONLINE
</div>
<progress value="0" max="100" class="progress is-success"></progress><br></div>
<a class="button is-info is-medium is-fullwidth">Play Now</a></div></div></div></div></div></section></div>
Where We fetch from API the following things
<?php
require_once("sampsvr.php");
$monitor = new SACNR\Monitor;
//$obj = $monitor->get_info_by_id(1790345);
$obj = $monitor->get_info_by_ip('87.98.241.207','7775');
$players = $obj->Players;
$hostName = $obj->Hostname;
$Gamemode = $obj->Gamemode;
$Mapname = $obj ->Language;
?>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$('.progress').val(<?php echo $players;?>);
$('.playersOnline').html(<?php echo $players;?> +' PLAYERS ONLINE' );
$('.hosting').html("<?php echo $hostName;?>");
$('.game-mode').html("<?php echo $Gamemode;?>");
$('.language').html("<?php echo $Mapname;?>");
});
</script>
You may find the API at http://monitor.sacnr.com/api.html

Making Photo Grid with Different size images

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>

Displaying parent div horizontally beyond width of page

<div class="parent">
<div class="child">
</div>
<div class="child">
</div>
<div class="child">
</div>
<div class="child">
</div>
<div class="child">
</div>
</div>
The height of the child class is 20% and the width 200px.
What I'm wondering is how to make it so that when I have, for example, 20 parent divs it is set up so that they are all displayed horizontally, beyond the width of the page, so that I have to scroll left or right to view the data. At the moment it fills the width of the page and starts the next parent div underneath the first one so that I have to scroll vertically and I can't fathom how to rectify this problem.
Thanks in advance.
To do this you need to create a parent container, and then assign a overflow-x: scroll css rule to it. You will also need to assign a display: inline-block rule to your parent class. You also need to apply white-space: nowrap;.
DEMO http://jsfiddle.net/qujyunn4/
.wrapper {
overflow-x: scroll;
white-space: nowrap;
}
.parent {
display: inline-block;
width: 200px;
height: 300px;
border: red solid thin;
margin-right: 5px;
white-space: nowrap;
}
<div class="wrapper">
<div class="parent">
<div class="child">
</div>
<div class="child">
</div>
<div class="child">
</div>
<div class="child">
</div>
<div class="child">
</div>
</div>
<div class="parent">
<div class="child">
</div>
<div class="child">
</div>
<div class="child">
</div>
<div class="child">
</div>
<div class="child">
</div>
</div>
<div class="parent">
<div class="child">
</div>
<div class="child">
</div>
<div class="child">
</div>
<div class="child">
</div>
<div class="child">
</div>
</div>
<div class="parent">
<div class="child">
</div>
<div class="child">
</div>
<div class="child">
</div>
<div class="child">
</div>
<div class="child">
</div>
</div>
</div>

Categories