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
Related
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 making paging from table using the following code my table name is category and 2nd one posts and the one with sit setting setting
<div id="wrap">
<div class="container" style="margin-top:50px;">
<div class="row">
<?php
$per_page =8;
if (!isset($_GET['page']))
{
$page = 1;
}
else
{
$page = (int)$_GET['page'];
}
$start_f = ($page-1) * $per_page;
$query = mysqli_query($conn, " SELECT * FROM `posts` p INNER JOIN `users` u
WHERE p.p_author = u.u_id AND `p_category` = '$_GET[category]' ORDER BY `p_id` DESC LIMIT $start_f , $per_page" );
while ( $post = mysqli_fetch_assoc($query)) {
# code...
?>
<div class="col-xs-12 col-sm-6 col-md-3" style="margin-top: 20px;">
<div class="col-item" >
<div class="post-img-content">
<img src="<?php echo $post['p_image']; ?>" style="width: 100%; height: 100%;" class="img-responsive" />
<span class="post-title">
<b class="pull-left"><?php echo $post['p_title']?></b>
</span>
</div>
<div>
<div class="info" >
<div class="row ">
<div class="col-md-12"></div>
<div class="price col-md-6">
<h5><b><?php echo $post['p_category'];?></b></h5>
</div>
<div class=" hidden-sm col-md-6">
<h5 style="text-align: right;"><a href="admin-cp/profile.php?user=<?php echo $post['u_id']; ?>"><b ><?php echo
$post['u_name'];?></b></a></h5>
</div>
<div class=" hidden-sm col-md-12">
<h5><b>Date : </b><?php echo $post['p_date'];?></h5>
</div>
</div>
<div class="separator clear-left">
<p style="text-align: left;">
<i class="fa fa-eye"></i>View</p>
</div>
<div class="clearfix">
</div>
</div>
</div>
</div>
</div>
<?php
}
$page_sql = mysqli_query($conn, "SELECT * FROM
`posts` WHERE `p_category` = '$_GET[category]'");
$count_page = mysqli_num_rows($page_sql);
$total_page = ceil($count_page/$per_page); /// cail for make value int
?>
<nav class="text-center">
<ul class="pagination">
<?php
for($i = 1 ; $i <= $total_page; $i++)
{
echo '<li '.($page==$i ? 'class="active"' : '').' >'.$i.'</li>';
}
?>
</ul>
</nav>
</div>
this http://localhost/p/category.php?category=Computers work and get me the first page but that http://localhost/p/category.php?category=Computers?page=1 give me 0 result
pleas help me it's very important to me
You link concatenation is wrong, use & instead of ? again:
<?php
}
$page_sql = mysqli_query($conn, "SELECT * FROM
`posts` WHERE `p_category` = '$_GET[category]'");
$count_page = mysqli_num_rows($page_sql);
$total_page = ceil($count_page/$per_page); /// cail for make value int
?>
<nav class="text-center">
<ul class="pagination">
<?php
for($i = 1 ; $i <= $total_page; $i++)
{
echo '<li '.($page==$i ? 'class="active"' : '').' >'.$i.'</li>';
}
?>
I'm using a while loop to echo a PHP array :
$connect = mysqli_connect("localhost", "user", "password", "db");
$sql = "SELECT * FROM table ORDER BY RAND() LIMIT 0,4";
$result = mysqli_query($connect, $sql);
if(mysqli_num_rows($result) > 0)
{
echo'<section>
<div class="container">
<div class="row">
<h2 class="bold">title</h2>
<hr> ';
while($row = mysqli_fetch_array($result))
{
echo'
<div class="col-sm-3 col-md-3 col-md-push-3">
<div class="portfolio-wrapper">
<div class="portfolio-single">
<div class="portfolio-thumb">
<img src="'.$row['image'].'" class="img-responsive" alt="'.$row['alt'].">
</div>
</div>
<div class="portfolio-info" dir="rtl">
<a href="'.$row['link2']." target="_self"><h2> </h2>
<h6>'.$row['title'].</h6>
</a>
</div>
</div>
</div>';
}
echo'
</div>
</div>
</section';
}
?>
This code works fine for me but the problem is that i need to echo 4 different
for example :
first will be: <div class="col-sm-3 col-md-3 col-md-push-9">
second: <div class="col-sm-3 col-md-3 col-md-push-3">
third: <div class="col-sm-3 col-md-3 col-md-pull-3">
last: <div class="col-sm-3 col-md-3 col-md-pull-9">
any ideas how can i accomplish this structure using a while loop ?
Use a counter, and put your classes into an array like this:
$classes = [
"col-sm-3 col-md-3 col-md-push-9",
"col-sm-3 col-md-3 col-md-push-3",
"col-sm-3 col-md-3 col-md-pull-3",
"col-sm-3 col-md-3 col-md-pull-9"
];
$i = 0;
while($row = mysqli_fetch_array($result)) {
if ($i > 3) {
$i = 0;
}
?>
<div class="<?php echo $classes[$i]; ?>">
Your content comes here
</div>
<?php
$i++;
}
NOTE
This is resets the counter, if there is no index like that in the array. Your script assumes, that you will get always 4 rows.
<?php
$connect = mysqli_connect("localhost", "user", "password", "db");
$sql = "SELECT * FROM table ORDER BY RAND() LIMIT 0,4";
$result = mysqli_query($connect, $sql);
$i = 1;
$count = count($result);
if(mysqli_num_rows($result) > 0)
{
echo'<section>
<div class="container">
<div class="row">
<h2 class="bold">title</h2>
<hr> ';
while($row = mysqli_fetch_array($result))
{
if($i==1){echo '<div class="col-sm-3 col-md-3 col-md-push-9">';}
elseif($i == $count){echo '<div class="col-sm-3 col-md-3 col-md-pull-9">';}
elseif($i%2 == 0){echo '<div class="col-sm-3 col-md-3 col-md-push-3">';}
elseif($i%3 == 0){echo '<div class="col-sm-3 col-md-3 col-md-pull-9">';}
echo'
<div class="portfolio-wrapper">
<div class="portfolio-single">
<div class="portfolio-thumb">
<img src="'.$row['image'].'" class="img-responsive" alt="'.$row['alt'].">
</div>
</div>
<div class="portfolio-info" dir="rtl">
<a href="'.$row['link2']." target="_self"><h2> </h2>
<h6>'.$row['title'].</h6>
</a>
</div>
</div>
</div>';
$i =$i+1;
}
echo'
</div>
</div>
</section';
}
?>
this is the code:
<div id="spinner" class=" col-lg-13">
<div class="line"></div>
<div class="spinner">
<ul class="cont"> </ul>
</div>
</div>
<div class="col-lg-13 col border-right col-spinner">
<div class="col-xs-12 col-sm-8 col-md-8 icon-col">
<div class="users-online ico">
<div class="img"></div>
<span id="users-online" class="h3">
<?php
$result = mysqli_query($link, "SELECT id FROM games WHERE `starttime` > ".(time()-86400));
$result2 = mysqli_query($link, "SELECT id FROM users WHERE `lastseen` > ".(time()-86400));
echo mysqli_num_rows($result2);
?>
</span>
<?php echo $msg[$lang]["ptd"]; ?>
</div>
</div>
<div class="col-xs-12 col-sm-8 col-md-8 icon-col">
<div class="games-today ico">
<div class="img"></div>
<span id="games-today" class="h3">
<?php
$result2 = mysqli_query($link, "SELECT id FROM users WHERE `lastseen` > ".(time()-86400));
echo mysqli_num_rows($result);
?>
</span>
<?php echo $msg[$lang]["gtd"]; ?>
</div>
</div>
<div class="col-sm-8 col-md-8 icon-col hidden-xs">
<div class="biggest-win ico">
use your queries like this:
$result = mysqli_query($link, "SELECT id FROM games WHERE `starttime` > '".(time()-86400))."'";
$result2 = mysqli_query($link, "SELECT id FROM users WHERE `lastseen` > '".(time()-86400))."'";
echo mysqli_num_rows($result2);
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
}
}