【发布时间】:2019-03-20 17:16:30
【问题描述】:
我正在尝试通过运行 mysql 查询从表中获取用户 user_id 和名称。
我的表中总共有 12 个用户/行数据,我只想在任何给定时间显示 6 个结果。
目的是通过将存储图像的目录与用户唯一的user_id相结合来显示用户的个人资料图像:
data/profiles/users/profile_img/{users_id}/main.jpg
然后图像每 5 秒旋转一次(随机),使用 JQUERY 更新 img src 属性。
除此之外,我还设置了一个带有用户 user_id 的 href 链接,以便用户可以单击图像并转到该用户的个人资料。
这一切都很好。但是,我还想在每个元素的 html <h3> 属性中显示用户名。
我似乎很难做到这一点。我不是在每个元素上获取一个用户名,而是在每个元素上获取所有用户名。
请谁能告诉我哪里出错了?
代码:
<?php $result = $conn->query("SELECT * FROM user_data, user_locations WHERE user_data.user_id = user_locations.user_id AND user_data.advert_type = 'Deluxe' AND user_data.advert_status = 'Active' ");
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
$p_id = $row['user_id'];
$name = $row['first_name'] . ' ' . $row['last_name'];
$new_array[] = $p_id;
$new_array2[] = $name;
}
}
echo '<script>';
echo 'var imagesArray = ' . json_encode($new_array) . ';';
echo 'var imagesArray2 = ' . json_encode($new_array2) . ';';
echo '</script>';
?>
<script>
//Good shuffle algorithm: https://stackoverflow.com/questions/2450954/how-to-randomize-shuffle-a-javascript-array
function shuffle(array) {
var currentIndex = array.length, temporaryValue, randomIndex;
// While there remain elements to shuffle...
while (0 !== currentIndex) {
// Pick a remaining element...
randomIndex = Math.floor(Math.random() * currentIndex);
currentIndex -= 1;
// And swap it with the current element.
temporaryValue = array[currentIndex];
array[currentIndex] = array[randomIndex];
array[randomIndex] = temporaryValue;
}
return array;
}
function changeComponent() {
// store image query in a variable
var $allImages = $(".deluxe_ad_img");
// take X random IDs by shuffling the list and taking the first X
// (slice total dynamically, in case you ever need more or less than exactly 6)
var randomIDs = shuffle(imagesArray).slice(0, $allImages.length);
var randomIDs2 = shuffle(imagesArray2).slice(0, $allImages.length);
// iterate over each image, using the index of the iteration.
$allImages.each(function (idx) {
$(this).attr('src', 'data/profiles/users/profile_img/' + randomIDs[idx] + '/main.jpg');
$(this).parent(".deluxe_component_link").attr('href', 'profile.php?p_id=' + randomIDs[idx]);
$("h3.deluxe_title").text(randomIDs2);
});
}
$(document).ready(function () {
changeComponent();
setInterval(function() {
changeComponent();
}, 10000);
})
</script>
<div class="deluxe_listing_container">
<?php
//Echo out results
echo '<div class="deluxe_component"><a href="" class="deluxe_component_link">';
echo '<img class="deluxe_ad_img" src="" height="auto" width="auto" />';
echo '<h3 class="deluxe_title"></h3><p class="deluxe_subtitle"></p></a></div>';
echo '<div class="deluxe_component"><a href="" class="deluxe_component_link">';
echo '<img class="deluxe_ad_img" src="" height="auto" width="auto" />';
echo '<h3 class="deluxe_title"></h3><p class="deluxe_subtitle"></p></a></div>';
echo '<div class="deluxe_component"><a href="" class="deluxe_component_link">';
echo '<img class="deluxe_ad_img" src="" height="auto" width="auto" />';
echo '<h3 class="deluxe_title"></h3><p class="deluxe_subtitle"></p></a></div>';
echo '<div class="deluxe_component"><a href="" class="deluxe_component_link">';
echo '<img class="deluxe_ad_img" src="" height="auto" width="auto" />';
echo '<h3 class="deluxe_title"></h3><p class="deluxe_subtitle"></p></a></div>';
echo '<div class="deluxe_component"><a href="" class="deluxe_component_link">';
echo '<img class="deluxe_ad_img" src="" height="auto" width="auto" />';
echo '<h3 class="deluxe_title"></h3><p class="deluxe_subtitle"></p></a></div>';
echo '<div class="deluxe_component"><a href="" class="deluxe_component_link">';
echo '<img class="deluxe_ad_img" src="" height="auto" width="auto" />';
echo '<h3 class="deluxe_title"></h3><p class="deluxe_subtitle"></p></a></div>';
?>
</div>
【问题讨论】:
标签: javascript jquery html mysql