[英]jquery while loop
我想顯示每個廣告(縮略圖)的距離。 我正在使用 Google Maps JavaScript API。 問題是只有第一個廣告顯示了一個距離,或者,如果我更改代碼,每個距離都會顯示在每個縮略圖中。
當縮略圖處於 while 循環中時,如何顯示與 Google(或 jQuery)的距離? 非常感謝您的幫助。
這是代碼:
<?php
include 'includes/db.inc.php'; //Connection to the database.
include 'includes/login.inc.php'; //Processes the login and the sign up info to fetch the user info in the database.
?>
<!DOCTYPE html>
<html lang="fr">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<!-- The above 3 meta tags *must* come first in the head; any other head content must come *after* these tags -->
<meta name="description" content="Vous avez besoin d'un service économique? Trouvez-le sur Jobbinn.">
<meta name="keywords" content="petites annonces classées, kijiji, Les PACs">
<meta name="author" content="">
<link rel="icon" href="images/jobbinn_logo.png" type="image/png" sizes="16x16">
<title>Jobbinn</title>
<link href="bootstrap/css/bootstrap.min.css" rel="stylesheet" type="text/css">
<!-- Custom CSS file -->
<link href="bootstrap/css/custom.css" rel="stylesheet" type="text/css">
<!-- Font Awesome -->
<link rel="stylesheet" href="fonts/css/font-awesome.min.css">
<!-- stylesheet for index.php (style folder) -->
<link href="style/index.css" rel="stylesheet" type="text/css">
<!-- Roboto Condensed Font -->
<link href='https://fonts.googleapis.com/css?family=Roboto+Condensed:400,700,300' rel='stylesheet' type='text/css'>
</head>
<body>
<?php
if(isset($_SESSION['email']) && isset($_SESSION['password']) == true) {
$sel_sql = "SELECT * FROM users WHERE email = '$_SESSION[email]' AND password = '$_SESSION[password]'";
if($run_sql = mysqli_query($conn, $sel_sql)) {
if(mysqli_num_rows($run_sql) == 1) {
include 'includes/dash_header.inc.php';
}
}
}
?>
<?php include 'includes/header.inc.php'; ?>
<?php include 'includes/subheader.inc.php'; ?>
<div id="head">
<div class="container bring-down">
<div class="row">
<div class="col-md-12">
<h2>Vous avez besoin d'un service économique? </br> Trouvez-le sur Jobbinn.</h2>
<h4>Trouvez. Achetez. Profitez de la vie.</h4>
<form class="form-group" action="search.php">
<input type="search" name="search" class="form-control" placeholder="Que recherchez-vous?">
<button name="search_submit" type="submit" class="btn btn-danger btn-lg">Trouver un service</button>
<a id="start" href="startselling.php" class="btn btn-danger btn-lg">Commencer à vendre</a>
</form>
<div class="icon-align-right">
<a href="https://www.facebook.com/sharer/sharer.php?u=http%3A//www.jobbinn.com"><i class="fa fa-facebook-square fa-2x icon-spacing" style="color:#F1F1EC;"></i></a>
<a href="https://twitter.com/home?status=Quand%20vous%20voulez%20un%20travail%20bien%20fait%20sans%20que%20%C3%A7a%20vous%20co%C3%BBte%20un%20bras.%20http%3A//www.jobbinn.com%20%23mtl"><i class="fa fa-twitter-square fa-2x icon-spacing" style="color:#F1F1EC;"></i></a>
<a href="https://www.instagram.com/instajobbinn"><i class="fa fa-instagram fa-2x icon-spacing" style="color:#F1F1EC;"></i></a>
<a href="https://plus.google.com/share?url=http%3A//www.jobbinn.com"><i class="fa fa-google-plus-square fa-2x icon-spacing" style="color:#F1F1EC;"></i></a>
<a href="https://www.linkedin.com/shareArticle?mini=true&url=http%3A//www.jobbinn.com&title=Quand%20vous%20voulez%20un%20travail%20bien%20fait,%20sans%20payer%20un%20bras.&summary=&source="><i class="fa fa-linkedin-square fa-2x icon-spacing" style="color:#F1F1EC;"></i></a>
</div>
</div>
</div>
</div>
</div>
<div id="thumbnails">
<div class="container">
<h2>Services Populaires</h2>
<h4 style="color:#333;">Choisissez parmi nos services les plus populaires.</h4>
<div class="row">
<?php
$return_arr = array();
$sel_sql = "SELECT
a_id AS annonce_id,
titre AS annonce_titre,
image AS annonce_image,
price AS annonce_prix,
postal_code AS annonce_code,
city AS annonce_ville,
numcommentaires AS num_commentaires,
SUM(communication)+SUM(service)+SUM(recommandation) AS total_rating
FROM annonces a
INNER JOIN users u
ON a.user_id = u.id
LEFT JOIN (SELECT
b.ad_id,
SUM(communication) AS communication,
SUM(service) AS service,
SUM(recommandation) AS recommandation,
COUNT(id) AS numcommentaires
FROM commentaires b
GROUP BY b.ad_id) b
ON a.a_id = b.ad_id
WHERE statut = 'published'
GROUP BY a_id
LIMIT 20
";
$conn->set_charset("utf8");
$run_sql = mysqli_query($conn, $sel_sql);
while ($rows = mysqli_fetch_assoc($run_sql)) {
$return_arr[] = $rows['annonce_code'];
echo '
<div class="col-xs-12 col-md-4">
<div class="thumbnail">
<a href="annonce.php?post_id='.$rows['annonce_id'].'">
<input class="tid" type="hidden" name="annonceid" value="'.$rows['annonce_id'].'">
<input class="tid" type="hidden" name="annoncecode" value="'.$rows['annonce_code'].'">
<p>'.$rows['annonce_titre'].'</p>
<img src="'.$rows['annonce_image'].'" alt="image">
<div class="caption">
<div>
<h3 style="color:#05C33C; font-weight:bold;">'.$rows['annonce_prix'].'$</h3>
<p style="color:#FE6E32;">'.($rows['num_commentaires'] == '' ? 'nouveau' : ''.substr($rows['total_rating'] / $rows['num_commentaires'] / 3,0,3).'/10 ('.$rows['num_commentaires'].')').'</p>
<p>'.$rows['annonce_ville'].'</p>
<p class="distance-'.$rows['annonce_id'].'"></p>
</div>
</div>
</a>
</div>
</div>
';
}
$postal = json_encode($return_arr);
?>
</div>
</div>
</div>
<div id="bottom-container" class="container profile text-center">
<div class="row">
<div class="col-md-4 col-xs-12">
<img src="images/icon_graph.png" class="img-circle">
<h3>Faites avancer les choses</h3>
<p>Des gens qui aiment se qu'ils font vous aident à tout faire à un prix imbattable.</p>
</div>
<div class="col-md-4 col-xs-12">
<img src="images/icon_money.png" class="img-circle">
<h3>Offrez vos services</h3>
<p>Qu'est-ce que vous faites de mieux? Créez votre annonce et commencez à vendre. C'est gratuit et ça prend seulement 5 minutes.</p>
</div>
<div class="col-md-4 col-xs-12">
<img src="images/icon_gears.png" class="img-circle">
<h3>Confiance et sécurité</h3>
<p>Votre sécurité est notre top priorité. Des transactions sécuritaires et notre équipe vous protègent en tout temps.</p>
</div>
</div>
</div>
<footer>
<?php include 'includes/footer.inc.php'; ?>
</footer>
<script src="js/jquery.js"></script>
<script src="bootstrap/js/bootstrap.min.js"></script>
<script src="scripts/index.js"></script>
<script src="https://maps.googleapis.com/maps/api/js?v=3.exp"></script>
<script type="text/javascript">
$.each($(".thumbnail"), function() {
$this = $(this);
id = $this.find('input[name="annonceid"]').val();
code = $this.find('input[name="annoncecode"]').val();
//$this.find('input[name="annonceid"]').val(text);
$(".distance-" + id).text(code + "/" + id);
var service = new google.maps.DistanceMatrixService(); //Creates a new request to the API
service.getDistanceMatrix({
origins: ['"' + code + '"'], //The seller side
destinations: ["J7Z 7B9"], //The buyer side
travelMode: google.maps.TravelMode.DRIVING,//Can have multiple travel mode like driving, cycling and walking
}, callback);
function callback(response, status) { //We get a document in JSON format
if (status == google.maps.DistanceMatrixStatus.OK) {
var origins = response.originAddresses;
var destinations = response.destinationAddresses;
for (var i = 0; i < origins.length; i++) {
var results = response.rows[i].elements;
for (var j = 0; j < results.length; j++) {
var element = results[j];
var distance = element.distance.text; //The distance between the origin and a destination
var duration = element.duration.text; //The time it takes to get there
var from = origins[i];
var to = destinations[j];
console.log(element);
console.log(distance);
console.log(duration);
console.log(from);
console.log(to);
}
}
}
}
});
</script>
</body>
</html>
根據規范,HTML 中的 id 屬性必須是唯一的。 因此,當您操作#distance
時,jQuery 將假定只有一個元素。 這會留下您看到的行為,其中只有第一個元素具有所需的結果。
要解決這個問題,您可以簡單地將行 id 作為 id 的一部分,如下所示: #distance-1
、 #distance-2
等。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.