I imported a database in phpMyAdmin that contains articles. I want it to be regulated, to automatically post the image, title, and contents in the articles page. And to direct it to another page when clicked.
But when rendering the articles page, the second row has two columns offset. I think it's the content of the first 2 articles that's making the problem since I did another tables with few contents in it then it renders exactly the way I want(a row that contains 3 articles an no offset below it). I'm a newbie here, feel free to suggest if there's a better way. I genuinely appreciate it. Thank you!
<div class="blogs-2" id="blogs-2">
<div class="container">
<div class="row">
<div class="col-md-10">
<div class="row">
<?php
$sql = "SELECT * FROM news LIMIT 20";
$result= $DBcon->query($sql);
if ($result->num_rows > 0) {
while ($row = $result->fetch_assoc()) {
?>
<div class="col-md-4" style="margin-top:0;margin-bottom:0;padding-top:0;padding-bottom:0; ">
<div class="card card-plain card-blog" style="max-height:300px;float:left;">
<div class="card-image">
<a href="../news/article.php?id=<?php echo $row['id']; ?>">
<img class="img img-raised" src="<?php header("Content-type: image/png"); echo $row['featured_image'] ?>" style="max-height:300px;" />
</a>
</div>
<div class="card-content">
<h4 class="card-title">
<a href="../news/article.php?id=<?php echo $row['id']; ?>">
<?php echo $row['title']; ?>
</a>
</h4>
<p class="card-description" style="overflow: hidden;display: -webkit-box;-webkit-line-clamp: 3;-webkit-box-orient: vertical;">
<?php echo $row['content']; ?>
</p>
<p class="card-description">
Read More
</p>
</div>
</div>
</div>
<?php
}
}
else {
echo "<p>No News Articles to Display</p>";
}
?>
</div>
</div>
<div class="col-md-2">
<h4 class="title">Recent News</h4>
<ul>
<?php
$sql = "SELECT * FROM news LIMIT 5";
$result= $DBcon->query($sql);
if ($result->num_rows > 0) {
while ($row = $result->fetch_assoc()) {
echo "<li style='list-style:none;color:black;padding-top:5px;padding-bottom:5px;'><a href='../news/article.php?id={$row['id']}'>" .$row['title']. "</a></li>";
}
}
else {
echo "<li style='list-style:none;'>No Recent News</li>";
}
?>
</ul>
</div>
</div><!--row-->
</div><!--container-->
</div><!--blogs-->
Related
Please Help Me, How can I Run two While Loop one after another. I want to get all the rows of database. If I put the manual-control DIV inside first while loop it shows the whole div separately. and when placed it outside it do nothing. Please rewrite the code.
I want to get value like this_
<div class="img-slider-container">
<div class="img-slide active">
<img src="img/1.jpg" alt="Image">
<div class="img-info">
<h2>Heading 1</h2>
<p>Image Text 1</p>
</div>
</div>
<div class="img-slide">
<img src="img/2.jpg" alt="Image">
<div class="img-info">
<h2>Heading 2</h2>
<p>Image Text 2</p>
</div>
</div> <!--Continue-->
<div class="manual-control">
<div class="slide-btn 1"></div>
<div class="slide-btn 2"></div> <!--Continue-->
</div>
My Code Sample is given bellow_
<div class="img-slider-container">
<?php
$sql = "SELECT * FROM $tbImadeSlide";
$stmt = mysqli_stmt_init($conn);
if (!mysqli_stmt_prepare($stmt, $sql)) {
header("Location: index.php?error=sqlerror");
exit();
}
else{
$result = mysqli_query($conn, $sql);
$resultCheck = mysqli_num_rows($result);
if ($resultCheck > 0){
while($row = mysqli_fetch_assoc($result)){
echo '
<div class="img-slide '.$row["imageStatus"].'">
<img src="img/'.$row["imageName"].'" alt="Image">
<div class="img-info">
<h2>'.$row["imageHeader"].'</h2>
<p>'.$row["imageText"].'</p>
</div>
</div>';
}
echo '<div class="manual-control">';
while($row = mysqli_fetch_assoc($result)){
echo '<div class="slide-btn '.$row["imageId"].'"></div>';}
echo '</div>';
} else{
echo "<div class='no_result'>No Photos To Slide</div>";
}
}
?>
But the code just repeat first while loop like this_
<div class="img-slider-container">
<div class="img-slide active">
<img src="img/1.jpg" alt="Image">
<div class="img-info">
<h2>Heading 1</h2>
<p>Image Text 1</p>
</div>
</div>
<div class="img-slide">
<img src="img/2.jpg" alt="Image">
<div class="img-info">
<h2>Heading 2</h2>
<p>Image Text 2</p>
</div>
</div> <!--Continue-->
<div class="manual-control">
<!--Nothing Shows-->
</div>
</div>
Without keep all the rows in array you can simply do this by foreach loop.
Just replace
while($row = mysqli_fetch_assoc($result)){ to foreach($result as $row){
Your code may look like:
<div class="img-slider-container">
<?php
$sql = "SELECT * FROM $tbImadeSlide";
$stmt = mysqli_stmt_init($conn);
if (!mysqli_stmt_prepare($stmt, $sql)) {
header("Location: index.php?error=sqlerror");
exit();
}
else{
$result = mysqli_query($conn, $sql);
$resultCheck = mysqli_num_rows($result);
if ($resultCheck > 0){
foreach($result as $row){
echo '
<div class="img-slide '.$row["imageStatus"].'">
<img src="img/'.$row["imageName"].'" alt="Image">
<div class="img-info">
<h2>'.$row["imageHeader"].'</h2>
<p>'.$row["imageText"].'</p>
</div>
</div>';
}
echo '<div class="manual-control">';
foreach($result as $row){
echo '<div class="slide-btn '.$row["imageId"].'"></div>';}
echo '</div>';
} else{
echo "<div class='no_result'>No Photos To Slide</div>";
}
}
?>
I'm fetching data from mysql table with mysqli fetch array and after the while of fetching i want to the carousel item showing what his index number from the array so i can identify them using the index number for giving active property
<?php
$qry = mysqli_query($koneksi, "SELECT * FROM t_berita ORDER BY tanggal DESC LIMIT 3");
while($row = mysqli_fetch_array($qry, MYSQLI_ASSOC)){
$qrygbr = mysqli_query($koneksi, "SELECT * FROM t_gambar WHERE id_berita = '".$row['id']."'");;
$rowcount=mysqli_num_rows($qry);
while ($rowgbr = mysqli_fetch_array($qrygbr, MYSQLI_ASSOC)) {
# code...
?>
<div class="carousel-item active">
<div class="card">
<div class="card-body" style="padding: 0;">
<div class="row">
<div class="col-lg-6"><img class="img-fluid" style="width: 100%;height: 100%;background-image: url(data:image/jpeg;base64,<?php echo base64_encode($rowgbr['gambar']);?>);background-position: center;background-size: cover;background-repeat: no-repeat;" /></div>
<div class="col-lg-6">
<div class="d-md-flex d-lg-flex flex-column justify-content-md-center align-items-md-center justify-content-lg-center align-items-lg-start" style="padding-top: 20px;padding-left: 10px;padding-right: 10px;height: 100%;margin-bottom: 20px;">
<h4><?php
echo $row['judul'];
?></h4>
<h6 class="text-muted card-subtitle mb-2">
<?php
echo $row['tanggal'].$rowgbr[];
?>
</h6>
<p>
<?php
echo $row['artikel'];
?>
</p>
<p>
</p>
<div class="d-flex"></div><a class="btn btn-primary" role="button" href="bacaberita.php?id=<?php echo $row['id'] ?>">Read More</a></div>
</div>
</div>
</div>
</div>
</div>
<?php
}
}
mysqli_close($koneksi);
?>
change the 2nd query
$qrygbr = mysqli_query($koneksi, "SELECT #a:=#a+1 serial_number,t_gambar.* FROM t_gambar, (SELECT #a:= 0) AS a WHERE t_gambar.id_berita = '".$row['id']."'");
while ($rowgbr = mysqli_fetch_array($qrygbr, MYSQLI_ASSOC)) {
echo $rowgbr['serial_number']; //this is you your needed serial number
}
The following should help you understand what you need to do. I've applied a $count variable within the loop to increase as the loop continues. I've added the variable to a to show you how it works.
<?php
$koneksi = mysqli_connect('localhost', 'admbumm_DedSec', 'omnimon786.,', 'admbumm_selipin');
$qry = mysqli_query($koneksi, "SELECT * FROM t_berita ORDER BY tanggal DESC LIMIT 3");
while($row = mysqli_fetch_array($qry, MYSQLI_ASSOC)){
$qrygbr = mysqli_query($koneksi, "SELECT * FROM t_gambar WHERE id_berita = '".$row['id']."'");
$rowcount=mysqli_num_rows($qry);
$count = 1;
while ($rowgbr = mysqli_fetch_array($qrygbr, MYSQLI_ASSOC)) {
# code...
?>
<div class="carousel-item active">
<div class="card">
<div class="card-body" style="padding: 0;">
<div class="row">
<div class="col-lg-6"><img class="img-fluid" style="width: 100%;height: 100%;background-image: url(data:image/jpeg;base64,<?php echo base64_encode($rowgbr['gambar']);?>);background-position: center;background-size: cover;background-repeat: no-repeat;" /></div>
<div class="col-lg-6">
<div class="d-md-flex d-lg-flex flex-column justify-content-md-center align-items-md-center justify-content-lg-center align-items-lg-start" style="padding-top: 20px;padding-left: 10px;padding-right: 10px;height: 100%;margin-bottom: 20px;">
<h4><?php
echo $row['judul'];
?></h4>
<h6 class="text-muted card-subtitle mb-2">
<?php
echo $row['tanggal'].$rowgbr[];
?>
</h6>
<p>
<?php
echo $row['artikel'];
?>
</p>
<p>
<p>
<?php
echo $count;
?>
</p>
</p>
<div class="d-flex"></div><a class="btn btn-primary" role="button" href="bacaberita.php?id=<?php echo $row['id'] ?>">Read More</a></div>
</div>
</div>
</div>
</div>
</div>
<?php
$count = $count + 1;
}
}
mysqli_close($koneksi);
?>
you might want to add another variable inside your SQL Query like so
$qry = mysqli_query($koneksi, "SELECT * FROM t_berita ORDER BY tanggal DESC LIMIT 3");
to
$qry = mysqli_query($koneksi, "SELECT COUNT(*) as c,* FROM t_berita ORDER BY tanggal DESC LIMIT 3");
basically, SQL will count the total rows available and put it c. So you can just do some simple loop with c inside your PHP code
i need some help with one problem, i tried to make a card carousel getting data from my mysql database, all went well but the cards are displayed under each other not like it should be displayed in a carousel/slider, any ideas how could i fix this to show them in one line?
here is my code and a picture:
<?php
include_once 'includes/dbh.inc.php';
?>
<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
<link rel="stylesheet" type="text/css" href="css/style.css">
<link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" />
<link href="//maxcdn.bootstrapcdn.com/font-awesome/4.5.0/css/font-awesome.min.css" rel="stylesheet" />
<link href="//maxcdn.bootstrapcdn.com/font-awesome/4.3.0/css/font-awesome.min.css" rel="stylesheet" />
<meta charset="utf-8">
<title>carusel</title>
</head>
<body>
<section class="container p-t-3">
<div class="row">
<div class="col-lg-12">
<h1>Bootstrap 4 Card Slider</h1>
</div>
</div>
</section>
<section class="carousel slide" data-ride="carousel" id="postsCarousel">
<div class="container">
<div class="row">
<div class="col-xs-12 text-md-right lead">
<a class="btn btn-outline-secondary prev" href="" title="go back"><i class="fa fa-lg fa-chevron-left"></i></a>
<a class="btn btn-outline-secondary next" href="" title="more"><i class="fa fa-lg fa-chevron-right"></i></a>
</div>
</div>
</div>
<div class="container p-t-0 m-t-2 carousel-inner">
<div class="row row-equal carousel-item active m-t-0">
<div class="col-md-4">
<?php
$sql = "SELECT * FROM users;";
$results = mysqli_query($conn, $sql);
$resultCheck = mysqli_num_rows($results);
if ($resultCheck > 0){
while ($row = mysqli_fetch_assoc($results)) { ?>
<div class="card" style="width: 18rem;">
<div class="card-img-top">
<img class="img-fluid" src="<?php echo $row['avatar']; ?>" alt="Carousel">
</div>
<div class="card-body">
<h5 class="card-title"><?php echo $row['user_first'].' '.$row['user_last']; ?></h5>
<p><?php echo $row['card_text_profil']; ?></p>
Profil site
</div>
</div>
<?php } ?>
<?php } ?>
</div>
</div>
</div>
</section>
</body>
<script src="//ajax.googleapis.com/ajax/libs/jquery/2.2.4/jquery.min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/popper.js/1.13.0/umd/popper.min.js"></script>
<script src="//maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js"></script>
<script src="js/scripts.js"></script>
</html>
You're closing 2 </div> inside the while loop, but they are being opened before the while loop, you need to add them to the beginning of the while loop.
Added a condition to check if it's the first element in the carousel, and if it is, add the active class as required per the documentation.
Initial active element required The .active class needs to be added to
one of the slides. Otherwise, the carousel will not be visible.
The code I suggest as an answer makes it a lot more readable, this makes it a lot easier to prevent problems of this nature. Indentation goes a long way to help you see clearly where a block/loop starts and where it ends.
Working minimal fiddle here.
Replace:
<div class="container p-t-0 m-t-2 carousel-inner">
<div class="row row-equal carousel-item active m-t-0">
<div class="col-md-4">
<?php
$sql = "SELECT * FROM users;";
$results = mysqli_query($conn, $sql);
$resultCheck = mysqli_num_rows($results);
if ($resultCheck > 0){
while ($row = mysqli_fetch_assoc($results)) {
//echo '<tr><th scope="row">'.$row['user_id'].'</th><td>'.$row['user_last']."</td><td>".$row['user_uid']."</td></tr>";
/*echo '<div class="card" style="width: 18rem;">';
echo '<img class="card-img-top" src="'.$row['avatar'].'" alt="Card image cap">';
echo '<div class="card-body">';
echo '<h5 class="card-title">'.$row['user_first'].' '.$row['user_last'].'</h5>';
echo '<p class="card-text">'.$row['card_text_profil'].'</p>';
echo 'Profil site';
echo '</div>';
echo '</div>';*/
echo '<div class="card" style="width: 18rem;">';
echo '<div class="card-img-top">';
echo '<img class="img-fluid" src="'.$row['avatar'].'" alt="Carousel">';
echo'</div>';
echo '<div class="card-body">';
echo '<h5 class="card-title">'.$row['user_first'].' '.$row['user_last'].'</h5>';
echo '<p class="card-text">'.$row['card_text_profil'].'</p>';
echo 'Profil site';
echo '</div>';
echo '</div>';
echo '</div>';
echo '</div>';
}
}
?>
</div>
With:
<div class="container p-t-0 m-t-2 carousel-inner">
<?php
$sql = "SELECT * FROM users;";
$i = 1;
$results = mysqli_query($conn, $sql);
$resultCheck = mysqli_num_rows($results);
if ($resultCheck > 0){
while ($row = mysqli_fetch_assoc($results)) { ?>
<?php if($i % 2 != 0): //add new slider every odd row ?>
<div class="row row-equal carousel-item <?php echo ($i == 1) ? "active" : ""; ?> m-t-0">
<?php endif; ?>
<div class="col-md-4">
<div class="card" style="width: 18rem;">
<div class="card-img-top">
<img class="img-fluid" src="<?php echo $row['avatar']; ?>" alt="Carousel">
</div>
<div class="card-body">
<h5 class="card-title"><?php echo $row['user_first'].' '.$row['user_last']; ?></h5>
<p><?php echo $row['card_text_profil']; ?></p>
Profil site
</div>
</div>
</div>
<?php if($i % 2 != 0): //add new slider every odd row ?>
</div>
<?php endif; ?>
<?php $i++; } ?>
<?php } ?>
</div>
what about this css files do you float the picture divs?
Grades table is this:
username assignment weight mark
a a1 10% 50%
b a1 10% 60%
How would I print this out using php/html?
<div class="a">
<div class="divTableHeading">
<div class="divTableRow">
<div class="divTableHead">Username</div>
<div class="divTableHead">assignment</div>
<div class="divTableHead">weight</div>
<div class="divTableHead">mark</div>
</div>
</div>
<div class="divTableBody">
<?php
include("config.php");
$sql = "SELECT username, assignment, weight, mark FROM grades";
$result = mysqli_query($db,$sql);
while($row = $result->fetch_assoc()) {
?>
<div class="divTableRow">
<div class="divTableCell">$row['username']</div>
<div class="divTableCell">$row['assignment']</div>
<div class="divTableCell">$row['weight']</div>
<div class="divTableCell">$row['mark']</div>
</div>
</div>
</div>
$db is the connected database that is from config.php. How would I keep looping through and output the grades table?
accidental edit
You're missing to echo your record values.
From just $row['username'] changed to <?php echo $row['username']; ?>
And you missed } to close your while loop. Closed it as well with - <?php } ?>
Updated code:
<div class="divTable blueTable">
<div class="divTableHeading">
<div class="divTableRow">
<div class="divTableHead">Username</div>
<div class="divTableHead">assignment</div>
<div class="divTableHead">weight</div>
<div class="divTableHead">mark</div>
</div>
</div>
<div class="divTableBody">
<?php
include("config.php");
$sql = "SELECT username, assignment, weight, mark FROM grades";
$result = mysqli_query($db,$sql);
while($row = $result->fetch_assoc()) {
?>
<div class="divTableRow">
<div class="divTableCell"><?php echo $row['username']; ?></div>
<div class="divTableCell"><?php echo $row['assignment']; ?>/div>
<div class="divTableCell"><?php echo $row['weight']; ?></div>
<div class="divTableCell"><?php echo $row['mark']; ?></div>
</div>
<?php } ?>
</div>
</div>
Don't forget to output your values
<?php echo $row['username']; ?>
I was able to retrieve data from Mysql database however every 4 column I need to close row and start and new row
Following is my HTML code that I am trying to loop through columns and after every 4 columns close the row and keep adding columns from database.
Here is my code (I know this is not PDO and I am trying to learn so I can convert to PDO MySQLi connection)
<div class="row">
<div class="row margin-bottom-20">
<?php
include('dbconnect.php');
$query = "SELECT * FROM selection";
mysql_set_charset("UTF8");
$result = mysql_query($query) or die(mysql_error());
for($i=1; $row = mysql_fetch_array($result); $i++){
?>
<div class="col-md-3">
<div class="panel panel-default">
<div class="panel-heading">
<h3 class="panel-title"><i class="fa fa-tasks"></i> <strong><?php echo $row['Title']; ?></strong></h3>
</div>
<div class="panel-body">
<p>
<?php echo $row['ContactInfo']; ?><br/>
<img class="img-responsive" src="http://myurl.com/selections/<?php echo $row['file_url']; ?>" >
</p>
</div>
</div>
</div>
<?php
}
?>
I need to add a new for loop but I haven't been successful so far. Thank you for your help.
if($i%4==0)
{
//close the existing div and start new row div here
}
That will start a new row after every 4 iterations
Try this
<?php
include('dbconnect.php');
$query = "SELECT * FROM selection";
mysql_set_charset("UTF8");
$res = mysql_query($query) or die(mysql_error());
while($rows = mysql_fetch_array($res)){
$result[] = $rows;
}
$array = array_chunk($result, 4);
foreach ($array as $value) {
foreach ($value as $row){
?>
<div class="col-md-3">
<div class="panel panel-default">
<div class="panel-heading">
<h3 class="panel-title"><i class="fa fa-tasks"></i> <strong><?php echo $row['Title']; ?></strong></h3>
</div>
<div class="panel-body">
<p>
<?php echo $row['ContactInfo']; ?><br/>
<img class="img-responsive" src="http://myurl.com/selections/<?php echo $row['file_url']; ?>" >
</p>
</div>
</div>
</div>
<?php
}
}