I am trying to fetch a users user_id and name from a table via running a mysql query.
There are 12 users/rows of data in my table in total and i only want to display 6 results at any one given time.
The aim is to display the users profile image by combining the directory where the image is stored with the users unique user_id:
data/profiles/users/profile_img/{users_id}/main.jpg
Then the images rotate (randomly) every 5 seconds using JQUERY to update the img src attribute.
As well as this, I am setting a href link with the users user_id so that the user can click onto the image and be taken to that users profile.
This all works fine. However, I also want to display the users name in the html <h3>
attribute for each element.
I seem to be having difficulty achieving this. Instead of getting one users name on each element i am getting all the users name on each element.
Please can someone show me where I am going wrong?
Code:
<?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>