I'm having a problem when I'm retrieving conversations from my MySQL database. When I run the query on phpMyAdmin, it gives me the order;
- 41 (2021-07-24)
- 371 (2021-07-20)
- 361 (2021-07-19)
- 351 (2021-07-17)
- 21 (2021-07-11)
But when I run my code in PHP, I get this order;
- 371 (2021-07-20)
- 361 (2021-07-19)
- 351 (2021-07-17)
- 41 (2021-07-24)
- 21 (2021-07-11)
Heres the code in PHP:
<?php
include 'config.php';
$requestId = $conn->real_escape_string($_GET['requestid']);
$requestIdPost = $conn->real_escape_string($_POST['requestid']);
if($requestId == 1){ //Find all unique chat_id's
$id = $conn->real_escape_string($_GET['userid']);
$chatid = $conn->real_escape_string($_GET['chatid']);
$sender = $conn->real_escape_string($_GET['sender']);
$sql = "SELECT DISTINCT chat_id FROM chatdb WHERE (account_id='$id' OR counterpart='$id')
AND active=1 ORDER BY chat_time DESC";
$result = mysqli_query($conn,$sql);
$rows = mysqli_num_rows($result);
if(mysqli_num_rows($result) > 1){
for($i=0; $i < $rows; $i++){
$rs=mysqli_fetch_array($result);
$chatid = $rs['chat_id'];
$chatid = utf8_encode($chatid);
$points[$i][0]=($chatid);
}
echo (json_encode($points));
}
else if(mysqli_num_rows($result) == 1){
$rs=mysqli_fetch_array($result);
$chatid = $rs['chat_id'];
$chatid = utf8_encode($chatid);
echo json_encode($chatid);
}
else{
echo (0);
}
}
?>
Does anyone know why the array created by my For-loop messes up the order of the chat-ids? Keep in mind that the array (if #rows > 1) is echoed back to an ajax request in javascript:
function populateChatList(){
var loginId = getCookie("a_user");
var newArrayLoginId = loginId.split(',');
var accountid = newArrayLoginId[0];
$.ajax(
{
url: './PHP/inbox.php',
dataType: 'text',
method: 'GET',
data: {
requestid: 1,
userid: accountid,
},
success: function(response){
var response = JSON.parse(response);
console.log(response)
if(Array.isArray(response) == false){ //single chatbox
sessionStorage.setItem('c_id', `${response}`);
$.ajax(
{
url: './PHP/inbox.php',
dataType: 'text',
method: 'GET',
data: {
requestid: 2,
chatid: sessionStorage.getItem('c_id'),
},
success: function(response){
var query = JSON.parse(response);
if(query !== 0){
conversationPopulate(query);
}
else{ //no messages, display no-message-svg
document.getElementById('no-message-svg-wrapper').style.display = "flex";
}
},
}
);
}
else if(response.length > 1){ //multiple chatbox
response.forEach(element => {
$.ajax(
{
url: './PHP/inbox.php',
dataType: 'text',
method: 'GET',
data: {
requestid: 2,
chatid: element[0],
},
success: function(response){
var query = JSON.parse(response);
conversationPopulate(query, element);
},
}
);
});
}
else if(response == 0){
document.getElementById('noconversations').style.display = "block";
document.getElementById('noconversations').innerText = "Inga Konversationer"
}
},
}
);
};
$result = mysqli_query($conn,$sql);andprint_r(mysqli_fetch_all($result, MYSQLI_ASSOC));is order incorrect? The$result = mysqli_query($conn,$sql); $rows = mysqli_num_rows($result); if(mysqli_num_rows($result) > 1){ for($i=0; $i < $rows; $i++){ $rs=mysqli_fetch_array($result);seems like it could be simplified with awhileloop. Theutf8_encodeI also don't think is needed.