When I try to view multiple data from database my static images are also showing multiple time. I try to use foreach and while with i++ but both time I get the same result.
How to view one image but multiple data.
<div class="row">
<?php
// set array
$array = array();
while($row=mysqli_fetch_assoc($result1))
{
// add each row returned into an array
$array[] = $row;
}
// debug:
foreach ($array as $arrays)
{
?>
<div class="col-sm-3">
<div class="address_item">
<img src="img/soul-icon/address-1.png" alt="">
<h3>Address</h3>
<h4><?php echo $arrays['address']; ?></h4>
</div>
</div>
<div class="col-sm-3">
<div class="address_item">
<img src="img/soul-icon/call.png" alt="">
<h3>Mobile </h3>
<h4><?php echo $arrays['mobile_no']; ?></h4>
</div>
</div>
<div class="col-sm-3">
<div class="address_item">
<img src="img/soul-icon/address-2.png" alt="">
<h3>Phone</h3>
<h4><?php echo $arrays['phone_no'];?> </h4>
</div>
</div>
<div class="col-sm-3">
<div class="address_item">
<img src="img/soul-icon/address-3.png" alt="">
<h3>Email</h3>
<h4><?php echo $arrays['email']; ?></h4>
</div>
</div>
</div>
<?php } ?>
</div>
It's normal because your images are in the foreach.
Remove your first foreach
Make function like
foreach($array as $value){
echo $value;
}
and put them below your images.
example with your code : (but better make functions for all the foreach)
<div class="row">
<?php
// set array
$array = array();
while($row=mysqli_fetch_assoc($result1))
{
// add each row returned into an array
$array[] = $row;
}
// debug:
?>
<div class="col-sm-3">
<div class="address_item">
<img src="img/soul-icon/address-1.png" alt="">
<h3>Address</h3>
<h4><?php foreach ($array as $arrays)
{
echo $arrays['mobile_no'];
}?></h4>
</div>
</div>
<div class="col-sm-3">
<div class="address_item">
<img src="img/soul-icon/call.png" alt="">
<h3>Mobile </h3>
<h4>//same here</h4>
</div>
</div>
<div class="col-sm-3">
<div class="address_item">
<img src="img/soul-icon/address-2.png" alt="">
<h3>Phone</h3>
<h4>//same here </h4>
</div>
</div>
<div class="col-sm-3">
<div class="address_item">
<img src="img/soul-icon/address-3.png" alt="">
<h3>Email</h3>
<h4>//same here</h4>
</div>
</div>
</div>
</div>
Define the Image field outside the loop:
<div class="row">
<?php
// set array
$array = array();
while($row=mysqli_fetch_assoc($result1))
{
// add each row returned into an array
$array[] = $row;
}
// debug:
<div class="col-sm-3">
<div class="address_item">
<img src="img/soul-icon/address-1.png" alt="">
<h3>Address</h3>
</div>
</div>
<div class="col-sm-3">
<div class="address_item">
<img src="img/soul-icon/call.png" alt="">
<h3>Mobile </h3>
</div>
</div>
<div class="col-sm-3">
<div class="address_item">
<img src="img/soul-icon/address-2.png" alt="">
<h3>Phone</h3>
</div>
</div>
<div class="col-sm-3">
<div class="address_item">
<img src="img/soul-icon/address-3.png" alt="">
<h3>Email</h3>
</div>
</div>
foreach ($array as $arrays)
{
?>
<div class="col-sm-3">
<div class="address_item text-center">
<h4><?php echo $arrays['address']; ?></h4>
</div>
</div>
<div class="col-sm-3">
<div class="address_item text-center">
<h4><?php echo $arrays['mobile_no']; ?></h4>
</div>
</div>
<div class="col-sm-3">
<div class="address_item text-center">
<h4><?php echo $arrays['phone_no'];?> </h4>
</div>
</div>
<div class="col-sm-3">
<div class="address_item text-center">
<h4><?php echo $arrays['email']; ?></h4>
</div>
</div>
</div>
<?php } ?>
</div>
Simplest put not the most beatiful solution is to just check if its your "header" line and adding those images at first run.. you can also add the if around your images instead of dublication to much. but you will get the idea
<div class="row">
<?php
// set array
$array = array();
while($row=mysqli_fetch_assoc($result1))
{
// add each row returned into an array
$array[] = $row;
}
// debug:
$i = 0;
foreach ($array as $arrays)
{
?>
if($i == 0){
//first line
<div class="col-sm-3">
<div class="address_item">
<img src="img/soul-icon/address-1.png" alt="">
<h3>Address</h3>
<h4><?php echo $arrays['address']; ?></h4>
</div>
</div>
<div class="col-sm-3">
<div class="address_item">
<img src="img/soul-icon/call.png" alt="">
<h3>Mobile </h3>
<h4><?php echo $arrays['mobile_no']; ?></h4>
</div>
</div>
<div class="col-sm-3">
<div class="address_item">
<img src="img/soul-icon/address-2.png" alt="">
<h3>Phone</h3>
<h4><?php echo $arrays['phone_no'];?> </h4>
</div>
</div>
<div class="col-sm-3">
<div class="address_item">
<img src="img/soul-icon/address-3.png" alt="">
<h3>Email</h3>
<h4><?php echo $arrays['email']; ?></h4>
</div>
</div>
}else{
<div class="col-sm-3">
<div class="address_item">
<h3>Address</h3>
<h4><?php echo $arrays['address']; ?></h4>
</div>
</div>
<div class="col-sm-3">
<div class="address_item">
<h3>Mobile </h3>
<h4><?php echo $arrays['mobile_no']; ?></h4>
</div>
</div>
<div class="col-sm-3">
<div class="address_item">
<h3>Phone</h3>
<h4><?php echo $arrays['phone_no'];?> </h4>
</div>
</div>
<div class="col-sm-3">
<div class="address_item">
<h3>Email</h3>
<h4><?php echo $arrays['email']; ?></h4>
</div>
</div>
</div>
}
<?php } ?>
</div>
Related
I have a little problem that might be syntax (since I'm not that good at php). Basically, I'm de-wrapping a code where there will be records where there will be a date (in this case, a foundation date, for example; 20-08-2027). So I created an "if". If there are records with the date, for example, 2027, the records appear. If there is not, then an error message will be displayed saying that there is still no record made with that date. I've tried a few things, but nothing worked. At this point, an error appears. Below will be the error. Please try to help me. It's no use saying more technical things, because I'm not that good at php. Thank you.
Error: "Parse error: syntax error, unexpected 'else' (T_ELSE) in /home/host/public_html/template/year/2027.php on line 300"
2027.php
<section class="content products">
<div class="container">
<h1 class="hidden">Eventos</h1>
<div class="row">
<div class="col-sm-10">
<div class="row grid" id="products">
<div class="releated-products">
<div class="row grid" id="products">
<?php
$sqli=sprintf("SELECT * FROM eventos WHERE YEAR(data) = 2027");
$resu=mysqli_query($con,$sqli);
mysqli_set_charset($con, 'UTF8');
if (mysqli_num_rows($resu)>0) {
while($regi=mysqli_fetch_array($resu)){
$sqli_consulta=sprintf("select * from eventos where id=%d;",$regi['id']);
$resu_consulta=mysqli_query($con,$sqli_consulta);
$regi_consulta=mysqli_fetch_array($resu_consulta);
$linkk='../eventoindividual.php?id='.$regi_consulta['id'];
?>
<div class="col-sm-3 col-xs-6">
<article class="product-item">
<div class="row">
<div class="col-sm-3">
<div class="product-overlay">
<div class="product-mask"></div>
<img src="<?php echo '../admin/documentos/'.$regi['nome_doc']; ?>" style="width:100%">
</div>
</div>
<div class="col-sm-9">
<div class="product-body">
<h3><?php echo $regi['nome']; ?></h3>
<br>
<span class="price">
<ins><span class="amount"><?php echo $regi['preco']; ?></span></ins>
</span>
<div class="buttons buttons-simple">
<i class="fa fa-shopping-cart"></i>Comprar bilhetes
</div>
</div>
</div>
</div>
</article>
</div>
<?php
} else{
?>
<p>Nada!</p>
<?php
}
}
?>
</div>
</div>
</div>
</div>
</div>
</div>
</section>
The reason you are getting the error because you have while loop in your if block that you are not closing.
Check the code below for this fixed version.
<?php
$sqli=sprintf("SELECT * FROM eventos WHERE YEAR(data) = 2027");
$resu=mysqli_query($con,$sqli);
mysqli_set_charset($con, 'UTF8');
if (mysqli_num_rows($resu)>0):
while($regi=mysqli_fetch_array($resu)):
$sqli_consulta=sprintf("select * from eventos where id=%d;",$regi['id']);
$resu_consulta=mysqli_query($con,$sqli_consulta);
$regi_consulta=mysqli_fetch_array($resu_consulta);
$linkk='../eventoindividual.php?id='.$regi_consulta['id'];
?>
<div class="col-sm-3 col-xs-6">
<article class="product-item">
<div class="row">
<div class="col-sm-3">
<div class="product-overlay">
<div class="product-mask"></div>
<img src="<?php echo '../admin/documentos/'.$regi['nome_doc']; ?>" style="width:100%">
</div>
</div>
<div class="col-sm-9">
<div class="product-body">
<h3><?php echo $regi['nome']; ?></h3>
<br>
<span class="price">
<ins><span class="amount"><?php echo $regi['preco']; ?></span></ins>
</span>
<div class="buttons buttons-simple">
<i class="fa fa-shopping-cart"></i>Comprar bilhetes
</div>
</div>
</div>
</div>
</article>
</div>
<?php endwhile; else: ?>
<p>Nada!</p>
<?php
endif;
?>
Move the last } bracket above }else{
<section class="content products">
<div class="container">
<h1 class="hidden">Eventos</h1>
<div class="row">
<div class="col-sm-10">
<div class="row grid" id="products">
<div class="releated-products">
<div class="row grid" id="products">
<?php
$sqli=sprintf("SELECT * FROM eventos WHERE YEAR(data) = 2027");
$resu=mysqli_query($con,$sqli);
mysqli_set_charset($con, 'UTF8');
if (mysqli_num_rows($resu)>0) {
while($regi=mysqli_fetch_array($resu)){
$sqli_consulta=sprintf("select * from eventos where id=%d;",$regi['id']);
$resu_consulta=mysqli_query($con,$sqli_consulta);
$regi_consulta=mysqli_fetch_array($resu_consulta);
$linkk='../eventoindividual.php?id='.$regi_consulta['id'];
?>
<div class="col-sm-3 col-xs-6">
<article class="product-item">
<div class="row">
<div class="col-sm-3">
<div class="product-overlay">
<div class="product-mask"></div>
<img src="<?php echo '../admin/documentos/'.$regi['nome_doc']; ?>" style="width:100%">
</div>
</div>
<div class="col-sm-9">
<div class="product-body">
<h3><?php echo $regi['nome']; ?></h3>
<br>
<span class="price">
<ins><span class="amount"><?php echo $regi['preco']; ?></span></ins>
</span>
<div class="buttons buttons-simple">
<i class="fa fa-shopping-cart"></i>Comprar bilhetes
</div>
</div>
</div>
</div>
</article>
</div>
<?php
} // while()
} else{
?>
<p>Nada!</p>
<?php
}
// } //wrong bracket
?>
</div>
</div>
</div>
</div>
</div>
</div>
</section>
For your own sanity it is a good idea leave a comment after closing bracket of a long code, so you know which block it belongs to.
Compiler can't be wrong, if it says there's syntax error then there is. The bracket before the else is closing the while loop - hence else cannot be used there, close the if condition. Here's the corrected code:
<section class="content products">
<div class="container">
<h1 class="hidden">Eventos</h1>
<div class="row">
<div class="col-sm-10">
<div class="row grid" id="products">
<div class="releated-products">
<div class="row grid" id="products">
<?php
$sqli=sprintf("SELECT * FROM eventos WHERE YEAR(data) = 2027");
$resu=mysqli_query($con,$sqli);
mysqli_set_charset($con, 'UTF8');
if (mysqli_num_rows($resu)>0) {
while($regi=mysqli_fetch_array($resu)) {
$sqli_consulta=sprintf("select * from eventos where id=%d;",$regi['id']);
$resu_consulta=mysqli_query($con,$sqli_consulta);
$regi_consulta=mysqli_fetch_array($resu_consulta);
$linkk='../eventoindividual.php?id='.$regi_consulta['id'];
/* 2 brackets { */
?>
<div class="col-sm-3 col-xs-6">
<article class="product-item">
<div class="row">
<div class="col-sm-3">
<div class="product-overlay">
<div class="product-mask"></div>
<img src="<?php echo '../admin/documentos/'.$regi['nome_doc']; ?>" style="width:100%">
</div>
</div>
<div class="col-sm-9">
<div class="product-body">
<h3><?php echo $regi['nome']; ?></h3>
<br>
<span class="price">
<ins><span class="amount"><?php echo $regi['preco']; ?></span></ins>
</span>
<div class="buttons buttons-simple">
<i class="fa fa-shopping-cart"></i>Comprar bilhetes
</div>
</div>
</div>
</div>
</article>
</div>
<?php
} // while loop
} // if condition
else {
?>
<p>Nada!</p>
<?php
}
?>
</div>
</div>
</div>
</div>
</div>
</div>
</section>
hope this helps. 👍
So I'm converting bootstrap site to Wordpress and post cards don't want to stack horizontally... I know there should be foreach loop which will loop through posts but I don't know how to set it up...
<?php while(have_posts()) {
the_post();
?>
<section id="blog" class="py-3">
<div class="container">
<div class="row">
<div class="col-md-4">
<div class="card-columns">
<div class="card">
<img src="<?php the_post_thumbnail_url('medium')?>" alt="" class="img-fluid card-img-top">
<div class="card-body">
<h4 class="card-title">
<?php the_title(); ?>
</h4>
<small class="text-muted">Written by <?php the_author_posts_link(); ?></small>
<hr>
<p class="card-text"><?php the_excerpt(); ?></p>
</div>
</div>
</div>
</div>
</div>
</section>
<?php } ?>
while is also a loop. Inside the loop, you can just put the repeated code. As I've understood your code than you need to something like this:
<section id="blog" class="py-3">
<div class="container">
<div class="row">
<?php while( have_posts() ) {
the_post();
?>
<div class="col-md-4">
<div class="card-columns">
<div class="card">
<img src="<?php the_post_thumbnail_url('medium')?>" alt="" class="img-fluid card-img-top">
<div class="card-body">
<h4 class="card-title">
<?php the_title(); ?>
</h4>
<small class="text-muted">Written by <?php the_author_posts_link(); ?></small>
<hr>
<p class="card-text"><?php the_excerpt(); ?></p>
</div>
</div>
</div>
<?php } ?>
</div>
</div>
</section>
Also, better to move code for one block to another file for example partials/post.php:
<div class="col-md-4">
<div class="card-columns">
<div class="card">
<img src="<?php the_post_thumbnail_url('medium')?>" alt="" class="img-fluid card-img-top">
<div class="card-body">
<h4 class="card-title">
<?php the_title(); ?>
</h4>
<small class="text-muted">Written by <?php the_author_posts_link(); ?></small>
<hr>
<p class="card-text"><?php the_excerpt(); ?></p>
</div>
</div>
</div>
</div>
And update your current file:
<section id="blog" class="py-3">
<div class="container">
<div class="row">
<?php while( have_posts() ) {
the_post();
get_template_part( 'partials/post' );
} ?>
</div>
</div>
</section>
<div id="products" class="row list-group">
<?php $counter = 1; foreach ($photo_products as $value) {
$product = new WC_Product( $value->ID ); ?>
<div class="item col-xs-4 col-lg-4">
<div class="thumbnail">
<img class="myImg" class="group list-group-image" src="<?php echo get_the_post_thumbnail_url($value->ID, array(324, 324)); ?>" alt="" />
<div class="caption">
<h4 class="group inner list-group-item-heading">
<?php echo esc_attr($product->get_title()); ?></h4>
<div class="row">
<div class="col-xs-12 col-md-6">
<p class="lead">
<?php echo wc_price($product->get_price()); ?></p>
</div>
<div class="col-xs-12 col-md-6">
<a class="btn btn-success " href="<?php echo esc_attr($current_url).'/?add-to-cart='.esc_attr($value->ID); ?>">
<?php esc_html_e('Add to cart', 'woocommerce-photography-plugin'); ?>
</a>
</div>
</div>
</div>
<div id="myModal" class="modal">
<span class="close">×</span>
<img id="img01" class="modal-content" src="">
<div id="caption" class="modal-content">
<h4 class="group inner list-group-item-heading">
<?php echo esc_attr($product->get_title()); ?></h4>
<div class="row">
<div class="col-xs-12 col-md-6">
<p class="lead" >
<?php echo wc_price($product->get_price()); ?></p>
</div>
<div class="col-xs-12 col-md-6">
<a class="btn btn-success " href="<?php echo esc_attr($current_url).'/?add-to-cart='.esc_attr($value->ID); ?>">
<?php esc_html_e('Add to cart', 'woocommerce-photography-plugin'); ?>
</a>
</div>
</div>
</div>
</div>
</div>
</div>
<?php $counter++; } ?>
</div>
Instead of
$product = new WC_Product( $value->ID );
You can try, following code:
$product = WC_Product::find()->where('id'=>$value->ID);
Following is my code I am displaying details as 3 columns per row using Bootstrap class row.
I tried like changing div and some condition
<div class="container">
<?php
if($lclResult->rowCount() > 0){
while($row = $lclResult->fetch(PDO::FETCH_ASSOC)) {
# code...
$lclUserName = $row['us_business_name'];
$lclImage1 = $row['us_image1'];
$lclCategory = $row['us_category'];
$lclArea = $row['us_area'];
?>
<div class="row">
<div class="col-sm-4">
<div class="card">
<img class="card-img-top " src="<?php echo $lclImage1 ?>" alt="Card image" style="width:100%; height: 158px;">
<div class="card-body">
<h4 class="card-title"><?php echo $lclUserName?></h4>
<p class="card-text" style="font-size: 25px;"><?php echo $lclCategory?></p>
<hr>
<i class="fa fa-map-marker" style="font-size: 23px;"><span> </span><?php echo $lclArea?></i>
<!-- See Profile -->
</div>
</div>
<?php
}
?>
</div>
<?php
} else {
?>
<div class="container">
<div class="row">
<div class="col-sm-12">
<h1> NO RESULT FOUND...</h1>
</div>
</div>
</div>
<?php
}
?>
</div>
</div>
I want to display 3 columns per row to display data. If anyone knows Please guide me with the above code.
Change your code like this
<div class="container">
<div class="row">
<?php
if($lclResult->rowCount() > 0){
while($row = $lclResult->fetch(PDO::FETCH_ASSOC)) {
# code...
$lclUserName = $row['us_business_name'];
$lclImage1 = $row['us_image1'];
$lclCategory = $row['us_category'];
$lclArea = $row['us_area'];
?>
<div class="col-sm-4">
<div class="card">
<img class="card-img-top " src="<?php echo $lclImage1 ?>" alt="Card image" style="width:100%; height: 158px;">
<div class="card-body">
<h4 class="card-title">
<?php echo $lclUserName?>
</h4>
<p class="card-text" style="font-size: 25px;">
<?php echo $lclCategory?>
</p>
<hr>
<i class="fa fa-map-marker" style="font-size: 23px;">
<span>
</span>
<?php echo $lclArea?>
</i>
<!-- See Profile -->
</div>
</div>
</div>
<?php
}
?>
<?php
} else {
?>
<div class="container">
<div class="row">
<div class="col-sm-12">
<h1> NO RESULT FOUND...
</h1>
</div>
</div>
</div>
<?php
}
?>
</div>
</div>
I have a loop which I want to take this behaver
first to add item div then loop into this div to add 6 items And after complete 6 items it will close this div and start again with next six items until finish
my current code like this :
<?php $x = 0; ?>
<?php foreach ($files as $lst) { ?>
<div class="item <?= ($x == 1 ? 'active' : ''); ?>">
<div class="row">
<div class="col-xs-2">
<a href="<?= base_url() ?>global/uploads/<?= $lst['FileName'] ?>"
rel="prettyPhoto[gallery1]">
<?php if ($lst['FileName'] != "" && file_exists(PUBPATH . "global/uploads/" . $lst['FileName'])) { ?>
<img src="<?= thumb($lst['FileName'], 150, 150); ?>" class="img-responsive">
<?php } else { ?>
<img src="<?= base_url() ?>global/site/data/1.jpg" class="img-responsive">
<?php } ?>
</a>
</div>
</div>
</div>
<?php $x++;
} ?>
but i want final structure to be like this
<div class="item active">
<div class="row">
<div class="col-xs-2">
<img src="data/1.jpg" class="img-responsive">
</div>
<div class="col-xs-2">
<img src="data/1.jpg" class="img-responsive">
</div>
<div class="col-xs-2">
<img src="data/1.jpg" class="img-responsive">
</div>
<div class="col-xs-2">
<img src="data/1.jpg" class="img-responsive">
</div>
<div class="col-xs-2">
<img src="data/1.jpg" class="img-responsive">
</div>
<div class="col-xs-2">
<img src="data/1.jpg" class="img-responsive">
</div>
</div>
</div>
<div class="item">
<div class="row">
<div class="col-xs-2">
<img src="data/1.jpg" class="img-responsive">
</div>
<div class="col-xs-2">
<img src="data/1.jpg" class="img-responsive">
</div>
<div class="col-xs-2">
<img src="data/1.jpg" class="img-responsive">
</div>
<div class="col-xs-2">
<img src="data/1.jpg" class="img-responsive">
</div>
<div class="col-xs-2">
<img src="data/1.jpg" class="img-responsive">
</div>
<div class="col-xs-2">
<img src="data/1.jpg" class="img-responsive">
</div>
</div>
</div>
You should clean the code a bit, instead of filtering inside the loop the array should contain the files and urls you want to enlist.
That would separate HTML from your PHP code making it much easier to read.
$files = [
'data/1.jpg', // can make an array as well, ['src'=>'data/1.jpg', 'href => 'link.com']
'data/2.jpg',
'data/3.jpg',
'data/4.jpg',
'data/5.jpg',
'data/6.jpg',
'data/7.jpg',
];
foreach(array_chunk($files, 6) as $row){
echo '<div class="item"><div class="row">';
foreach($row as $col){
echo '<div class="col-xs-2">';
echo '<img src="'.$col.'" class="img-responsive">';
//echo '<img src="'.$col['src'].'" class="img-responsive">';
echo '</div>';
}
echo '</div></div>';
}