How to set only first item active on while loop in php - php

i using slider fetch MYSQL records i using while function i need only active class in first item only. i need only active class in loop first element.how to solve this problem. i using this code but can't change the item.
mycode
<?php
$qry = MYSQL_QUERY("SELECT * FROM `post` WHERE `post_type`='events' AND `post_event_start_date` <= CURDATE() ORDER BY `post_event_start_date`");
?>
<?php while( $row = MYSQL_FETCH_OBJECT($qry) ){ ?>
<div class="item <?php if($i==0) { ?> active<?php } ?>">
<ul class="thumbnails">
<li class="col-xs-12 col-lg-12 col-md-12 col-sm-12">
<div class="fff">
<div class="thumbnail">
<img src="administrator/slide_name/large/slide_1.jpg" alt="">
</div>
<div class="col-lg-12 well">
<div class="form-group">
<h3><?php print_r($row->post_title); ?></h3>
</div>
<div class="form-group">
<h4><?php print_r($row->post_url); ?></h4>
<h5></h5>
</div>
<div class="form-group">
<div class="row">
<div class="col-lg-6 col-md-6 col-sm-12 col-xs-12">
<center><p>Start Date & Time</p><?php print_r($row->post_event_start_date); ?></center>
</div>
<div class="col-lg-6 col-md-6 col-sm-12 col-xs-12">
<center><p>Start End & Time</p><?php print_r($row->post_event_end_date); ?></center>
</div>
</div>
</div>
<div class="form-group">
<p>Location:</p>
<p><?php print_r($row->post_maplocation); ?></p>
</div>
<div class="form-group">
<p>Description: </p>
<p><?php print_r($row->post_desc); ?></p>
</div>
<div class="row pull-right">
<a class="btn btn-mini" href="#">» Read More</a>
<a class="btn btn-mini" href="#">» Share it</a>
</div>
</div>
</div>
</li>
</ul>
</div>
<?php } ?>

Replace
<?php while( $row = MYSQL_FETCH_OBJECT($qry) ){ ?>
<div class="item <?php if($i==0) { ?> active<?php } ?>">
With
<?php
$i=0;
while( $row = MYSQL_FETCH_OBJECT($qry) ){ ?>
<div class="item <?php if($i==0) {
$i=1;
echo 'active'; } ?>">

Try this.
$qry = MYSQL_QUERY("SELECT * FROM 'post' WHERE 'post_type'='events' AND 'post_event_start_date' <= CURDATE() ORDER BY 'post_event_start_date'");
$first_item = true;?>
<?php while( $row = MYSQL_FETCH_OBJECT($qry) ){ ?>
<div class="item <?php echo $first_item? 'active': ''; ?>"><?php $first_item = false; ?>
<ul class="thumbnails">
<li class="col-xs-12 col-lg-12 col-md-12 col-sm-12">
<div class="fff">
<div class="thumbnail">
<img src="administrator/slide_name/large/slide_1.jpg" alt="">
</div>
<div class="col-lg-12 well">
<div class="form-group">
<h3><?php print_r($row->post_title); ?></h3>
</div>
<div class="form-group">
<h4><?php print_r($row->post_url); ?></h4>
<h5></h5>
</div>
<div class="form-group">
<div class="row">
<div class="col-lg-6 col-md-6 col-sm-12 col-xs-12">
<center><p>Start Date & Time</p><?php print_r($row->post_event_start_date); ?></center>
</div>
<div class="col-lg-6 col-md-6 col-sm-12 col-xs-12">
<center><p>Start End & Time</p><?php print_r($row->post_event_end_date); ?></center>
</div>
</div>
</div>
<div class="form-group">
<p>Location:</p>
<p><?php print_r($row->post_maplocation); ?></p>
</div>
<div class="form-group">
<p>Description: </p>
<p><?php print_r($row->post_desc); ?></p>
</div>
<div class="row pull-right">
<a class="btn btn-mini" href="#">» Read More</a>
<a class="btn btn-mini" href="#">» Share it</a>
</div>
</div>
</div>
</li>
</ul>
</div>
<?php } ?>`

Define $i = 0 and make it $i++
Try below code
<?php
$qry = MYSQL_QUERY("SELECT * FROM `post` WHERE `post_type`='events' AND `post_event_start_date` <= CURDATE() ORDER BY `post_event_start_date`");
?>
<?php
$i = 0;
while( $row = MYSQL_FETCH_OBJECT($qry) ){ ?>
<div class="item <?php if($i==0) { ?> active<?php } ?>">
<ul class="thumbnails">
<li class="col-xs-12 col-lg-12 col-md-12 col-sm-12">
<div class="fff">
<div class="thumbnail">
<img src="administrator/slide_name/large/slide_1.jpg" alt="">
</div>
<div class="col-lg-12 well">
<div class="form-group">
<h3><?php print_r($row->post_title); ?></h3>
</div>
<div class="form-group">
<h4><?php print_r($row->post_url); ?></h4>
<h5></h5>
</div>
<div class="form-group">
<div class="row">
<div class="col-lg-6 col-md-6 col-sm-12 col-xs-12">
<center><p>Start Date & Time</p><?php print_r($row->post_event_start_date); ?></center>
</div>
<div class="col-lg-6 col-md-6 col-sm-12 col-xs-12">
<center><p>Start End & Time</p><?php print_r($row->post_event_end_date); ?></center>
</div>
</div>
</div>
<div class="form-group">
<p>Location:</p>
<p><?php print_r($row->post_maplocation); ?></p>
</div>
<div class="form-group">
<p>Description: </p>
<p><?php print_r($row->post_desc); ?></p>
</div>
<div class="row pull-right">
<a class="btn btn-mini" href="#">» Read More</a>
<a class="btn btn-mini" href="#">» Share it</a>
</div>
</div>
</div>
</li>
</ul>
</div>
<?php
$i++;
} ?>

<?php $i = 0 ;
$qry = MYSQL_QUERY("SELECT * FROM `post` WHERE `post_type`='events' AND `post_event_start_date` <= CURDATE() ORDER BY `post_event_start_date`");
?>
<?php while( $row = MYSQL_FETCH_OBJECT($qry) ){ ?>
<div class="item <?php if($i==0) { ?> active<?php } ?>">
Your other code
php $i++; } ?>

Related

I have an error in my code that does not allow me to show the results and/or show an error message in php

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. 👍

not getting a diffrent price getting a same price for every pic not getting different id on caption

<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);

Dynamic div close in php while loop

code i have written below is working fine but at the end of the looping the div is not closed its still opening a loop
<div class="carousel-inner">
<div class="item active">
<div class="row">
<?php
$recent_projects_sql="SELECT * from recent_projects where service_type='upholstery'";
$recent_projects_conn=mysql_query($recent_projects_sql) or die(mysql_error());
$i=0; $split=0;
while($projects=mysql_fetch_array($recent_projects_conn)) {
$i++;
?>
<div class="col-sm-3">
<div class="col-item" style="">
<div class="photo-shadow"></div>
<div class="photo">
<img src="admin/assets/images/uploads/projects/<?php echo $projects['attachment1']; ?>" alt="User one">
</div>
<div class="info">
<div class="name">
<?php echo $projects['service_name']; ?>
</div>
<div class="degination">
<?php echo $projects['sub_title']; ?>
</div>
<div class="buttons">
<a class="btn btn-theme ripple-effect" href="#">View More</a>
</div>
<div class="clearfix"></div>
</div>
</div>
</div>
<?php
$split++;
if ($split % 4 == 0){
echo '</div></div><div class="item"><div class="row">';
}
}
?>
</div>
</div>
The Div has splited very well but in end of the loop div has not been closed. Thats only the problem please provide me the help to sort out the problem
When I inspect the element the last loop will show at the given result as follows:
<div class="col-sm-3">
<div class="col-item">
<div class="photo-shadow"></div>
<div class="photo">
<img src="admin/assets/images/uploads/projects/1557301934.jpg" alt="User one">
</div>
<div class="info">
<div class="name">UPHOLSTERY</div>
<div class="degination">UPHOLSTERY</div>
<div class="buttons">
<a class="btn btn-theme ripple-effect" href="#">View More</a>
</div>
<div class="clearfix"></div>
</div>
</div>
</div>
</div></div><div class="item"><div class="row">
I want to remove the two opening div's as dynamically. How can i set this to remove opened div's at then end of the looping
I just took a quick look and it looks like you are not closing the "carousel-inner" div
<div class="carousel-inner">
<div class="item active">
<div class="row">
<?php
$recent_projects_sql = "SELECT * from recent_projects where service_type='upholstery'";
$recent_projects_conn = mysql_query( $recent_projects_sql ) or die( mysql_error() );
$i = 0;
$split = 0;
while ( $projects = mysql_fetch_array( $recent_projects_conn ) ) {
$i ++;
?>
<div class="col-sm-3">
<div class="col-item" style="">
<div class="photo-shadow"></div>
<div class="photo">
<img src="admin/assets/images/uploads/projects/<?php echo $projects['attachment1']; ?>"
alt="User one">
</div>
<div class="info">
<div class="name">
<?php echo $projects['service_name']; ?>
</div>
<div class="degination">
<?php echo $projects['sub_title']; ?>
</div>
<div class="buttons">
<a class="btn btn-theme ripple-effect" href="#">View More</a>
</div>
<div class="clearfix"></div>
</div>
</div>
</div>
<?php $split ++;
if ( $split % 4 == 0 ) {
echo '</div></div><div class="item"><div class="row">';
}
}
?>
</div>
</div>
Add a Boolean check for the execution of loop, such as $check = true;, add this within the loop.
after the loop add this
if($check){
echo " </div></div>";
}
That's because at the end of iteration (in case of mod 4 and even without it), you keep 2 divs opened
echo '</div></div><div class="item"><div class="row">';

How to Active Loop Tab PHP

I don't know what to do loop and active tab on php.Active only one tab at a time.
My tab
<div class="container content">
<div class="row tab-v3">
<div class="col-sm-3">
<?php do { ?>
<ul class="nav nav-pills nav-stacked">
<li><i></i><?php echo $row_menulist['mlname']; ?></li>
</ul>
<?php } while ($row_menulist = mysql_fetch_assoc($menulist)); ?>
</div>
<div class="col-sm-9">
<div class="tab-content">
<div class="tab-pane fade in active" id="<?php echo $row_menulist['mlname']; ?>">
<?php do { ?>
<h4><?php echo $row_info['mlname']; ?></h4>
<p><?php echo $row_info['detail']; ?></p>
<?php } while ($row_info = mysql_fetch_assoc($info)); ?>
</div>
</div>
</div>
</div>
</div>
remove active class,i assume that change in loop not affect in design.
<div class="container content">
<div class="row tab-v3">
<div class="col-sm-3">
<?php do { ?>
<ul class="nav nav-pills nav-stacked">
<li><i></i><?php echo $row_menulist['mlname']; ?></li>
</ul>
<?php } while ($row_menulist = mysql_fetch_assoc($menulist)); ?>
</div>
<div class="col-sm-9">
<div class="tab-content">
<?php $count = 1; do { ?>
<div class="tab-pane fade in <?php if($count == '1') { echo 'active'; } ?>" id="<?php echo $row_menulist['mlname']; ?>">
<h4><?php echo $row_info['mlname']; ?></h4>
<p><?php echo $row_info['detail']; ?></p>
</div>
<?php $count++; } while ($row_info = mysql_fetch_assoc($info)); ?>
</div>
</div>
</div>
</div>
It's working!
I even use the same logic into a CMS PHP Based (SPIP).
<div class="row">
<div class="col-xs-12 col-sm-3">
<div class="w-100 srvc-tab">
<ul class="nav nav-tab">
<?php $count = 1; ?>
<BOUCLE_getServiceTitle (ARTICLES){id_rubrique=#ID_RUBRIQUE}{par id_article}{tous}>
<li class="<?php if($count == '1') { echo 'active'; } ?>">
<a href="#service-#COMPTEUR_BOUCLE" data-toggle="tab">
[(#TITRE)]
</a>
</li>
<?php $count++; ?>
</BOUCLE_getServiceTitle>
</ul>
</div>
</div>
<div class="col-xs-12 col-sm-9">
<div class="w-100 tab-content">
<?php $count = 1; ?>
<BOUCLE_getServiceContent (ARTICLES){id_rubrique=#ID_RUBRIQUE}{tous}>
<div id="service-#COMPTEUR_BOUCLE" class="col-xs-12 tab-pane fade in <?php if($count == '1') { echo 'active'; } ?>">
<div class="w-100 mb-15 pb-15">
<h3 class="mb-0 text-capitalize fw-600">
[(#TITRE)]
<span class="d-inline-block stroke"></span>
</h3>
</div>
<div class="w-100">
[(#TEXTE)]
</div>
</div>
<?php $count++; ?>
</BOUCLE_getServiceContent>
</div>
</div>
<div class="clearfix"></div>
</div>

How to add active state of category menu in wordpress?

I create a template for category menu. when click "category", based on category it reload the page shows the post in the category. i need to make the category menu active. How to do?
Category-menu.php
<div class="row text-center" >
<div class="col-md-12 col-xs-12 col-sm-12 col-lg-12">
<?php
$menu_item=wp_get_nav_menu_items( 'Category Menus', $args );
foreach ($menu_item as $menu) {
?>
<div class="col-lg-3 col-sm-12 col-xs-12 col-md-3 menu-padding">
<div class="blog-menu-items cat-menu-color">
<a href="<?php echo $menu->url; ?>">
<?php echo $menu->title; ?>
</a>
</div>
</div>
<?php } ?>
</div>
</div>
Category.php
<?php get_template_part( 'category', 'menus' ); ?>
<?php $cat_title = strtoupper(get_cat_name($category_id)); ?>
<div class="row" style="padding-top:5%;">
<div class="col-md-12 col-xs-12 col-sm-12 col-lg-12">
<a href="<?php echo home_url() ?>" class="back-to-blog">
<i class="icon-chevron-left"></i>
<?php echo __( 'Back to Home', 'tiny_theme') ?>
</a>
</div>
</div>
<div class="row popular-posts">
<div class="col-md-12 col-xs-12 col-sm-12 col-lg-12">
<div class="col-lg-8 col-sm-6 col-xs-6 col-md-8 popular-posts-header pull-left">
<h4>POSTS ON <?php echo $cat_title ?></h4>
</div>
<?php if ($query1->max_num_pages > 1) { ?>
<div class="col-lg-4 col-sm-6 col-xs-6 col-md-4 pull-right" id="popular-post-pager" >
<?php echo paginate_links( $pag_args1 );?>
</div>
<?php } ?>
</div>
<div class="col-md-12 col-xs-12 col-sm-12 col-lg-12">
<?php
if ($query1->have_posts()) {
while ($query1->have_posts()) {
$query1->the_post();
?>
<h4 style="padding-top:5px;">
<?php echo the_title(); ?>
</h4>
<p> <?php the_excerpt(10); ?></p>
<?php }
}
wp_reset_query();
?>
</div>
</div>
You can try to do a comparaison between the current category ID and the object_id which holds the ID of the object the link is in relation with, something like this
(not tested)
<div class="row text-center" >
<div class="col-md-12 col-xs-12 col-sm-12 col-lg-12">
<?php
global $post;
$menu_item=wp_get_nav_menu_items( 'Category Menus', $args );
foreach ($menu_item as $menu) {
$currentClass = '';
if($post->ID == $menu->object_id)
$currentClass = 'current-menu-item';
?>
<div class="col-lg-3 col-sm-12 col-xs-12 col-md-3 menu-padding">
<div class="blog-menu-items cat-menu-color">
<a class="<?php echo $currentClass; ?>" href="<?php echo $menu->url; ?>">
<?php echo $menu->title; ?>
</a>
</div>
</div>
<?php } ?>
</div>
</div>

Categories