Making php (WordPress) code DRY which has slight variations [closed] - php

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

Related

How to get pagination working on my page using php and wordpress?

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

bootstrap modal inside a php while loop to display different infornmation

so i have a while loop and i am fethcing the name and the description of them from my db
so when ever i click on one of the parts i want the modal to display the name of the item that i clicked on in the modal i hope this picture would describe it better
below is the code i have so far
at the moment when i click on the modal i get displayed the name of the item which is first on the list no matter where i click
while($row = mysqli_fetch_assoc($result))
{
$name= $row['name_of_product'];
$description = $row['description']
?>
<a href="#" data-toggle="modal" data-target="#mymodal">
<div class="col-sm-4">
<?php echo name; ?>
<?php echo description ; ?>
</div>
</a>
<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">Modal Header</h4>
</div>
<div class="modal-body">
<p><?php echo $name; ?></p>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
<?php } ?>
You are not doing things the way you have to do. You are re-creating your mymodal for every iteration of your while loop which is not a better way to achieve what you want.
Follow these steps:
Create your mymodal outside of your while loop.
Pass the ID of current row to a javascript function and populate the data of that id using javascript.
I have set the things which needs to be done. Try the following code
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<!--- display table header row-->
<table style="width:100%">
<tr>
<th>name</th>
<th>description</th>
<th>Action</th>
</tr>
<?php
while($row = mysqli_fetch_assoc($result))
{
$name= $row['name_of_product'];
$description = $row['description'];
$id = $row['id']; // I am assuming that your table has auto incremented primary key column by the name of id, if not then replace that by $row['your_id_column']
?>
<!--- desplay the data in a table row -->
<tr>
<td id="<?php echo "name_".$id; ?>"><?php echo $name; ?></td>
<td id="<?php echo "description_".$id; ?>"><?php echo $description ; ?></td>
<td><div href="#" data-toggle="modal" data-target="#mymodal" onClick="showModal(<?php echo $id ; ?>)">Edit</div></td>
</tr>
<?php } ?>
</table>
<!--- Your 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">Modal Header</h4>
</div>
<div class="modal-body">
<p id="name"></p> <!--- name will be shown here-->
<p id="description"></p><!--- description will be shown here-->
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
<script>
function showModal(id)
{
// setting the values of clicked item in the bootstrap Modal
$("#name").text($("#name_"+id).text());
$("#description").text($("#description_"+id).text());
}
</script>
Looks like you are not serializing the modals... If you wanted to do things that way your code should be something like;
<!-- language-all: lang-html -->
while($row = mysqli_fetch_assoc($result)) {
$procuct_id = $row['ID'];
$name = $row['name_of_product'];
$description = $row['description']
?>
<a href="#" data-toggle="modal" data-target="#mymodal_<?php echo $procuct_id;?>">
<div class="col-sm-4">
<?php echo name; ?>
<?php echo description ; ?>
</div>
</a>
<div id="mymodal_<?php echo $procuct_id;?>" 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">Modal Header</h4>
</div>
<div class="modal-body">
<p><?php echo $name; ?></p>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
<?php }
<--
A Better way to do it would be to write a javascript function to show the generic modal window, then run an ajax call to load your info into the "modal-body" div. You would have to pass a unique id to the call in order to have your ajax script hit the db and get the info to display.
It's been almost 4 years but I'm writing for everyone; change data-target(id) as row id.
edited code:
while($row = mysqli_fetch_assoc($result))
{
$name= $row['name_of_product'];
$description = $row['description']
?>
<a href="#" data-toggle="modal" data-target="#<?php echo $row['id']; ?>">
<div class="col-sm-4">
<?php echo name; ?>
<?php echo description ; ?>
</div>
</a>
<div id="<?php echo $row['id']; ?>" 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">Modal Header</h4>
</div>
<div class="modal-body">
<p><?php echo $name; ?></p>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
<?php } ?>

Creating a custom Woocommerce product modal on click

I have created a basic HTML/jQuery structure for a modal that will pop up when clicked on. I'm in the process of integrating Woocommerce into my site and am unsure as to how I can do the following:
Have modal load respective product info
Integrate into a Woocommerce loop
Here is my HTML for the skeleton of what I'm trying to achieve:
<div class="col-md-4"><img src="<?php bloginfo('stylesheet_directory'); ?>/media/temp_product_images/Alto-183x300.jpg" alt="Product Img" class="product-img" data-toggle="modal" data-target="#myModal">
<p class="product-img-title text-center">Alto</p>
<!-- Modal -->
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
<h4 class="modal-title text-center" id="myModalLabel">Alto</h4> </div>
<div class="modal-body" id="modal-body-one">
<div class="row">
<div class="col-sm-4"> <img src="<?php bloginfo('stylesheet_directory'); ?>/media/temp_product_images/Alto-183x300.jpg" alt="Modal Product Img" id="product-img"> </div>
<div class="col-sm-7">
<h5>Colours</h5>
<div class="scroll-container">
<div class="row">
<div class="col-xs-4">
<div class="selected-product-color"><img src="<?php bloginfo('stylesheet_directory'); ?>/media/temp_product_images/MACASSA.jpg" alt="Product Colour">
<p>Macassa</p>
</div>
</div>
</div>
<!-- End of Test Case -->
</div>
</div>
<div class="col-sm-1">
<button type="button" class="btn flip"><i class="fa fa-arrow-right" aria-hidden="true"></i></button>
</div>
</div>
</div>
<!-- Prices -->
<div class="modal-body" id="modal-body-two">
<div class="row">
<div class="col-sm-11">
<div class="row">
<h5 id="modal-prices">Download Price List</h5></div>
<div class="row">
<?php $my_query = new WP_Query('p=47');
while($my_query->have_posts()){
$my_query->the_post();
the_content();
} ?>
</div>
</div>
<div class="col-sm-1">
<button type="button" class="btn flip"><i class="fa fa-arrow-right" aria-hidden="true"></i></button>
</div>
</div>
</div>
<div class="modal-footer"> Get Quote </div>
</div>
</div>
</div>
<!-- End of Modal -->
</div>
And here is my jQuery:
//Modal Change Content
$('#modal-body-two').hide();
$('.flip').on('click', function () {
$('#modal-body-one, #modal-body-two').toggle()
});
Is there a way that I can display the product info on click within a modal using Woocommerce? Thanks

pass different variables in bootstrap modal

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>

php select fetch while loop into bootstrap modal

Have some db information that I'm trying to get into some modals.
Problem seems to be that the modal only ever grabs the variables from the last while loop. Does all the php on a page run first? Even when its not called?
So I know there is probably easier ways to do this using get_results() and fetch_array and fetch_row but those don't seem to work for me in php 5.5.
Also, I read somewhere to use AJAX. Well as I have never used ajax before, is this something I should look into?
<div class="col-md-4">
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
require ($_SERVER['DOCUMENT_ROOT'].'/db-connect.php');
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
//echo $conn->host_info . "\n";
if ($stmt = $conn->prepare("SELECT time, title, tool, descript, thumbpath, smallpath, mediumpath, largepath FROM websites ORDER BY id DESC LIMIT 1")){
//$stmt->bind_param('s',$id);
$stmt->execute();
$stmt->store_result();
$stmt->bind_result($time, $title, $tool, $descript, $thumbpath, $smallpath, $mediumpath, $largepath);
while ($stmt->fetch()) {
}
$stmt->free_result();
$stmt->close();
}
$conn->close();
?>
<img class="img-responsive" title="<?php echo $tool; ?>" data-toggle="modal" data-target="#modalPort" sizes="100vw" src="<?php echo $thumbpath; ?>" srcset="<?php echo $smallpath; ?> 500w, <?php echo $mediumpath; ?> 1000w, <?php echo $largepath; ?> 1500w" alt="Portfolio Site">
<span class="time line-height-small"><?php echo $time; ?></span>
</div>
The variable's here work fine. The problem is that I'm running this same php script a few other times with the same bind_result variables. I don't really want to change variables for each modal.
Modal:
<!-- Website Modals-->
<div class="modal fade" id="modalPort" tabindex="-1" role="dialog" aria-labelledby="portfolioModallabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h4 class="modal-title" id="portfolioModallabel"><?php echo $title; ?></h4>
</div>
<div class="modal-body text-center">
<img class="img-responsive center-block" src="<?php echo $thumbpath; ?>" sizes="100vw" srcset="<?php echo $smallpath; ?> 500w, <?php echo $mediumpath; ?> 1000w, <?php echo $largepath; ?> 1500w" alt="Portfolio Site">
<p class="line-height-small"><?php echo $descript; ?></p>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</div>
</div>
One way is to put modal inside while ($stmt->fetch()) { } assign both modal trigger button and modal itself unique id
Note the data-target="#modalPort<?php echo $id;?>" in modal trigger button and id="modalPort<?php echo $id;?>" in modal HTML
<?php while ($stmt->fetch()) { ?>
<button data-toggle="modal" data-target="#modalPort<?php echo $id;?>" class="btn btn-default">Modal</button>
<div class="modal fade" id="modalPort<?php echo $id;?>" tabindex="-1" role="dialog" aria-labelledby="portfolioModallabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h4 class="modal-title" id="portfolioModallabel"><?php echo $title; ?></h4>
</div>
<div class="modal-body text-center">
<img class="img-responsive center-block" src="<?php echo $thumbpath; ?>" sizes="100vw" srcset="<?php echo $smallpath; ?> 500w, <?php echo $mediumpath; ?> 1000w, <?php echo $largepath; ?> 1500w" alt="Portfolio Site">
<p class="line-height-small"><?php echo $descript; ?></p>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
<?php } ?>
Alternate If you want to do with ajax, put modal HTML outside the while loop, add <?php echo $id;?> as data attribute to modal trigger button data-id="<?php echo $id;?>"
<?php while ($stmt->fetch()) { ?>
<button data-toggle="modal" data-target="#modalPort" data-id="<?php echo $id;?>" class="btn btn-default">Modal</button>
<?php } ?>
Modal event listener to get the data attribute value and later use in Ajax method to get the respective row data to show in modal
$(document).ready(function(){
$('#modalPort').on('show.bs.modal', function (e) {
var dataid = $(e.relatedTarget).data('id');
var dataString = 'dataid=' + dataid;
$.ajax({
type: "POST",
url: "get_data.php",
data: dataString,
cache: false,
success: function(data){
$("#content").html(data);
}
});
});
});
get_data.php will be
<?php
//Include database connection
if($_POST['dataid']) {
//run query against `dataid`
?>
<div class="modal-header">
<h4 class="modal-title" id="portfolioModallabel"><?php echo $title; ?></h4>
</div>
<div class="modal-body text-center">
<img class="img-responsive center-block" src="<?php echo $thumbpath; ?>" sizes="100vw" srcset="<?php echo $smallpath; ?> 500w, <?php echo $mediumpath; ?> 1000w, <?php echo $largepath; ?> 1500w" alt="Portfolio Site">
<p class="line-height-small"><?php echo $descript; ?></p>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
<?php } ?>
and Modal HTML will be (outside the loop)
<div class="modal fade" id="modalPort" tabindex="-1" role="dialog" aria-labelledby="portfolioModallabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div id="content"></div> //display data output via ajax
</div>
</div>
</div>

Categories