Getting primary key/id of content on main page on a modal - php

I am working on a music web app.On one page I have audios and I want to store it in a playlist. I click on add button so a modal with all the available playlists show up. On this modal I have the ids of playlist, I want to move the id of that audio here but something messy show up. Here is the code
<div id="owl-demo" class="owl-carousel owl-theme">
<?php
$audio=Audio::view_all();
foreach ($audio as $audios) {
$artist=Artist::find_by_id($audios->id);
?>
<div id="playlist" class="audio-cover">
<img src="admin/uploads/images/<?php echo $audios->audio_img_name; ?>">
<h5><?php echo $audios->audio_title; ?></h5><h5><?php echo "artist"; ?></h5>
<h6><?php echo $audios->audio_plays_count; ?> views </h6>
<div class="clear-fix"></div>
<h6><?php echo $audios->audio_likes_count; ?>likes</h6>
<a style="display:none;"track="<?php echo $audios->audio_link_name; ?>"></a>
<i class="fa fa-plus-square add"></i>
<i class="fa fa fa-heart like"></i>
<!--<div class="audio-cover-overlay"><li track="<?php echo $audios->audio_link_name; ?>" cover="<?php echo $audios->audio_img_name; ?>.jpg" artist="<?php echo $audios->audio_title ."-". $artist->name; ?>"><i id="play" class="fa fa-play-circle fa-2x play"></i></li></div>
!-->
</div>
<?php } ?>
</div>
<div class="modal" id="modal_playlists">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h3 class="modal-title">Add To Playlist</h3>
</div>
<div class="modal-body">
<?php
$playlist=Playlist::view_all();
foreach ($playlist as $playlists) {
?>
<img src="./admin/uploads/playlist_images/<?php echo $playlists->img_link;?>" height="100px" width="100px">
<?php echo $playlists->title;?><br>
<?php }?>
</div>
</div>
</div>
</div>
Modal shows up for a second but then, this messy thing
Messy Thing after one second appearance of modal
ANy kind of help will be appreciated. Thank you

You can Carry audio id to model by bellow steps
First you have prevent open model automatically.
Create function which call on click of audio div or any element
on that function set audio id in var.
now trigger model, progamicaly. and stores audio id at any where in model by using element.html() or element.val()

Related

Bootstrap - Open off-canvas with GET Request PHP

Hi I am working on a project that use bootstrap. I have edit button and that button sends a get request. What I want to do is send that GET request and get it from the bootstrap off-canvas. My off-canvas opens with data-bs-target. Any help?
edit_album_item is the page where off-canvas included
My main page:
<?php include "includes/edit_album_item.php" ?>
<div class="col-md-4" style="margin-top: 20px;">
<div class="card">
<div class="row" style="position: absolute;">
<div class="col-md-4">
<button class="btn btn-danger delete-card"><i class="fa-solid fa-trash"></i></button>
</div>
<div class="col-md-6">
<button class="btn btn-warning delete-card"><i class="fas fa-edit"></i></button>
</div>
</div>
<img src="includes/images/<?php echo $item_image ?>" class="card-img-top" alt="...">
<div class="card-body">
<h5 class="card-title"><?php echo $item_title ?></h5>
<p class="card-text"><?php echo $item_description ?></p>
</div>
</div>
</div>
Actually the edit_album_item.php is not well developed yet. I want send a get request and open off-canvas get the information from the get request. Thanks in advance!
Example Image
I understand your question. To do this what you need to do is,
add anchor to your edit button to send get request like this
index.php?yourreq
and try this is on the bottom of your page
if(isset($_GET['your req'])) {
echo '
<script>
$("#offcanvasAlbumItem").modal("show");
</script>
';
}
I think now you are good to go.

How do i pass multiple Variables to a modal

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']?>);

Problem with holding id of current clicked post on my webapp

I have a page where i listed a posts from a database and I want to do that when i clicked a specific post it redirect me to another page with blank area where is more details with ID of this post. I don't know how to do that the blank area will which post were clicked. (Sorry for my english)
<div class="row">
<?php
foreach ($viewmodel as $item) : ?>
<div class="col-sm-4">
<div class="card shadow">
<div class="card-header border-dark shadow"><?php echo $item['TITLE']; ?></div>
<div class="card-body">
<p class="card-text"><?php echo $item['DESCRITIONS']; ?></p>
<a class="btn badge-secondary btn-sm" type="submit"
href="<?php echo ROOT_PATH; ?>shares/zobaczZadanie">More</a>
</div>
<div class="card-footer">
<small class="text-muted"><?php echo $item['DATE']; ?></small>
</div>
</div>
</div>
<?php endforeach; ?>
But I don't have acces to specific post

Show Selected row full information on Modal View

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

Bootstrap: Same ID Models inside and outstide of Carousel Display Issue

I could paste all my code here but thought it may not help so I tried to paste the important stuff. I hope I am asking the questions appropriately so please feel free to guide me. I will explain the logic below and show what is not working.
In Bootstrap, I have a carousel where models are dynamically created in the following manner.
i. Begin Carousel
<div class="hero-unit" style="padding-right:30px; padding-left:30px; padding-top:30px; padding-bottom: 50px;">
<div id="myCarousel" class="carousel slide">
<div class="carousel-inner">
</div>
</div>
</div>
ii. Dynamically create href links to models
<div class="hero-unit" style="padding-right:30px; padding-left:30px; padding-top:30px; padding-bottom: 50px;">
<div id="myCarousel" class="carousel slide">
<div class="carousel-inner">
<?php $carouselClass = "active item";
while($r = mysql_fetch_assoc($events))
{
?>
<div class="<?php echo $carouselClass; ?>">
<?php $carouselClass = "item"; ?>
// Here, we create model dynamically
<a role="button" data-toggle="modal" href="#myModel<?php echo $r['_id']; ?>" ><?php echo $r['_name'] ; ?></a>
</div>
</div>
</div>
iii. Use partials in PHP to improve Don't Repeat Yourself (DRY) operations
<div class="hero-unit" style="padding-right:30px; padding-left:30px; padding-top:30px; padding-bottom: 50px;">
<div id="myCarousel" class="carousel slide">
<div class="carousel-inner">
<?php $carouselClass = "active item";
while($r = mysql_fetch_assoc($events))
{
?>
<div class="<?php echo $carouselClass; ?>">
<?php $carouselClass = "item"; ?>
// Here, we create model dynamically
<a role="button" data-toggle="modal" href="#myModel<?php echo $r['_id']; ?>" ><?php echo $r['_name'] ; ?></a>
<?php require '_actualModel.php'; ?>
</div>
</div>
</div>
iv. _actualModel.php
<div id="myModel<?php echo $r['_id']; ?>" class="modal hide fade" tabindex="-1" aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-header center">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
</div>
<div class="modal-body">
<?php echo $r['_body'];
</div>
<div class="modal-footer">
<button class="btn" data-dismiss="modal" aria-hidden="true">Close</button>
</div> </div>
Now, this works very well because the carousel cycles the content and the models pop up. However, suppose on the same page I would like to make a model with the same id but outside the carousel. I would go about doing it this way.
// Here, we create model which would have the same id as one of them in the carousel
<a role="button" data-toggle="modal" href="#myModel<?php echo $r['_id']; ?>" ><?php echo $r['_name'] ; ?></a>
<?php require '_actualModel.php'; ?>
The problem is the model (with the same id) created outside the carousel will only pop up if the model within the carousel (with the same id) is active at that point in time; that is, it is in display. If it is not active or within display, then the background will grey out, but the pop up model will not come into view.![Here is what happens when the model with the same id outside the carousel is click while the model with the same id inside is not in display - Can't upload picture yet :) ][3]
May I state that my reason for having models with the same ID is because they have the exact same information and so I am trying to implementing DRY practices. Sorry about the indentation.
Figured out a possible solution. Like #Carsten said, it is not right to have elements of the same ID. But here is a solution to somebody who lingers on the same problem; that is trying to make dynamic modals from inside and outside a carousel where some modals may have the same id if using the above approach (in question).
The trick is to give the carousel modals a unique identifier and the same goes for outside the carousel.
Thus, before I begin my carousel, I can assign:
$identifier = "carousel";
and then I append $identifier to the href. That is:
<a role="button" data-toggle="modal" href="#myModel<?php echo $identifier . $r['_id']; ?>" ><?php echo $r['_name'] ; ?></a>
and of course the modal would have
<div id="myModel<?php echo $identifier . $r['_id']; ?>" class="modal hide fade" tabindex="-1" aria-labelledby="myModalLabel" aria-hidden="true">
So, to dynamically create a similar modal outside, I would make $identifier = "outside" and use the same methods to create the href and modal.
Hope this helps someone.

Categories