Javascript 从MYSQL表中获取用户id,并使用Jquery设置HTML属性(img src、href link、h3标记)
我试图通过运行mysql查询从表中获取用户id和名称 我的表中总共有12个用户/行数据,我只想在任何给定时间显示6个结果 其目的是通过将存储图像的目录与用户唯一的用户id相结合来显示用户配置文件图像:Javascript 从MYSQL表中获取用户id,并使用Jquery设置HTML属性(img src、href link、h3标记),javascript,jquery,html,mysql,Javascript,Jquery,Html,Mysql,我试图通过运行mysql查询从表中获取用户id和名称 我的表中总共有12个用户/行数据,我只想在任何给定时间显示6个结果 其目的是通过将存储图像的目录与用户唯一的用户id相结合来显示用户配置文件图像: data/profiles/users/profile_img/{users_id}/main.jpg 然后使用JQUERY每隔5秒(随机)旋转一次图像,以更新img src属性 除此之外,我还设置了一个带有用户id的href链接,以便用户可以单击该图像并进入该用户配置文件 这一切都很好。但是,
data/profiles/users/profile_img/{users_id}/main.jpg
然后使用JQUERY每隔5秒(随机)旋转一次图像,以更新img src属性
除此之外,我还设置了一个带有用户id的href链接,以便用户可以单击该图像并进入该用户配置文件
这一切都很好。但是,我还希望在每个元素的html
属性中显示用户名
我似乎很难做到这一点。我不是在每个元素上获取一个用户名,而是在每个元素上获取所有用户名
有人能告诉我哪里出了问题吗
代码:
请尝试以下操作,并阅读所有注释,以便理解更改
<?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'];
// have 1 array, but each element is an array containing both name and id. this will keep the data together even when you shuffle later
$new_array[] = array('p_id' => $p_id, 'name' => $name);
}
}
echo '<script>';
echo 'var imagesArray = ' . json_encode($new_array) . ';';
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() {
// get all the component containers
var $allComponents = $(".deluxe_component");
// take X random people by shuffling the list and taking the first X
// (slice by .length dynamically, in case you ever need more or less than exactly 6)
var randomPeople = shuffle(imagesArray).slice(0, $allComponents.length);
// iterate over each image, using the index of the iteration.
$allComponents.each(function (idx) {
// find the image element in this component and add the src, using the 'p_id' field of this particular randomPeople[idx]
$(this).find('.deluxe_ad_img').attr('src', 'data/profiles/users/profile_img/' + randomPeople[idx]['p_id'] + '/main.jpg');
// find the link element in this component and add the href, using the 'p_id' field of this particular randomPeople[idx]
$(this).find(".deluxe_component_link").attr('href', 'profile.php?p_id=' + randomPeople[idx]['p_id']);
// find the h3 element in this component and add the name, using the 'name' field of this particular randomPeople[idx]
$(this).find("h3.deluxe_title").text(randomPeople[idx]['name']);
});
}
$(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>
<?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'];
// have 1 array, but each element is an array containing both name and id. this will keep the data together even when you shuffle later
$new_array[] = array('p_id' => $p_id, 'name' => $name);
}
}
echo '<script>';
echo 'var imagesArray = ' . json_encode($new_array) . ';';
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() {
// get all the component containers
var $allComponents = $(".deluxe_component");
// take X random people by shuffling the list and taking the first X
// (slice by .length dynamically, in case you ever need more or less than exactly 6)
var randomPeople = shuffle(imagesArray).slice(0, $allComponents.length);
// iterate over each image, using the index of the iteration.
$allComponents.each(function (idx) {
// find the image element in this component and add the src, using the 'p_id' field of this particular randomPeople[idx]
$(this).find('.deluxe_ad_img').attr('src', 'data/profiles/users/profile_img/' + randomPeople[idx]['p_id'] + '/main.jpg');
// find the link element in this component and add the href, using the 'p_id' field of this particular randomPeople[idx]
$(this).find(".deluxe_component_link").attr('href', 'profile.php?p_id=' + randomPeople[idx]['p_id']);
// find the h3 element in this component and add the name, using the 'name' field of this particular randomPeople[idx]
$(this).find("h3.deluxe_title").text(randomPeople[idx]['name']);
});
}
$(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>