Im new to Laravel framework and im trying to develop a simple car listing web using Bootstrap Cards. I've successfully implemented AJAX Get including populating data from Controller using Jquery append() method. However it is displaying all cars in one Card. How to populate each Cards containing each car?. current result console.log result
public function GetCars(Request $request)
{
if ($request->ajax())
{
$cars = Car::all();
return response()->json( $cars );
}
}
<div class="album py-1">
<div class="container bg-light">
<div class="row py-2">
<div class="col-md-3">
<div class="card mb-3 shadow-sm">
<div class="hover-container">
<img src="" class="image img-thumbnail">
<a class="Viewbtn text-decoration-none" href="#" style="font-size: small">View</a>
</div>
<div class="card-body CarDetail">
<p class="card-text font-weight-bold">Make:<span class="car_Make"></span></p>
<p class="card-text font-weight-bold">Model:<span class="car_Model"></span></p>
<div class="d-flex justify-content-between align-items-center">
<p class="font-italic table_data">Price: RM <span class="car_Year"></span></p>
</div>
</div>
</div>
</div>
</div>
</div>
<button id="testbtn" type="submit" class="btn btn-success">AJAX</button>
</div>
<script>
$(document).ready(function(){
$('#testbtn').click(function () {
$.ajax({
type: 'GET',
url: '/ajax/getCar',
success : function (data) {
$.each(data, function( i, car ) {
$('.car_Make').append(car.make);
$('.car_Model').append(car.model+'<br>');
$('.car_Year').append(car.year+'<br>');
});
}
});
});
});
</script>
This is what are you meaning?
I created an example, where I'm using some rows from your console.log data.
Explanation:
I am iterating throught the data array. If it is first iteration I'm adding data (one CarDetail div exists in your code.
For the next iterations I am cloning the CarDetail div and fill with next data.
Of course,there are more posibilities how to add your data to page. For example, you can fill the first item in page and then iterate from the second to the last (in that case you won't need the if/else condition) or you can add the HTML code in a JS variable. Personally, I wouldn't do that because it looks very ugly and the readability of the code could get worse.
$(document).ready(function(){
var data = [
{id:31,make:"Audi",model:"R8",year:2000},
{id:32,make:"Ferrari",model:"FF",year:2000},
{id:33,make:"Mazda",model:"Rx-7",year:2000},
{id:34,make:"Ferrari",model:"F12",year:2012},
{id:35,make:"Porsche",model:"911",year:2015},
{id:36,make:"Land Rover",model:"Vogue",year:2015},
{id:37,make:"Mercedes Benz",model:"SL350",year:2013},
{id:38,make:"Ferrari",model:"F12",year:2013},
{id:39,make:"Ferrari",model:"458",year:2013},
];
$.each(data, function( i, car ) {
if(i == 0) {
$('.card').find('.car_Make').text(car.make);
$('.card').find('.car_Model').text(car.model);
$('.card').find('.car_Year').text(car.year);
} else {
var carDetailCloned = $('.card').first().clone();
carDetailCloned.find('.car_Make').text(car.make);
carDetailCloned.find('.car_Model').text(car.model);
carDetailCloned.find('.car_Year').text(car.year);
$('.card-container').append(carDetailCloned);
}
});
});
.CarDetail{background-color:red;margin-bottom:2em}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="album py-1">
<div class="container bg-light">
<div class="row py-2">
<div class="col-md-3 card-container">
<div class="card mb-3 shadow-sm">
<div class="hover-container">
<img src="" class="image img-thumbnail">
<a class="Viewbtn text-decoration-none" href="#" style="font-size: small">View
</a>
</div>
<div class="card-body CarDetail">
<p class="card-text font-weight-bold">Make:
<span class="car_Make"></span>
</p>
<p class="card-text font-weight-bold">Model:
<span class="car_Model"></span>
</p>
<div class="d-flex justify-content-between align-items-center">
<p class="font-italic table_data">Price: RM
<span class="car_Year"></span>
</p>
</div>
</div>
</div>
</div>
</div>
</div>
<button id="testbtn" type="submit" class="btn btn-success">AJAX</button>
</div>
Edit: I edited my code to clone <div class="card mb-3 shadow-sm" for every card and add data in there. Moreover, I add a card-container class in the parent element to use it when I append a new car. Is that enough?
Related
I want to get the name of two contestants from the database using PHP loop and at the same time calculate the total vote of each candidate and display it using ajax jquery but am only getting the first candidate votes while the other candidate is not even showing anything, not even 0.
index.php
<h4 class="header-title mb-3">Presidential Result</h4>
<div class="">
<div class="row text-center">
<?php
$sql = "SELECT * FROM contestants WHERE post='president'";
$query = mysqli_query($conn, $sql);
while($row=mysqli_fetch_array($query)){
?>
<div class="col-md-6">
<p class="text-muted mb-0 mt-3" id="contestname"><?php echo $row['fullname']?></p>
<h2 class="fw-normal mb-3" id="precount">
</h2>
</div>
<?php
}
?>
</div>
The index.php only displays the name of the contestants/candidates.
Query.js
function view_pres_count(){
var presname =$('#contestname').html();
$.ajax({
url: 'prescount.php',
method: 'post',
data:{present:presname},
success: function(data){
data = $.parseJSON(data);
$('#precount').html(data.html);
}
})
}
in Query.js I passed each name to presname using the tag id. At this point even without the ajax request, if I log presname to console I only got the first candidate name.
Any way out of this?
IDs need to be unique. Change to class and do
const presnames = $('.contestname').map(function() {
return this.textContent.trim()
}).get()
console.log(presnames)
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
<h4 class="header-title mb-3">Presidential Result</h4>
<div class="">
<div class="row text-center">
<div class="col-md-6">
<p class="text-muted mb-0 mt-3 contestname">
Jimmy Carter
</p>
<h2 class="fw-normal mb-3" class="precount">
</h2>
</div>
<div class="col-md-6">
<p class="text-muted mb-0 mt-3 contestname">
Ulyssus Grant
</p>
<h2 class="fw-normal mb-3" class="precount">
</h2>
</div>
</div>
You will need to give more details of your precount html but I suggest you return an array and loop over
$(".precount").each(function(i) { $(this).html(data[i].html) })
I'm making an e-commerce capstone project. The query is already okay but the problem is with displaying the data. If I choose a different category the current category will be hidden and only the new category will be displayed.
https://imgur.com/a/YsLj9S2
<section>
<div class="container">
<div class="row justify-content-center">
<div class="col-sm-9" style="border:solid 1px;">
<div class="row">
<div class="col-sm-4" id="test3">
<div class="card" id="test">
<div class="card-body" id="test2">
<div class="product-grid">
<div class="product-image">
<a href="#" class="image" style="background-color:#F3F3F3;">
<img class="pic-1" src="https://media.comicbook.com/2020/05/dragon-ball-super-when-will-should-goku-master-ultra-instinct-1219439.jpeg?auto=webp&width=1200&height=628&crop=1200:628,smart" class="d-block w-100" id>
</a>
<a class="add-to-cart" id="cart" href=""> + </a>
</div>
<div class="product-content">
<h3 class="title" id="productname">Product Name</h3>
<div class="price" id="price"></div>
</div>
<div class="action-buttons">
<button class="btn btn-danger mt-3" id="cart" onclick="addtocart()"><i class="fas fa-shopping-cart"></i> Add to Cart</button>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
function searchcategory(category_id) {
$.ajax({
url: "<?php echo base_url() ?>admin/getproducts/" + category_id,
type: "POST",
dataType: "json",
success: function(data) {
$.each(data, function(key, value) {
let row = `<div class="price" id="price">${value.price}</div>`;
let val = `<h3 class="title" id="productname">${value.product_name}</h3> `;
let test = `<button class="btn btn-danger mt-3" id="cart" onclick="${value.product_id}" ><i class="fas fa-shopping-cart"></i> Add to Cart</button>`;
$('#price').append(row);
$('#price').append();
$('#productname').append(val);
/* $('#test3').append(val);
$('#cart').text(test); */
console.log(test);
});
}
});
}
Help i want to display all of this data in cards. the code already can get the result from model to only problem is to display. my plans is when i click the burgers category it will display all the data in card-body and when i press beverages it will display all the beverages without refreshing. for some reason i need to use jquery.. IMAGE LINK: https://imgur.com/a/2DsBKqG
View:
<div class="col-sm-9" style="border:solid 1px;">
<div class="row">
<div class="col-sm-4">
<div class="card">
<div class="card-body">
<div class="product-grid">
<div class="product-image">
<a href="#" class="image" style="background-color:#F3F3F3;">
<img class="pic-1" src="https://media.comicbook.com/2020/05/dragon-ball-super-when-will-
should-goku-master-ultra-instinct-1219439.jpeg?
auto=webp&width=1200&height=628&crop=1200:628,smart" class="d-block w-100">
</a>
<a class="add-to-cart" href=""> + </a>
</div>
<div class="product-content">
<h3 class="title">Product Name</h3>
<div class="price"></div>
</div>
<div class="action-buttons">
<a class="btn-outline">ADD TO CART</a>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
Jquery:
function searchcategory(category_id)
{
$.ajax(
{
url:"<?php echo base_url() ?>admin/getproducts/"+category_id,
type:"POST",
dataType:"json",
success:function(data)
{
data.forEach(key =>
{
let row = `<div class="price" id="price" >${key.price}</div>`;
});
}
});
}
I'm trying to use a while loop inside in PHP And Ajax.
But I only get the First ID, not the 2nd, 3rd and so on.
Here is the code Of PHP:
<div class="container pt-5 my-5">
<section class="p-md-3 mx-md-5 text-center">
<h2 class="text-center mx-auto font-weight-bold mb-4 pb-2">Our Team</h2>
<div class="row">
<?php while ($t_run=mysqli_fetch_array($t_query)){?>
<div class="col-lg-3 col-md-4 col-sm-6 mb-4">
<div class="p-4">
<div class="avatar w-100 white d-flex justify-content-center align-items-center">
<input type="hidden" name="team" class='team_id' value="<?php echo $t_run['id']?>">
<img src='images/<?php echo $t_run['img']?>'class="team_img rounded-circle z-depth-1-half"/>
</div>
<div class="text-center mt-2">
<h6 class="font-weight-bold pt-2"><?php echo $t_run['name']?></h6>
<p class="text-muted">
<small><i><?php echo $t_run['title']?></i></small>
</p>
<button class="team btn-info border-0 p-2 rounded">View profile</button>
</div>
</div>
</div>
<?php }?>
</div>
</section>
and here is the Ajax code I use
<script type="text/javascript">
$(document).ready(function () {
$('.team').click(function (){
var name = $('.team_id').val();
$('#content_container').load('team-ajex.php',{
id:name
});
});
});
</script>
I'd recommend you to try to change
$('.team_id').val()
to
$(this).parent().parent().find('input[name="team"]').val()
When you try to get $('.team_id') it returns an array of all inputs with this class...
I'm trying to make a page that gets entries from a database and displays them in a <div> called "gallery-item". However the lightbox works, it only shows the first entry of the database. I hope someone can make sense of this code and may be able to help me with my problem.
My html/php/sql
<?php if (isset($_POST["Website"])) {
$query=$db->prepare("SELECT * FROM `portfoliodb`.`website`");
$query->execute();
while ( $row = $query->fetch()){
$website_id = $row['website_id'];
$website_name = $row ['website_name'];
$website_desc_en = $row ['website_desc_en'];
$website_tags = $row ['website_tags'];
$website_image = $row ['website_image'];
$website_type = $row['website_type'];
echo'
<div class="background hidden" id="background"></div>
<a>
<div class="lightbox hidden" id="lightbox">
<div class="box-title hidden" id="box-title">
<h4 class="hidden" id="box-title-h">' . htmlspecialchars($website_name) . '</h4>
<h4 class="close hidden" id="close">X</h4>
</div>
<div class="box-img hidden" id="box-img">
</div>
<div class="box-foot hidden" id="box-foot">
<div class="box-space hidden" id="box-space"></div>
<div class="box-desc hidden" id="box-desc">
<p class="desc-text hidden" id="box-text">'. htmlspecialchars($website_desc_en) .' </p>
</div>
<div class="tag-space-box hidden" id="box-tags-space"></div>
<div class="tag-box hidden" id="box-tags">
<small id="box-small" class="hidden">'. htmlspecialchars($website_tags) .'</small>
</div>
</div>
</div>
</a>
<a>
<div class="gallery-item-web button" id="button">
<p class="gallary-item-text">';
echo htmlspecialchars($website_name);
echo'</p>';
echo '<i class="fa fa-desktop fa-3x position-icon"></i>
</div></a>
';
}}
The gallery shows all entries in the database so that works fine, the only problem is that the lightbox shows the first entry, so if I were to have a database with the entries "Apples, Pears, Oranges", the gallery would display "Apples, Pears, Oranges". But the lightbox would display "Apples" on every single entry.
My Jquery
$(document).ready(function(){
$(".button").click(function(){
$("#background").toggleClass("hidden");
$("#lightbox").toggleClass("hidden");
$("#box-title").toggleClass("hidden");
$("#box-title-h").toggleClass("hidden");
$(".close").toggleClass("hidden");
$("#box-img").toggleClass("hidden");
$("#box-foot").toggleClass("hidden");
$("#box-space").toggleClass("hidden");
$("#box-desc").toggleClass("hidden");
$("#box-text").toggleClass("hidden");
$("#box-tags-space").toggleClass("hidden");
$("#box-tags").toggleClass("hidden");
$("#box-small").toggleClass("hidden");
});
$(".close").click(function(){
$("#background").toggleClass("hidden");
$("#lightbox").toggleClass("hidden");
$("#box-title").toggleClass("hidden");
$("#box-title-h").toggleClass("hidden");
$(".close").toggleClass("hidden");
$("#box-img").toggleClass("hidden");
$("#box-foot").toggleClass("hidden");
$("#box-space").toggleClass("hidden");
$("#box-desc").toggleClass("hidden");
$("#box-text").toggleClass("hidden");
$("#box-tags-space").toggleClass("hidden");
$("#box-tags").toggleClass("hidden");
$("#box-small").toggleClass("hidden");
});
$(".background").click(function(){
$("#background").toggleClass("hidden");
$("#lightbox").toggleClass("hidden");
$("#box-title").toggleClass("hidden");
$("#box-title-h").toggleClass("hidden");
$(".close").toggleClass("hidden");
$("#box-img").toggleClass("hidden");
$("#box-foot").toggleClass("hidden");
$("#box-space").toggleClass("hidden");
$("#box-desc").toggleClass("hidden");
$("#box-text").toggleClass("hidden");
$("#box-tags-space").toggleClass("hidden");
$("#box-tags").toggleClass("hidden");
$("#box-small").toggleClass("hidden");
});
});
(It's pretty bad I know, this is my first time using Jquery)
This is the Jquery for the lightbox, it toggles the class on every item of the lightbox to "hidden". Whih basically makes every item with that class invisible, great for a lightbox.
First off, you don't need to add class of "hidden" to every element. You can just add it to the container element and then all tags inside would also be hidden.
Your main mistake was to have the same ids and classes for all images, you will need to ensure that each image has an unique identifier.
Try my solution: https://jsfiddle.net/3vhv90ce/2/
HTML:
<div class="container1">
<div class="background" id="background"></div>
<a>
<div class="lightbox" id="lightbox">
<div class="box-title" id="box-title">
<h4 class="" id="box-title-h">name</h4>
<h4 class="close" id="close">X</h4>
</div>
<div class="box-img" id="box-img">
</div>
<div class="box-foot" id="box-foot">
<div class="box-space" id="box-space"></div>
<div class="box-desc" id="box-desc">
<p class="desc-text" id="box-text">description</p>
</div>
<div class="tag-space-box" id="box-tags-space"></div>
<div class="tag-box" id="box-tags">
<small id="box-small" class="">tags</small>
</div>
</div>
</div>
</a>
</div>
</div>
<div class="gallery-item-web button" data-id="1">
<p class="gallary-item-text">Click me</p>
<i class="fa fa-desktop fa-3x position-icon"></i>
</div>
<div class="container2">
<div class="background" id="background"></div>
<a>
<div class="lightbox" id="lightbox">
<div class="box-title" id="box-title">
<h4 class="" id="box-title-h">name</h4>
<h4 class="close" id="close">X</h4>
</div>
<div class="box-img" id="box-img">
</div>
<div class="box-foot" id="box-foot">
<div class="box-space" id="box-space"></div>
<div class="box-desc" id="box-desc">
<p class="desc-text" id="box-text">description</p>
</div>
<div class="tag-space-box" id="box-tags-space"></div>
<div class="tag-box" id="box-tags">
<small id="box-small" class="">tags</small>
</div>
</div>
</div>
</a>
</div>
</div>
<div class="gallery-item-web button" data-id="2">
<p class="gallary-item-text">Click me</p>
<i class="fa fa-desktop fa-3x position-icon"></i>
</div>
JS:
$(document).ready(function(){
$(".button").click(function(){
$('.container'+$(this).data('id')).toggleClass("hidden");
});
});
CSS:
.hidden {
display: none;
}
.gallery-item-web {
cursor: pointer;
}
I suppose your problem came from those lines:
<div class="background hidden" id="background"></div>
<div class="gallery-item-web button" id="button">
you have many controls with the same id, And this is ambiguous for jQuery.