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
}
}
Related
Every time I try and load the page below the images do not show. I have the image names in an SQL database and also have the image folder in the same path as my index.php.
<div class="container-fluid">
<div class="col-md-12">
<div class="row">
<div class="col-md-6">
<h2 class="text-center">Products</h2>
<?php
$query = "SELECT * FROM product";
$result = mysqli_query($connect,$query);
while ($row = mysqli_fetch_array($result)) {?>
<form method="get" action="index.php?id=<?= $row['id'] ?>">
<img src="img/<?= $row['image'] ?>" style='height: 150px;'>
<h2><?= $row['name']; ?></h2>
<h2><?= $row['price']; ?></h2>
</form>
<?php }
?>
</div>
<div class="col-md-6">
<h2 class="text-center">Shopping Cart</h2>
</div>
</div>
</div>
</div>
</body>
</html>
I tried this code on my computer and the image appears perfectly.
Perhaps, you forgot to place a connection query.
<div class="container-fluid">
<div class="col-md-12">
<div class="row">
<div class="col-md-6">
<h2 class="text-center">Products</h2>
<?php
$connect = mysqli_connect('localhost','root','','test');
$query = "SELECT * FROM product";
$result = mysqli_query($connect,$query);
while ($row = mysqli_fetch_array($result)) {?>
<form method="get" action="index.php?id=<?= $row['id'] ?>">
<img src="img/<?= $row['image'] ?>" style='height: 150px;'>
<h2><?= $row['name']; ?></h2>
<h2><?= $row['price']; ?></h2>
</form>
<?php }
?>
</div>
<div class="col-md-6">
<h2 class="text-center">Shopping Cart</h2>
</div>
</div>
</div>
</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
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 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-->
I have written CSS for two column layout and I have a "users" database with notes of the users.
So when I run a while loop like this: while($row = mysqli_fetch_array($result))
PHP should dynamically create html like this (depending on no. of users):
<div class="row">
<div class="col">
<div class="col-content"><?php echo $row['notes'] ?></div>
<!-- Notes of user 1 -->
</div>
<div class="col">
<div class="col-content"><?php echo $row['notes'] ?></div>
<!-- Notes of user 2 -->
</div>
</div>
<div class="row">
<div class="col">
<div class="col-content"><?php echo $row['notes'] ?></div>
<!-- Notes of user 3 -->
</div>
<div class="col">
<div class="col-content"><?php echo $row['notes'] ?></div>
<!-- Notes of user 4 -->
</div>
</div>
If there's is only one user, html should be like this:
<div class="row">
<div class="col">
<div class="col-content"><?php echo $row['notes'] ?></div>
<!-- Notes of user 1 -->
</div>
</div>
And similarly if there are 3 users, there should one row with two columns and another row with one column.
Is there any way to do that?
Edit: Added my php code here.
<?php
$query = "SELECT * FROM users";
$result = mysqli_query($conn, $query);
while($row = mysqli_fetch_array($result)){
?>
<div class="row">
<div class="col">
<div class="col-content"><?php echo $row['notes'] ?></div>
</div>
</div>
<?php } ?>
Problem with that code is it's creating a new row with "one column" for every user. My goal is to have "two columns" in the row.
you can achieve things like that with the modulo operator (%) within loops.
$i = 0;
$rowCount = mysqli_num_rows($result);
$rowsHTML = '';
while($row = mysqli_fetch_array($result)){
$notesHTML = '<div class="col">
<div class="col-content">'.$row['notes'].'</div>
<!-- Notes of user 1 -->
</div>';
if($i%2 == 0){
$notesHTML = '<div class="row">'.$notesHTML;
// check if its the last item and the row has to be closed after 1 col
if($i+1 == $rowCount){
$notesHTML = $notesHTML.'</div>';
}
} else {
$notesHTML = $notesHTML.'</div>';
}
$rowsHTML .= $notesHTML;
$i++;
}
echo $rowsHTML;
<?php
$query = "SELECT * FROM users";
$result = mysqli_query($conn, $query);
$i = 0;
?>
<div class="row">
<?php
while($row = mysqli_fetch_array($result)){
if($i>1 && $i%2==0) echo '</div><div class="row">';
$i++;
?>
<div class="col">
<div class="col-content"><?php echo $row['notes'] ?></div>
</div>
<?php } ?>
</div>