Referring to my previously answered question (View Here)
I've created a new JSON variable in PHP and passed it to a JavaScript variable. I have implemented a feature that displays the last read post by the user like this:
[ //defined by var = book | removed URL for privacy reasons.
{
"id": 39,
"title": "My Pet",
"url": "https:///novel/my-pet/",
"chapter": {
"id": 1192,
"title": "35",
"url": "https:///my-pet-35/"
}
},
{
"id": 37,
"title": "Nobunaga’s Imouto",
"url": "https:///novel/nobunagas-imouto/",
"chapter": {
"id": 1449,
"title": "2",
"url": "https:///nobunaga-imouto-2/"
}
},
{
"id": 2,
"title": "Duke's Daughter",
"url": "https:///novel/dukes-daughter/",
"chapter": {
"id": 1398,
"title": "99",
"url": "https:///dukes-daughter-99/"
}
}
]
The first JSON retrieves data from cookies, enabling users to track their last read post. The second JSON variable displays the newest posts from each category.
[ //defined by var = newest
{
"id": 39,
"title": "My Pet Chapter 35",
"url": "https:///my-pet-35/",
},
{
"id": 37,
"title": "Nobunaga’s Imouto Chapter 4",
"url": "https:///nobunaga-imouto-4/",
},
{
"id": 2,
"title": "Duke's Daughter Chapter 106",
"url": "https:///dukes-daughter-106/",
}
]
To display them, I use a for loop:
$bookcontainer = $('#release_history');
for (var i in books) {
var book = books[i];
var html = '<div class="row"><div class="book_history">';
html += '<a href="'+book.url+'">'+book.title+'</a></div>';
// Displaying the latest news
html += '<div class="newest_history">';
for (var j in newest) { // displaying the newest post of a category
var news = newest[j];
html += '<a href="'+news.url+'">»Chapter '+news.title+'</a></div>';
}
html += '</div></div></div>';
$bookcontainer.append(html);
}
However, the output appears messed up: https://i.sstatic.net/nBeU1.png
This led me to think about adding an if conditional statement when the IDs match.
for (var j in newest) {
var news = newest[j];
if (news.id == book.id){
html += '<a href="'+news.url+'">»Chapter '+news.title+'</a></div>';}
}
Unfortunately, the loop terminates after displaying the initial result. Is there a solution to ensure they are all displayed separately? I aim to show the latest chapters/posts from various categories so users can stay updated on their last read book.