I'm currently working on a blog type page with a comment area, this is a personal learning adventure and I would appreciate detailed explanations to solidify the learning. I currently have everything structured in an MVC pattern for PHP using CodeIgniter, and my JavaScript code is also formatted in a similar fashion.
EDIT: I've added the output of the array from the build_array() function from PHP. I feel that is the root cause of my issue, if I could rework the array to not feed each comment the entire blog data it would allow me to achieve what I desire.
I appreciate all of the comments and I may not know all that you ask so please be patient with me, I'm slow. (:
I can't figure out how to get the data to be displayed properly, currently it outputs:
Blog Post Two
Blog Post Two Comment
Blog Post Two
Blog Post Two Comment
It is ignoring the first blog post and I need it to not post duplicates and post each blog post.
The code for the api/get_blog():
$query = $this->db->query(
"SELECT blog.blog_id, blog.dateposted, blog.content, blog.title, blog.published,
user.login, user.img,
bc.username, bc.comment
FROM blog
JOIN user
ON blog.user_id = user.user_id
LEFT JOIN blog_comments bc
ON blog.blog_id = bc.blog_id
WHERE blog.published = 1
ORDER BY blog.blog_id DESC
");
$data = $query->result_array();
$result = $this->build_array($data);
Build array function:
foreach ($data as $row) {
if (!isset($outputArr[$row['blog_id']])) {
$outputArr[$row['blog_id']] = array();
}
$outputArr[$row['blog_id']][] = $row;
}
if(!empty($outputArr)) {
$result = $outputArr;
}
return $result;
Returns the following Array:
<pre>Array
(
[37] => Array
(
[0] => Array
(
[blog_id] => 37
[dateposted] => 2014-04-13
[content] => <p>blog article two</p>
[title] => blog post two
[published] => 1
[login] => admin
[img] => public/img/blog-image.png
[username] => Skewled
[comment] => blog two comment
)
[1] => Array
(
[blog_id] => 37
[dateposted] => 2014-04-13
[content] => <p>blog article two</p>
[title] => blog post two
[published] => 1
[login] => admin
[img] => public/img/blog-image.png
[username] => Skewled
[comment] => blog two comment
)
)
[36] => Array
(
[0] => Array
(
[blog_id] => 36
[dateposted] => 2014-04-13
[content] => <p>blog article one</p>
[title] => blog post one
[published] => 1
[login] => admin
[img] => public/img/blog-image.png
[username] => Gaddam
[comment] => blog post one comment
)
[1] => Array
(
[blog_id] => 36
[dateposted] => 2014-04-13
[content] => <p>blog article one</p>
[title] => blog post one
[published] => 1
[login] => admin
[img] => public/img/blog-image.png
[username] => Gaddam
[comment] => blog post one comment
)
)
)
</pre>
Original Array returned from MySQL:
<pre>Array
(
[0] => Array
(
[blog_id] => 37
[dateposted] => 2014-04-13
[content] => <p>blog article two</p>
[title] => blog post two
[published] => 1
[login] => admin
[img] => public/img/blog-image.png
[username] => Skewled
[comment] => blog two comment
)
[1] => Array
(
[blog_id] => 37
[dateposted] => 2014-04-13
[content] => <p>blog article two</p>
[title] => blog post two
[published] => 1
[login] => admin
[img] => public/img/blog-image.png
[username] => Skewled
[comment] => blog two comment
)
[2] => Array
(
[blog_id] => 36
[dateposted] => 2014-04-13
[content] => <p>blog article one</p>
[title] => blog post one
[published] => 1
[login] => admin
[img] => public/img/blog-image.png
[username] => Gaddam
[comment] => blog post one comment
)
[3] => Array
(
[blog_id] => 36
[dateposted] => 2014-04-13
[content] => <p>blog article one</p>
[title] => blog post one
[published] => 1
[login] => admin
[img] => public/img/blog-image.png
[username] => Gaddam
[comment] => blog post one comment
)
)
</pre>
JSON String:
{"37":[{"blog_id":"37","dateposted":"2014-04-13","content":"<p>blog article two<\/p>","title":"blog post two","published":"1","login":"admin","img":"public\/img\/blog-image.png","username":"Tom","comment":"blog post 2 comment 1"},{"blog_id":"37","dateposted":"2014-04-13","content":"<p>blog article two<\/p>","title":"blog post two","published":"1","login":"admin","img":"public\/img\/blog-image.png","username":"Frank","comment":"blog post 2 comment 2"},{"blog_id":"37","dateposted":"2014-04-13","content":"<p>blog article two<\/p>","title":"blog post two","published":"1","login":"admin","img":"public\/img\/blog-image.png","username":"Joey","comment":"blog post 2 comment 3"}],"36":[{"blog_id":"36","dateposted":"2014-04-13","content":"<p>blog article one<\/p>","title":"blog post one","published":"1","login":"admin","img":"public\/img\/blog-image.png","username":"Ted","comment":"blog one comment number one"},{"blog_id":"36","dateposted":"2014-04-13","content":"<p>blog article one<\/p>","title":"blog post one","published":"1","login":"admin","img":"public\/img\/blog-image.png","username":"John","comment":"blog one comment two"},{"blog_id":"36","dateposted":"2014-04-13","content":"<p>blog article one<\/p>","title":"blog post one","published":"1","login":"admin","img":"public\/img\/blog-image.png","username":"Bill","comment":"blog one comment three"}]}
jQuery Function to get work with the JSON string:
var load_blog = function() {
$.getJSON('api/get_blog', function(data) {
$.each(data, function() { //Loop through each blog_id section
var output = '';
$.each(this, function(){ //Loop through each comment in this blog_id
output += Template.blog(this); //output the template
});
$("#list_blog").html(output);
});
});
};
Template for Blog View:
this.blog = function(obj) {
var output = '';
output += '<div class="blog-post" style="margin: 5px;">';
output += '<h2 class="blog-post-title">';
output += obj.title + '</h2>';
output += '<p class="blog-post-meta">';
output += '<img src="' + obj.img +'">' + ' ' + obj.dateposted + ' by ' + obj[i].login[i] + '</p>';
output += '<p>' + obj.content + '</p>';
output += '</div>';
output += '<span class="options">';
output += '<a class ="blog_update_display" data-id="' + obj.blog_id + '" href="#"><i class="glyphicon glyphicon-pencil"></i></a> Leave Comment ';
output += '<div class="blog_comment_container" id="blog_comment_container_'+ obj[i].blog_id[i] +'"></div>';
output += '<div class="hide" id="blog_comment_' + obj.blog_id + '">' + obj[i].comment[i] + '</div>';
output += '</span>';
output += '<hr>';
output += '<span><a class="comment_toggle" data-id="'+ obj.blog_id +'" id="blog_title_' + obj.blog_id + '" href="#">View Comments</a></span> ';
output += '<div class="hide" id="blog_comment_block_' + obj.blog_id + '">';
output += '<hr>';
if (obj.username) {
output += '<h5 class="users">' + obj.username + ' commented:</h5>';
output += '<p>' + obj.comment + '</p>';
} else {
output += '<p>Be the first to comment!</p>';
}
output += '</div>';
output += '<hr>';
output += '</div>';
return output;
};
alertstatements here to check that it is reading the data correctly?