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>
Related
So I have succeeded in passing the variables from one modal to another.The problem is they are multiple variables and I am looping them with the PHP foreach function, all the items are displayed but when I click on any item only the first variable is passed and not the specific one I chose. How do I fix this
here is the code for the initial modal
<div class="modal fade bs-modal-md" id="cardmodal" tabindex="-1" role="dialog" aria-
labelledby="mySmallModalLabel" aria-hidden="true">
<div class="modal-dialog modal-md">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalLongTitle">SELECT A CARD</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<div class="mini-carousel">
<ul class="mini-item">
<?php
$cards = $card->load_category('WOMEN','WR');
$category_products=$cards['category_products'];
$num_rows = count($category_products);
if ($num_rows < 1) {
echo '<div class="info">We shall update you when this is back in Stock</div>';
?>
<a onClick="pageModel(this);" href="javascript:void(0);" id="<?php echo
site_url('home/getview/contact_form/'.$categorynameID); ?>" >Click here to submit your
contact to be updated</a>
<?php
} else {
foreach($category_products as $cp)
{
$status = $cp["status"];
$product_quantity = $cp["quantity"];
$notify_quantity = $cp["reorder_level"];
$images = $this->home_m->get_product_images($cp['inventory_id'],true);
if($images){
$image = $images[0];
$image_name = $image["thumb"].'.'. $image["extension"];
$image_source = $image['source'];
$check_pic = DEFAULT_UPLOAD.'/'.$image_source.'/'.$image_name;
$product_image = $image_name;
}else{
$product_image = '';
}
?>
<li class="product-item col-xs-12 col-sm-4 col-md-3">
<div class="product-inner">
<div class="product-thumb has-back-image zoom">
<a id="testd" data-id="<?php echo base_url($check_pic); ?>" data-
dismiss="modal" data-toggle="modal" onclick="addModalDetails()" href="#single_card_view">
<? php
if(($product_image != "") || (($product_image != NULL))) {
if (file_exists($check_pic)) { ?>
<img id="cardimage" src="<?php echo base_url($check_pic); ?>" alt="Product Image" ></img>
<?php } else {
echo "<img class=\"img-fluid\"
src=\"http://geestar.co.tz/images/category/product.png\" alt=\"Product Image\" />";
}
} else {
echo "<img class=\"img-fluid\" src=\"http://geestar.co.tz/images/category/product.png\"
alt=\"Product Image\" />";
}
?>
</a>
</div>
<div class="product-info">
<h6 id="cardname" class="product-name"><?=$cp["product_name"];?></a>
</h6>
<span class="price">
<ins id="cardprice">TZS <?=number_format($cp["selling_price"]);?
>
</ins>
</span>
</div>
</div>
</li>
<?php
}
}
?>
this is the JS Code where i pass the values to the other modal
function addModalDetails(){
var name = $('#cardname').html();
var image= $('#cardimage').html();
var price =$('#cardprice').html();
$('#heading').html(name);
$('#cost').html(price);
var imgsrc = $('#testd').data('id');
$('#picture').attr('src',imgsrc);
}
And this is the code of the modal where i want the data to be passed
<div class="modal fade product_view" id="single_card_view">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<a href="#" data-dismiss="modal" class="class pull-right"><span class="glyphicon
glyphicon-remove"></span></a>
<h3 id="heading" class="modal-title"></h3>
</div>
<div class="modal-body">
<div class="row">
<div class="col-md-6 product_img">
<img id="picture" src="" class="img-responsive">
</div>
<div class="col-md-6 product_content">
<p> <input id="gift_message" class="form-control" rows="5" placeholder="<?
php _l('Gift Message'); ?>" name="gift_message" > </input></p>
<h3 id="cost" class="cost"></span>TZS
<? =number_format($cp["selling_price"]);?></h3>
<div class="row">
<!-- end col -->
<!-- end col -->
<!-- end col -->
</div>
<div class="space-ten"></div>
<div class="btn-ground">
<button type="button" class="button" data-dismiss="modal"
onClick="addCardDetails()"> Add Card</button>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
You're currently using a foreach loop to assign the same ID to multiple html tags. For simplicity of this answer, I will be taking your <img> tag with the ID cardimage as example. Furthermore I'd like to note that your ID's should always be unique!
The problem
<img id="cardimage" src="<?php echo base_url($check_pic); ?>" alt="Product Image"></img>
Every team you're looping through the $category_products array, you're adding another one of those tags, identical to the last one.
For example if you loop 3 times through the $category_products array, you will get 3 times the same ID.
<img id="cardimage" src="<?php echo base_url($check_pic); ?>" alt="Product Image"></img>
<img id="cardimage" src="<?php echo base_url($check_pic); ?>" alt="Product Image"></img>
<img id="cardimage" src="<?php echo base_url($check_pic); ?>" alt="Product Image"></img>
When there are multiple identical ID's on a webpage, your javascript will pick the first one it can find, and run your script on that, as it only expects one result. To fix this, you should make all your ID's unique.
A possible solution
First of all, make all of your ID attributes unique. In your loop you're looping through $category_products as $cp, and you appearantly have the variable $cp['inventory_id']. I'll be taking that as unique id for this answer for now.
You could append this unique ID behind your ID like this:
foreach($category_products as $cp) { ?>
<img id="cardimage-<?=$cp['inventory_id']?>" src="<?php echo base_url($check_pic); ?>" alt="Product Image"></img>
<?php }
Then change your addModalDetails() like this:
function addModalDetails(id){
var name = $('#cardname-'+id).html();
var image= $('#cardimage-'+id).html();
var price =$('#cardprice-'+id).html();
$('#heading-'+id).html(name);
$('#cost-'+id).html(price);
var imgsrc = $('#testd-'+id).data('id');
$('#picture-'+id).attr('src',imgsrc);
}
and call it like this:
addModalDetails(<?=$cp['inventory_id']?>);
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 } ?>
Example :
i have 6 Columns for every row.
Column1 = personid
Column2 = Image
Column3 = FirstName
Column4 = MiddleName
Column5 = LastName
Column6 = Position
Example:
Person ID Image FirstName MiddleName LastName Position
1 Picture1.jpg John Doe Peter Manager
The PHP Code is:
<?php
include('connect.php');
$selected=mysql_query("SELECT * FROM persontbl order by personid");
$i=1;
while($personrow=mysql_fetch_array($selected))
{
$personid=$personrow['personid'];
$personimage=$personrow['personimage'];
$firstname=$personrow['firstname'];
$middlename=$personrow['middlename'];
$lastname=$personrow['lastname'];
$position=$personrow['position'];
?>
I have created this code to display specific information of the row on grid view using bootstrap(Image and FirstName only):
<div class="col-sm-2">
<img src="<?php echo $personimage;?>" alt="">
<p><h5 align="center"><?php echo $FirstName; ?></h5></p>
<hr/>
</div>
And the modal form is:
<!-- Modal content-->
<div class="modal-content">
<div class="modal-header">
<button type="button" class="btn btn-info close" data-dismiss="modal">Close Form ×</button>
<h4 class="modal-title">Full Information of the Person</h4>
</div>
<div class="modal-body">
<p><img src="<?php echo $personimage; ?>" width="25%" height="25%" alt="">
<h5><b> Person's Name: </b></h5><?php echo $FirstName; ?></p>
<hr/>
<p><h5><b>Person's Middle Name: </b></h5><?php echo $middlename; ?></p>
<p><h5><b>Person's Last Name: </b></h5><?php echo $lastname; ?></p>
<p><h5><b>Person's Position: </b></h5><?php echo $position; ?></p>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close Form</button>
</div>
</div>
Then I want to display full information on Modal View. Then When I click on one record row, the Modal View will pop up and display the full information for the SELECTED record ROW.
But NOW, The Modal shows only the information of the first row. When I click on another row, It's not showing the full information of the selected row, It only displays the first row's data. But It still opens the Modal.
What should I do to display the selected record full information from the database to the modal view using PDO Method? please support me with full code detail.
this is code i tested in localhost
<?php
include('login/dbconnect.php');
$selected=mysqli_query($conn,"SELECT * FROM leads");
$i=1;
?>
<link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>
<script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
<?php
while($personrow=mysqli_fetch_array($selected))
{
$personid=$personrow['ID'];
$FirstName=$personrow['FIRST_NAME'];
?>
<div class="col-sm-2">
<img src="<?php echo $personimage;?>" alt="">
<p><h5 align="center"><?php echo $FirstName; ?></h5></p>
<hr/>
</div>
<div id="<?php echo $personid ?>" class="modal fade" role="dialog">
<div class="modal-dialog">
<!-- Modal content-->
<div class="modal-content">
<div class="modal-header">
<button type="button" class="btn btn-info close" data-dismiss="modal">Close Form ×</button>
<h4 class="modal-title">Full Information of the Person</h4>
</div>
<div class="modal-body">
<p>hiiiiiiiiiiiiiiii</p>
<p><?php echo $FirstName; ?></p>
<!-- <h5><b> Person's Name: </b></h5><?php echo $FirstName; ?></p>
<hr/>
<p><h5><b>Person's Middle Name: </b></h5><?php echo $middlename; ?></p>
<p><h5><b>Person's Last Name: </b></h5><?php echo $lastname; ?></p>
<p><h5><b>Person's Position: </b></h5><?php echo $position; ?></p> -->
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close Form</button>
</div>
</div>
</div>
</div>
<?php
}
?>
pass unique id on each row and in html tag use onclick method and pass the id
based on id u can fetch the data from the database by using ajax and then show the modal
Example :
<a href = '#' onclick = '<?php callback(echo $id);?>' >click here </a>
the java-script code like
<script>
function callback($id){
var $id = $id;
$.ajax({
url : '<?php echo $fetch_url?>';
type: 'post',
data : {id,$id},
success : function(data){
var data = eval(data);
$.each(data,function(index ,value){
$('#some_id').html(value.key_of_value);
$('#some_id').html(value.key_of_value);
});
$('#popupmodel').modal('show');
}
});
}
</script>
also no need to write this inside <a> tag data-toggle="modal" data-target="#myModal"
and set the default modal like as
<div id="popupmodel" class="modal fade">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
some header
</div>
<input type = 'text' id= 'some_id'>//as per your requirement
</div>
</div>
fetch url will be your pdo query code. and after getting the result
convert it json using json_encode method and echo the result in fetch url
code like echo json_encode($fetch_data);
you have to use dynamic id to call every person info .use personid as id of anchor tag and modal id .
<?php
include('connect.php');
$selected=mysql_query("SELECT * FROM persontbl order by personid");
$i=1;
while($personrow=mysql_fetch_array($selected))
{
$personid=$personrow['personid'];
$personimage=$personrow['personimage'];
$firstname=$personrow['firstname'];
$middlename=$personrow['middlename'];
$lastname=$personrow['lastname'];
$position=$personrow['position'];
<div class="col-sm-2">
<img src="<?php echo $personimage;?>" alt="">
<p><h5 align="center"><?php echo $FirstName; ?></h5></p>
<hr/>
</div>
<div id="<?php echo $personid ?>" class="modal fade" role="dialog">
<div class="modal-dialog">
<!-- Modal content-->
<div class="modal-content">
<div class="modal-header">
<button type="button" class="btn btn-info close" data-dismiss="modal">Close Form ×</button>
<h4 class="modal-title">Full Information of the Person</h4>
</div>
<div class="modal-body">
<p><img src="<?php echo $personimage; ?>" width="25%" height="25%" alt="">
<h5><b> Person's Name: </b></h5><?php echo $FirstName; ?></p>
<hr/>
<p><h5><b>Person's Middle Name: </b></h5><?php echo $middlename; ?></p>
<p><h5><b>Person's Last Name: </b></h5><?php echo $lastname; ?></p>
<p><h5><b>Person's Position: </b></h5><?php echo $position; ?></p>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close Form</button>
</div>
</div>
</div>
</div>
}
?>
I have this while loop..
while($list = $cars->fetch(PDO::FETCH_ASSOC)) {
<div class="car-box modalButton" id="tbinfo" data-title="<?php echo $list['title'] ?>" data-toggle="modal" data-src="http://url.com/car.php?q=<?php echo $list['rand_num']; ?>" data-height="<?php echo $height; ?>" data-width="<?php echo $size[0]; ?>" data-target="#preview">
<div class="modal fade" id="preview" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
<h4 class="modal-title" id="myModalLabel"><?php echo $list['title']; ?></h4>
</div>
<div class="modal-body">
<iframe style="display:block;" frameborder="0"></iframe>
</div>
<div class="modal-footer">
</div>
</div><!-- /.modal-content -->
</div><!-- /.modal-dialog -->
</div><!-- /.modal -->
}
So when i click in the first car of the list my modal popup opens and shows the title normal as it is. When i click on the second car and the modal popup opens it shows me again the title of the first car instead of second car that i clicked on. I tried with jquery to take the title on #tbinfo click with no success:
$('#tbinfo').on('click',function(){
var title = $(this).attr('data-title');
$('.modal-title').html(title);
});
What can i do to fix it? any ideas?
In my project I'm using data- attributes.
Example. Look, how do I pass code to modal.
Link:
<a href="#modal-info" class="text-muted" data-toggle="modal" data-code="{{ code }}">
Modal:
<div class="modal fade" id="modal-info">
<dd name="code"></dd>
</div>
Event listener:
$('#modal-info').on('show.bs.modal', function (event) {
var button = $(event.relatedTarget); // Button that triggered the modal
var code = button.data('code');
var modal = $(this);
modal.find('[name="code"]').text(code);
}
hello you update your code like:
<?php
$i=1;
while($list = $cars->fetch(PDO::FETCH_ASSOC)) {?>
<div class="car-box modalButton tbinfo" data-title="<?php echo $list['title'] ?>" data-toggle="modal" data-src="http://url.com/car.php?q=<?php echo $list['rand_num']; ?>" data-height="<?php echo $height; ?>" data-width="<?php echo $size[0]; ?>" data-target="#preview_<?php echo $i; ?>">
<div class="modal fade" id="preview_<?php echo $i; ?>" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
<h4 class="modal-title" id="myModalLabel"><?php echo $list['title']; ?></h4>
</div>
<div class="modal-body">
<iframe style="display:block;" frameborder="0"></iframe>
</div>
<div class="modal-footer">
</div>
</div><!-- /.modal-content -->
</div><!-- /.modal-dialog -->
</div><!-- /.modal -->
<?php $i++; }?>
assign each modal unique id
You just need to increment your html id attributes as its currently always going to reference the first modal created. All id's must be unique in html.
I would just add in a simple counter and append it to your id. So create a new variable, in this example $i and assign it the value of 0. In your loop, increment the value either at the beginning or end of the loop. Then append the value of $i to your modal id attribute and the modalButton's data-target attribute. So preview becomes something like preview-<?=$i;?> in all instances.
Here, something like this:
<?php
$i = 0;
while($list = $cars->fetch(PDO::FETCH_ASSOC)) {
++$i;
?>
<div class="car-box modalButton" id="tbinfo" data-title="<?php echo $list['title'] ?>" data-toggle="modal" data-src="http://url.com/car.php?q=<?php echo $list['rand_num']; ?>" data-height="<?php echo $height; ?>" data-width="<?php echo $size[0]; ?>" data-target="#preview-<?=$i;?>">
<div class="modal fade" id="preview-<?=$i;?>" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
... modal markup ...
Make sure your PHP installation supports the shorthand echo <?=$VAR;?>, if that isn't working then you can just echo the variable out as normal, like you've done else where <?php echo ... ?> .
That will make sure that every time you click the button, it opens a new, unique, modal instead of just the first one.
Don't use id. Use class name.
<?php
while($list = $cars->fetch(PDO::FETCH_ASSOC)) {?>
<div class="car-box modalButton" id="tbinfo" data-title="<?php echo $list['title'] ?>" data-toggle="modal" data-src="http://url.com/car.php?q=<?php echo $list['rand_num']; ?>" data-height="<?php echo $height; ?>" data-width="<?php echo $size[0]; ?>" data-target="#preview"></div>
}?>
Place this code in footer or in same page in down.
<div class="modal fade" id="preview" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
</div><!-- /.modal-content -->
</div><!-- /.modal-dialog -->
</div><!-- /.modal -->
JS
<script>
$('.modalButton').click(function(){
var title = $(this).attr('data-title');
$.ajax({url:"ajax_modalTitle.php?title="+title,cache:false,success:function(result){
$(".modal-content").html(result);
}});
});
</script>
Create a page name ajax_modalTitle.php (If you are looking to change this page name. Change in <script></script> tag too. Both are related.)
<?php
$title = $_GET['title'];
?>
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
<h4 class="modal-title" id="myModalLabel"><?php echo $title;?></h4>
</div>
<div class="modal-body">
<iframe style="display:block;" frameborder="0"></iframe>
</div>
<div class="modal-footer">
</div>
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; ?>