$.ajax({
url: "notifications.php",
dataType: "json",
success: function(responseJSON) {
if (responseJSON.length > 0) {
document.title = document.title.replace(/^(?:\(\d+\))?/, "(" + responseJSON.length + ") ")
for (var i=0; i<10; i++) {
console.log(responseJSON[i].date_notify')
}
}
}, error : function(x) {
console.log(x.responseText)
}
})
In chrome I have this error:
Uncaught TypeError: Cannot read property ‘date_notify’ of undefined
And I figured out at this part for (var i=0; i<10; i++) should be replace to for (var i=0; i<responseJSON.length; i++) the problem is I only want to have 10 results… in my sql part I didn’t LIMIT the query. here’s my query
SELECT users.lastname, users.firstname, users.screenname, notifications.notify_id,
notifications.tagged_by, notifications.user_id, notifications.post_id,
notifications.type, notifications.action, notifications.date_notify,
notifications.notify_id
FROM website.users users INNER JOIN website.notifications notifications
ON (users.user_id = notifications.user_id)
WHERE notifications.user_id = ? and notifications.action = ?
ORDER BY notifications.notify_id DESC
//LIMIT 10
Is there any possible way to change this one?
Use an
ANDed conditional in your loop:This way the loop cuts out when
ieither exceeds the number of entries or reaches 10, whichever happens first. If the number of entries is less than 10, the first condition becomes false first, and if there are more than 10 records, the second condition becomes false first.