i am fetching few results from mysql db where each result has link ,on clicking that link a bootstrap modal should open.
So,everytime modal should shows content related to selected result.
My problem is its only taking 1st variable of loop.
My code is:
<?php
$id = 1;
while($row = mysqli_fetch_array($result))
{
$title = $row['content_title'];
$description = $row['content_description'];
$content_url = $row['content_url'];
$icon_url = $row['thumb_icon_url'];
$id++;
$_SESSION['result'][$id] = array('content_title' => $title, 'content_description' => $description, 'content_url' => $content_url, 'thumb_icon' => $icon_url);
?>
<?php include 'templates/div_data.php'; ?>
<?php
}
}
?>
div_data.php
<div class="col-md-3 photo-grid " style="float:left">
<div class="well well-sm" target="_blank">
<figure>
<img class="img" alt="PDF" src="<?php echo $icon_url; ?>" >
<figcaption id="hide"> <p> <a data-toggle="modal" data-target="#myModal">Preview</a></p></figcaption>
</figure>
<a href="final.php?id=<?php echo $id; ?>&name=<?php echo $title; ?>" target="_blank">
<h4><small><?php echo $title; ?></small></h4>
</a>
<br>
<ul style="list-style: none;" class="container-fluid">
<li class="col-md-4 col-xs-4 pull-left" id="hide">Preview</li>
<li name="id" class="col-md-6 col-xs-6"> View Full Doc</li>
<li class="col-md-2 col-xs-2"><i class="fa fa-eye" aria-hidden="true"> 100</i></li>
</ul>
</div>
</div>
<a data-toggle="modal" class="pull-right" data-target="#myModal"></a>
<div class="modal fade" id="myModal" role="dialog">
<div class="modal-dialog modal-md">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h4 class="modal-title">Preview</h4>
</div>
<div class="modal-body">
<form action="final.php?id=<?php echo $id; ?>&name=<?php echo $title; ?>" class="form-horizontal" role="form" method="post">
<div class="embed-responsive embed-responsive-16by9 col-md-8 col-sm-8 col-xs-8" >
<iframe id="iframe" class="embed-responsive-item item"src="web/viewer.html?file=https://cors-anywhere.herokuapp.com/<?php echo $content_url; ?> &embedded=true#toolbar=0&navpanes=0&scrollbar=0" frameborder="0" ></iframe>
</div>
<h3 class="full col-md-3"><?php echo $title; ?></h3>
<hr> <br>
<p class="col-md-4">Description:</p>
<p class="col-md-4"><?php echo $_SESSION['result'][$id]['content_description']; ?>
</p>
<p class="col-md-2">Shared by: Sarthak tuteja</p>
<p class="col-md-2">Views:200000</p>
<div class="col-md-4">
<button type="submit" id="btnFilter" formtarget="_blank" class="btn btn-primary " >View Full Doc</button>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
Related
I'm interested in how to make the pagination working. I tried everything, but it didn't work.
I tried after while echo paginate_links() and still nothing... I don't understand
This is navigation to filter ALU, PVC or ALL doors
<article id="post-<?php the_ID(); ?>" <?php post_class('container-fluid py-5'); ?>>
<div class="container py-5">
<div class="section-title position-relative text-center">
<h6 class="text-uppercase text-primary mb-3" style="letter-spacing: 3px;">Was wir haben</h6>
<h1 class="font-secondary display-4">Katalog</h1>
</div>
<!-- FILTERS -->
<div class="filters" id="productsDoorsSelect">
<p>Kategorien</p>
<span class="filter btn btn-primary text-uppercase" data-filter="all">ALL</span>
<span class="filter btn btn-primary text-uppercase" data-filter="alu">alu</span>
<span class="filter btn btn-primary text-uppercase" data-filter="pvc">pvc</span>
</div>
<!-- FILTERS -->
</div>
This is query for custom post type
<!-- PRODUCTS DOORS -->
<div class="container py-5 projects">
<div class="row-products" id="doorsProducts" >
<?php
$productsQuery = new WP_Query (array(
'post_type' => 'products_catalog'
));
if($productsQuery->have_posts()):
while($productsQuery->have_posts()):
$productsQuery->the_post();
?>
<div class="col-lg-4-products mb-3 project" data-filter="<?php the_field('doors_category_field') ?>">
<div class="product-item mb-2">
<div class="product-img">
<img class="img-fluid" src="<?php the_post_thumbnail_url() ?>" alt="">
<a type="button" class="btn btn-primary" data-toggle="modal" data-target="#exampleModalLong<?php the_title()?>">
<i class="fa fa-2x fa-plus text-white"></i>
</a>
</div>
<div class="bg-secondary text-center p-4">
<h3 class="m-0"><?php the_title() ?></h3>
</div>
</div>
</div>
<?php endwhile; endif; ?>
</div>
</div>
<!-- PRODUCTS DOORS -->
This is modals (after click on element modal with details)
<!-- Modal -->
<?php
$productsQuery = new WP_Query (array('post_type' => 'products_catalog'));
if($productsQuery->have_posts()):
while($productsQuery->have_posts()):
$productsQuery->the_post();
?>
<div class="modal fade" id="exampleModalLong<?php the_title() ?>" tabindex="-1" role="dialog" aria-labelledby="exampleModalLongTitle"
aria-hidden="true">
<div class="modal-dialog modal-lg" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalLongTitle"><?php the_title() ?></h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<?php the_post_thumbnail('post-thumbnail', ['class' => 'img-fluid mb-4']); ?>
<?php the_content();?>
</div>
</div>
</div>
</div>
<?php endwhile; endif; ?>
<!-- Modal -->
I'm trying to make an accordion for each row in the database using the Foreach loop
I tried this but I'm getting only one accordion out its supposed to make more than 2 accordion deppending on rows count this what i have tried :-
<?foreach ($fetchedDrafts as $draft):?>
<div id="<?=$draft['name'];?>" role="tablist" class="border border-danger rounded" aria-multiselectable="true">
<div class="card">
<div class="card-header" role="tab" style="height:120px !important;" id="section1HeaderId">
<h5 class="mb-0">
<a data-toggle="collapse" data-parent="#accordianId" href="#section1ContentId" aria-expanded="true" aria-controls="section1ContentId">
<center>
<!---->
<button type="button" class="btn btn-warning pill" style="display:none;"><?$draft['name'];?></button>
<h3 class="Title" style="display:none;font-family:hana;"></h3>
</center>
<img class="accordianimg pull-left" style="width:180px; height:120px;" src="..\upload\gsk.png" alt="alt"></img>
</a>
<div class="input-container" style="margin-top:2rem !important; margin-right:6rem;">
<h2>
<button onclick="funvction()" class="btn btn-danger SearchButton"style="font-family:hana;">بحث</button>
<input class="border border-danger rounded SearchInput" type="text" style="width:440px;"id="SearchInput" placeholder="البحث عن المنتج" name="DraftSearchFiled" onchange="DraftSearch()">
</h2>
</div>
</div><br>
<div id="section1ContentId" class="collapse in" role="tabpanel" aria-labelledby="section1HeaderId" style="overflow-x: hidden !important;" onscroll="">
<div class="card-body d-flex flex-wrap">
<div class="row justify-content-center">
<div id="demo" class="carousel slide carousel" data-ride="carousel" style="margin-left:20rem;width: 26rem; height:31rem;">
<!-- no need for indicators -->
<div class="carousel-inner canner">
<?
$i = 0;
foreach($DRTitems as $row){
$actives = '';
if($i == 0){
$actives ='active';
}
?>
<div class="carousel-item <?= $actives;?>">
<div class="container">
<div class="row justify-content-center">
<div class="card" style="width: 26rem; height:auto;">
<? if($row['Sale'] == true){
echo'<div class="corner-ribbon top-left sticky shadow">تخفيض</div>';
}else{
echo'<div class="corner-ribbon top-left-green sticky shadow">افضل سعر</div>';
}
?>
<img class="card-img-top" src="<?= $row['imageurl'] ?>" alt="Drug" style=""></img>
<div class="card-body body">
<h1 class="card-title Productname " style="font-family:Tahoma !important; font-size:42px !important;"><?= $row['name'] ?></h1><br><br><br>
<p class="chemicalcom"><?= $row['chemicalcom']?></p><br>
<p class="price"><?= $row['price'] ?>$</p>
<input type="submit" class="btn btn-success btn-sm btn-block btn" value="أضف للسلة"></input>
</div>
</div>
</div>
</div>
</div>
<? $i++; }?>
</div>
<?php endforeach; ?>
what i have in db is 3 rows but the code makes only one accordion
Here is the situation. I have an accordion, setup and the problem is that multiple accordions are opening and closing the same time.
Here is the code:
<div class="panel-group" id="accordion" role="tablist" aria-multiselectable="true">
<?php $x = 0; ?>
<?php foreach ($testFaqs as $faqs) { ?>
<div class="panel panel-default">
<div class="panel-heading" role="tab" id="heading-<?php echo $x; ?>">
<h4 class="panel-title">
<a role="button" data-toggle="collapse" data-parent="#accordion" href="#collapse-<?php echo $x; ?>" aria-expanded="true" aria-controls="collapse-<?php echo $x; ?>">
<?php echo $faqs['name']; ?>
</a>
</h4>
</div>
<div id="collapse-<?php echo $x; ?>" class="panel-collapse collapse in" role="tabpanel" aria-labelledby="heading-<?php echo $x; ?>">
<div class="panel-body">
<?php if ($faqs['videoUrl'] != '' ) { ?>
<iframe align="center" class="youtube" src="https://www.youtube.com/embed/<?php echo $faqs['videoUrl']?>"> </iframe>
<?php } ?>
<?php echo htmlspecialchars_decode(($faqs['answer'])); ?>
</div>
</div>
</div>
<?php $x++; } ?>
</div>
Any help would be appreciated.
Thank you,
Kevin Davis
You can use latest Bootstrap 4 Accordion. More about it here https://getbootstrap.com/docs/4.3/components/collapse/#accordion-example
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.5.0/css/bootstrap.min.css" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.5.0/js/bootstrap.min.js"></script>
<div class="accordion" id="accordionExample">
<div class="card">
<div class="card-header" id="heading-1">
<h2>
<button class="btn btn-link" type="button" data-toggle="collapse" data-target="#collapse-1" aria-expanded="true" aria-controls="collapse-1">
111
</button>
</h2>
</div>
<div id="collapse-1" class="collapse show" aria-labelledby="heading-1" data-parent="#accordionExample">
<div class="card-body">
111
</div>
</div>
</div>
<div class="card">
<div class="card-header" id="heading-2">
<h2>
<button class="btn btn-link collapsed" type="button" data-toggle="collapse" data-target="#collapse-2" aria-expanded="false" aria-controls="collapse-2">
222
</button>
</h2>
</div>
<div id="collapse-2" class="collapse" aria-labelledby="heading-2" data-parent="#accordionExample">
<div class="card-body">
222
</div>
</div>
</div>
<div class="card">
<div class="card-header" id="heading-3">
<h2>
<button class="btn btn-link collapsed" type="button" data-toggle="collapse" data-target="#collapse-3" aria-expanded="false" aria-controls="collapse-3">
333
</button>
</h2>
</div>
<div id="collapse-3" class="collapse" aria-labelledby="heading-3" data-parent="#accordionExample">
<div class="card-body">
333
</div>
</div>
</div>
</div>
So your code will be
<div class="accordion" id="accordionExample">
<?php $x = 0; ?>
<?php foreach ($testFaqs as $faqs) { ?>
<div class="card">
<div class="card-header" id="heading-<?php echo $x; ?>">
<h2>
<button class="btn btn-link" type="button" data-toggle="collapse" data-target="#collapse-<?php echo $x; ?>" aria-expanded="true" aria-controls="collapse-<?php echo $x; ?>">
<?php echo $faqs['name']; ?>
</button>
</h2>
</div>
<div id="collapse-<?php echo $x; ?>" class="collapse<?php if ($x == 0) { ?> show<?php } ?>" aria-labelledby="heading-<?php echo $x; ?>" data-parent="#accordionExample">
<div class="card-body">
<?php if ($faqs['videoUrl'] != '' ) { ?>
<iframe align="center" class="youtube" src="https://www.youtube.com/embed/<?php echo $faqs['videoUrl']?>"></iframe>
<?php } ?>
<?php echo htmlspecialchars_decode(($faqs['answer'])); ?>
</div>
</div>
</div>
<?php $x++; } ?>
</div>
Added this class="collapse<?php if ($x == 0) { ?> show<?php } ?>" for the first box to be opened.
If you have more then one accordion on one page - don't forget to change id="accordionExample" and data-parent="#accordionExample", maybe by adding them an increment for counting accordions.
<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);
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
In my single-work.php I have some code with slight variations that I feel could be more DRY. When there are only slight variations like this is it ok to repeat part of the code? Or is there another way to write parts of this?
// Advanced Custom Fields
// Grid images or videos
$grid_image_1 = get_field('image_1');
$grid_image_2 = get_field('image_2');
$grid_image_3 = get_field('image_3');
$grid_image_4 = get_field('image_4');
$grid_image_5 = get_field('image_5');
// Video embeds
$image_1_video = get_field('image_1_do_you_wish_to_add_a_video');
$video_embed_code_1 = get_field('video_embed_code_1');
$image_2_video = get_field('image_2_do_you_wish_to_add_a_video');
$video_embed_code_2 = get_field('video_embed_code_2');
$image_3_video = get_field('image_3_do_you_wish_to_add_a_video');
$video_embed_code_3 = get_field('video_embed_code_3');
$image_4_video = get_field('image_4_do_you_wish_to_add_a_video');
$video_embed_code_4 = get_field('video_embed_code_4');
$image_5_video = get_field('image_5_do_you_wish_to_add_a_video');
$video_embed_code_5 = get_field('video_embed_code_5');
// General
$about = get_field('about');
$subtitle = get_field('subtitle');
get_template_part( 'template-parts/content', 'carousel' );
?>
<!-- Page Content -->
<!-- Work -->
<section id="our-work">
<div class="container">
<div class="row">
<div class="col-lg-12 section-title">
<h3><?php echo get_the_title(); ?></h3>
</div><!-- title -->
</div><!-- row -->
<div class="work-images">
<div class="row">
<div class="col-sm-4">
<?php if( $image_1_video == "Yes" ): ?>
<a data-toggle="modal" data-target="#myModal" href="#">
<img src="<?php echo $grid_image_1['url']; ?>" alt="<?php echo $grid_image_1['alt']; ?>">
</a>
<?php else: ?>
<img src="<?php echo $grid_image_1['url']; ?>" alt="<?php echo $grid_image_1['alt']; ?>">
<?php endif; ?>
</div><!-- col -->
<div class="col-sm-4">
<?php if( $image_2_video == "Yes" ): ?>
<a data-toggle="modal" data-target="#myModal2" href="#">
<img src="<?php echo $grid_image_2['url']; ?>" alt="<?php echo $grid_image_2['alt']; ?>">
</a>
<?php else: ?>
<img src="<?php echo $grid_image_2['url']; ?>" alt="<?php echo $grid_image_2['alt']; ?>">
<?php endif; ?>
</div><!-- col -->
<div class="col-sm-4">
<?php if( $image_3_video == "Yes" ): ?>
<a data-toggle="modal" data-target="#myModal2" href="#">
<img src="<?php echo $grid_image_3['url']; ?>" alt="<?php echo $grid_image_3['alt']; ?>">
</a>
<?php else: ?>
<img src="<?php echo $grid_image_3['url']; ?>" alt="<?php echo $grid_image_3['alt']; ?>">
<?php endif; ?>
</div><!-- col -->
</div><!-- row -->
<div class="row">
<div class="col-sm-8">
<?php if( $image_4_video == "Yes" ): ?>
<a data-toggle="modal" data-target="#myModal2" href="#">
<img src="<?php echo $grid_image_4['url']; ?>" alt="<?php echo $grid_image_4['alt']; ?>">
</a>
<?php else: ?>
<img src="<?php echo $grid_image_4['url']; ?>" alt="<?php echo $grid_image_4['alt']; ?>">
<?php endif; ?>
</div><!-- col -->
<div class="col-sm-4">
<?php if( $image_5_video == "Yes" ): ?>
<a data-toggle="modal" data-target="#myModal2" href="#">
<img src="<?php echo $grid_image_5['url']; ?>" alt="<?php echo $grid_image_5['alt']; ?>">
</a>
<?php else: ?>
<img src="<?php echo $grid_image_5['url']; ?>" alt="<?php echo $grid_image_5['alt']; ?>">
<?php endif; ?>
</div><!-- col -->
</div><!-- row -->
</div><!-- work images -->
</div><!-- container -->
</section><!-- work -->
<section id="work-text">
<div class="container">
<div class="row">
<div class="col-md-10 col-md-offset-1">
<p><?php echo $about ?></p>
</div><!-- col -->
</div><!-- row -->
</div><!-- container -->
</section><!-- work -->
<!-- Modal -->
<div id="myModal" class="modal fade" role="dialog">
<div class="modal-dialog">
<!-- Modal content-->
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h4 class="modal-title">Video</h4>
</div>
<div class="modal-body">
<iframe src=" <?php echo $video_embed_code_1; ?>" width="500" height="281" frameborder="0" webkitallowfullscreen mozallowfullscreen allowfullscreen></iframe>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
<!-- Modal 2 -->
<div id="myModal2" class="modal fade" role="dialog">
<div class="modal-dialog">
<!-- Modal content-->
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h4 class="modal-title">Video</h4>
</div>
<div class="modal-body">
<iframe src=" <?php echo $video_embed_code_1; ?>" width="500" height="281" frameborder="0" webkitallowfullscreen mozallowfullscreen allowfullscreen></iframe>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
<!-- Modal 3 -->
<div id="myModal3" class="modal fade" role="dialog">
<div class="modal-dialog">
<!-- Modal content-->
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h4 class="modal-title">Video</h4>
</div>
<div class="modal-body">
<iframe src=" <?php echo $video_embed_code_3; ?>" width="500" height="281" frameborder="0" webkitallowfullscreen mozallowfullscreen allowfullscreen></iframe>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
<!-- Modal 4 -->
<div id="myModal4" class="modal fade" role="dialog">
<div class="modal-dialog">
<!-- Modal content-->
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h4 class="modal-title">Video</h4>
</div>
<div class="modal-body">
<iframe src=" <?php echo $video_embed_code_4; ?>" width="500" height="281" frameborder="0" webkitallowfullscreen mozallowfullscreen allowfullscreen></iframe>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
<!-- Modal 5 -->
<div id="myModal5" class="modal fade" role="dialog">
<div class="modal-dialog">
<!-- Modal content-->
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h4 class="modal-title">Video</h4>
</div>
<div class="modal-body">
<iframe src=" <?php echo $video_embed_code_5; ?>" width="500" height="281" frameborder="0" webkitallowfullscreen mozallowfullscreen allowfullscreen></iframe>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
<?php get_template_part( 'template-parts/content', 'otherwork' ); ?>
<?php get_template_part( 'template-parts/content', 'instagram' ); ?>
<?php get_footer(); ?>
Your instinct to make this code more DRY is spot-on. Here's a partial solution demonstrating how you should approach the problem:
<?php
$numVideos = 5;
$grid_image_array = array();
$image_video_array = array();
$video_embed_code_array = array();
for ($i = 1; $i <= $numVideos; $i++) {
$grid_image_array[$i] = get_field('image_' . $i);
$image_video_array[$i] = get_field('image_' . $i . '_do_you_wish_to_add_a_video');
$video_embed_code_array[$i] = get_field('video_embed_code_' . $i);
}
?>
<!-- I snipped out some of your HTML for brevity -->
<?php for ($i = 1; $i <= $numVideos; $i++): ?>
<div id="myModal<?= $i ?>" class="modal fade" role="dialog">
<div class="modal-dialog">
<!-- Modal content-->
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h4 class="modal-title">Video</h4>
</div>
<div class="modal-body">
<iframe src=" <?php echo $video_embed_code_array[$i]; ?>" width="500" height="281" frameborder="0" webkitallowfullscreen mozallowfullscreen allowfullscreen></iframe>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
<?php endfor; ?>