I'm trying to add a container every 2 iterations. I've implemented a count, but I'm getting weird results as I inspect the page.
Any ideas what may be going on and what I may be doing wrong with the wrapper injection?
<?php
include "includes2014_new/db_conx.php";
$sql ="SELECT * FROM articles WHERE id ORDER BY id DESC LIMIT 5";
$resultsHome = mysqli_query($db_conx,$sql);
$productCount = mysqli_num_rows($resultsHome); // count the output amount
if ($productCount > 0) {
$counter = 1;
while($row = mysqli_fetch_array($resultsHome)){
$id = $row["id"];
$article_title = $row["article_title"];
$category = $row["category"];
$readmore = $row["readmore"];
$author = $row["author"];
$date_added = $row["date_added"];
$content = $row["content"];
$short = substr(strip_tags($content), 0, 80);
if (($counter % 2) === 1) {
$blogList .= '<div class="clearer">';
}
//----------------
if($readmore == ''){
$blogList .= '<div class="col span_6_of_12 postBox textLeft fulltwelvepadfix">
<div class="section miniPad">
<div class="col span_8_of_12">'.$article_title.'</div>
<div class="col span_4_of_12"><img src="images/'.$id.'.jpg" height="80px" width="100px" alt="'.$category.' " /></div>
</div>
<div class="section miniPad">
<div class="col span_12_of_12">'.$date_added.' - '.$author.'</div>
<div class="col span_12_of_12"><p>'.$short.'[...]</p></div>
<div class="section group">Read More</div>
</div>
<div class="comments section group">
<div class="commentsInner">Comments(0)</div>
</div>
</div>';
}
else {
$blogList .= '<div class="col span_6_of_12 postBox textLeft fulltwelvepadfix">
<div class="section miniPad">
<div class="col span_8_of_12">'.$article_title.'</div>
<div class="col span_4_of_12"><img src="images/'.$id.'.jpg" height="80px" width="100px" alt="'.$category.' " /></div>
</div>
<div class="section miniPad">
<div class="col span_12_of_12">'.$date_added.' - '.$author.'</div>
<div class="col span_12_of_12"><p>'.$short.'[...]</p></div>
<div class="section group">Read More</div>
</div>
<div class="comments section group">
<div class="commentsInner">Comments(0)</div>
</div>
</div>';
}
//----------------
if (($counter % 2) === 0) { $blogList .= '</div>'; }
$counter++;
}
}
?>
Try to change
SELECT * FROM articles WHERE id ORDER BY id DESC LIMIT 5
to
SELECT * FROM articles WHERE `id`= '$id' ORDER BY id DESC LIMIT 5
Related
How do i disable and enable an anchor tag based on order_status condition? I want to make my receipt button only be able to click when the order_status been updated to ($irow['order_status'] == 5. It would be much appreciated if you all can provide me a code demo to show me how should I implement this using jquery. Thanks!
<div class="container mt-3 mb-5">
<div class="row form-group">
<?php
$total_price = 0.00;
$total_quantity = 0;
$sql = "SELECT *, sum(purchase_price) purchase_price, sum(quantity) quantity FROM ordered_items WHERE user_id = '$user_id' GROUP BY order_id ORDER BY order_datetime DESC";
$query = $conn->query($sql);
if (!mysqli_num_rows($query)) {
echo '
<div class="col-12 text-center">
<div class="alert alert-danger">
<strong><span class="iconify" data-icon="ic:round-remove-shopping-cart" data-width="30px" data-height="30px" data-inline="false"></span> You have no order</strong>
</div>
</div>
';
} else {
while ($row = $query->fetch_assoc()) {
$total_price = $row['purchase_price'];
if($total_price < 500.00 && $row['item_deliver_method'] == 'Delivery'){
$grand_total = $total_price + 10.00;
}else{
$grand_total = $total_price;
}
$total_quantity = $row['quantity'];
//$grand_total = $row['grand_total'];
?>
<div class="col-12 form-group orderidfocus">
<div class="product-wrapper" id="<?php echo $row['order_id']; ?>">
<p class="pl-2 pt-2 pr-2">OrderID: <?php echo $row['order_id']; ?>
</br><?php echo $row['item_deliver_method']; ?> Date: <?php echo $row['delivery_date']; ?></p>
<hr style="border: 0.5px dashed #DBDBDB;">
<?php
$isql = "SELECT * FROM ordered_items LEFT JOIN products ON ordered_items.product_id = products.id WHERE ordered_items.order_id = '".$row['order_id']."' ";
$iquery = $conn->query($isql);
while ($irow = $iquery->fetch_assoc()) {
if($irow['order_status'] == 1) {
$order_status = '<div class="badge-secondary font-italic p-1">Waiting for Response </div>';
}
if($irow['order_status'] == 2) {
$order_status = '<div class="badge-warning font-italic p-1">Preparing</div>';
}
if($irow['order_status'] == 3) {
$order_status = '<div class="badge-info font-italic p-1">In Delivery</div>';
}
if($irow['order_status'] == 4) {
$order_status = '<div class="badge-danger font-italic p-1">Ready to Pick up</div>';
}
if($irow['order_status'] == 5) {
$order_status = '<div class="badge-success font-italic p-1">Completed</div>';
}
?>
<div class="row h-100 pl-2 pr-2 form-group">
<div class="col-12">
<small class="float-right"><?php echo $order_status; ?></small>
</div>
<div class="col-3 my-auto form-group">
<img class="order-page-img-thumbnail" src="images/product-main/<?php echo $irow['product_photo']; ?>" alt="">
</div>
<div class="col-5 pl-0 pr-0 my-auto form-group">
<div class="product-title"><?php echo $irow['product_title']; ?></div>
</div>
<div class="col-4 pl-0 my-auto form-group">
<div class="product-price" style="text-align: right;">
RM<?php echo $irow['product_price']; ?>/<?php echo $irow['product_quantity']; ?><br>
<span style="color: #00644C;">X<?php echo $irow['quantity']; ?></span>
</div>
</div>
</div>
<?php } ?>
<hr style="border: 0.5px dashed #DBDBDB;">
<div class="row justify-content-end pl-2 pr-1">
<div class="col-3 my-auto form-group pr-0">
<p class="cat-title" style="text-align: left;font-size: 13px;">Qty: <?php echo $total_quantity; ?>KG</p>
</div>
<div class="col-5 my-auto form-group pl-0">
<p class="cat-title" style="text-align: right;font-size: 13px;">Amount: RM<?php echo number_format($grand_total,2); ?></p>
</div>
<div class="col-4 form-group pull-right text-center pl-0">
<a id="receiptbtn" target="_blank" href="receipt.php?order_id=<?php echo $row['order_id']; ?>" class="btn addtocart" style="font-size: 12px;"><span class="iconify" data-icon="bx:bx-download" data-inline="false"></span> Receipt</a>
</div>
</div>
</div>
</div>
<?php
}
}
?>
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 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 want to add 3 divs in a row through a loop in PHP.
For example:
<div class="container">
<div class="row">
<div class="col-md-4"> Item 1 </div>
<div class="col-md-4"> Item 2 </div>
<div class="col-md-4"> Item 3 </div>
</div>
<div class="row">
<div class="col-md-4"> Item 4 </div>
<div class="col-md-4"> Item 5 </div>
<div class="col-md-4"> Item 6 </div>
</div>
</div>
I did a few searches on Stackoverflow but I didn't find a good solution. Who can help me? This is my PHP code so far, but as you see, it place only 1 item in a row
for($i = 0; $i < $limit; $i++) {
if ($i % 3 == 0) {
$content .= "<div class='row'>";
}
$content.= "<div class='col-md-4 col-sm-6'>Item ". $i ."</div></div>";
if ($i % 3 == 0) {
$content .= "</div>";
}
}
You can use nested foreach loops instead if you put your numbers into a multidimensional array.
foreach (array_chunk(range(1, $limit), 3) as $row) {
$content .= "<div class='row'>";
foreach ($row as $i) {
$content .= "<div class='col-md-4 col-sm-6'>Item ". $i ."</div>";
}
$content .= "</div>";
}
array_chunk(range(1, $limit), 3) will produce an array like [[1, 2, 3], [4, 5, 6]]. This will use slightly more memory, but I prefer the simpler code.
This will do it and cope with odd numbers in $limit
$limit = 9;
$content = '';
for($i = 0; $i < $limit; $i++) {
if ($i % 3 == 0) {
$content .= "\n<div class='row'>";
}
// remove the extra `</div>` from this line
//$content.= "<div class='col-md-4 col-sm-6'>Item ". $i ."</div></div>";
$content.= "\n\t<div class='col-md-4 col-sm-6'>Item$i</div>";
if ($i % 3 == 2) {
$content .= "\n</div>";
}
}
//cope with odd numbers in $limit
if ( $i%3 != 0) {
$content .= "\n</div>";
}
echo $content;
Result:
<div class='row'>
<div class='col-md-4 col-sm-6'>Item0</div>
<div class='col-md-4 col-sm-6'>Item1</div>
<div class='col-md-4 col-sm-6'>Item2</div>
</div>
<div class='row'>
<div class='col-md-4 col-sm-6'>Item3</div>
<div class='col-md-4 col-sm-6'>Item4</div>
<div class='col-md-4 col-sm-6'>Item5</div>
</div>
<div class='row'>
<div class='col-md-4 col-sm-6'>Item6</div>
<div class='col-md-4 col-sm-6'>Item7</div>
<div class='col-md-4 col-sm-6'>Item8</div>
</div>
Or with $limit set to an odd number like 5
<div class='row'>
<div class='col-md-4 col-sm-6'>Item 1</div>
<div class='col-md-4 col-sm-6'>Item 2</div>
<div class='col-md-4 col-sm-6'>Item 3</div>
</div>
<div class='row'>
<div class='col-md-4 col-sm-6'>Item 4</div>
<div class='col-md-4 col-sm-6'>Item 5</div>
</div>
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';
}
?>