Alright I got the comment_post_ID sorted out, but I still can't get the comment_content to insert. If I set the value of the comment_content key in the insert statement to a number instead of $_POST[comment], it inserts that value. I just can't figure out what is stopping the comments insert that isn't stopping the author insert.
For number 1 and 2, I think the form creation is done inside the loop. I'm confused as to how the submission part can be moved outside of the loop. I tried moving around the if statement containing the INSERT INTO statement but I don't think I found the right spot for it because I still get duplicates of the comment in the database(sometimes 32, sometimes 8) Here is my current index.php:
<?php get_header ();?>
<div id="box">
<?php
$qry = "SELECT u.id, user_nicename, post_date, post_content, post_title, post_type, post_excerpt, comment_content, comment_author, comment_approved, comment_count, comment_post_ID, comment_ID, wp_posts.ID as post_id
FROM wp_posts
INNER JOIN wp_users AS u ON post_author = u.id
LEFT JOIN wp_comments ON wp_posts.ID = comment_post_ID
WHERE post_type='post'
ORDER BY user_nicename ASC, post_date DESC";
$res = mysql_query($qry);
$author = array();
if ($res) {
while ($row = mysql_fetch_assoc($res)) {
$author[$row['user_nicename']][] = $row;
}
foreach($author as $k => $v) {
$orderbydate[$v[0]['post_date']][$k] = $v;
}
krsort($orderbydate);
foreach ($orderbydate as $k => $v) {
ksort($orderbydate[$k], SORT_STRING);
}
}
$divIds = array('one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine');
$i = 0;
foreach ($orderbydate as $date => $authors) {
foreach ($authors as $author=> $posts) {
echo '<div id="'.$divIds[$i++].'">';
if($author=="tony") {
echo '<img src="http://www.hippievstony.com/wp-content/uploads/2009/12/tonyname.jpg">';
}
elseif($author=="hippie") {
echo '<img src="http://www.hippievstony.com/wp-content/uploads/2009/12/hippiename.jpg">';
}
elseif($author=="eriku") {
echo '<img src="http://www.hippievstony.com/wp-content/uploads/2009/12/erikuname.jpg">';
}
elseif($author=="ben") {
echo '<img src="http://www.hippievstony.com/wp-content/uploads/2009/12/benname.jpg">';
}
elseif($author=="hingyi") {
echo '<img src="http://www.hippievstony.com/wp-content/uploads/2009/12/hingyiname.jpg">';
}
elseif($author=="chris") {
echo '<img src="http://www.hippievstony.com/wp-content/uploads/chrisname.jpg">';
}
elseif($author=="gabe") {
echo '<img src="http://www.hippievstony.com/wp-content/uploads/gabename.jpg">';
}
elseif($author=="bender") {
echo '<img src="http://www.hippievstony.com/wp-content/uploads/bendername.jpg">';
}
elseif($author=="jake") {
echo '<img src="http://www.hippievstony.com/wp-content/uploads/jakename.jpg">';
}
$lastPost = null;
foreach($posts as $post) {
if($lastPost != $post['post_id']) {
echo '<h2>' . $post['post_title'] . '</h2><small>' . $post['post_date'] . '</small>';
echo '<p>' . $post['post_content'] . '</p>';
echo '<p>' . $post['post_excerpt'] . '</p>';
$j=1;
}
$lastPost = $post['post_id'];
if ($post['comment_count']>=1) {
echo '<p>' . $post['comment_content'] . '</p>';
echo '<p><small>-' . $post['comment_author'] . '</small></p>';
}
if($post['comment_count']<=$j) {
$post['comment_approved']=0;
$post_id = $post['post_id'];
echo '<form method="POST" action="'.$_SERVER['PHP_SELF'].'"/>';
echo 'Comment <input type="text" name="comment" id="comment"/>';
if ($user_ID) {
$_POST[author]=$user_login;
$post['comment_approved']=1;
echo '<input type="text" name="author" style="display:none" value="$user_login"/>';
echo " ready to comment as $user_login";
}
else echo 'Name <input type="text" name="author"/>';
echo '<p><input name="submit_comment" type="submit" id="submit" value="fly like the eagle" tabindex="5" /></p>';
echo '<p><input type="hidden" name="number_of_the_post" value="'.$post_id.'" /></p>';
if (empty($_POST['submit_comment']) || !(($post_id = (int) $_POST['submit_comment']) > 0)) {
# bad comment_ID;
}
if (isset($_POST['submit_comment'])) {
$_POST[author]=mysql_real_escape_string($_POST[author]);
$user_login=mysql_real_escape_string($user_login);
$_POST[comment]=mysql_real_escape_string($POST[comment]);
$post_id=mysql_real_escape_string($post_id);
mysql_query ("INSERT INTO wp_comments(comment_content, comment_author, comment_post_ID) VALUES ('$_POST[comment]', '$_POST[author]', '$_POST[number_of_the_post]')");
}
echo '</form>';
}
$j++;
}
echo '</div>';
}
}
get_sidebar ();
get_footer();
?>
and here is just the form part:
code=php {
$post['comment_approved']=0;
$post_id = $post['post_id'];
echo '<form method="POST" action="'.$_SERVER['PHP_SELF'].'"/>';
echo 'Comment <input type="text" name="comment" id="comment"/>';
if ($user_ID) {
$_POST[author]=$user_login;
$post['comment_approved']=1;
echo '<input type="text" name="author" style="display:none" value="$user_login"/>';
echo " ready to comment as $user_login";
}
else echo 'Name <input type="text" name="author"/>';
echo '<p><input name="submit_comment" type="submit" id="submit" value="fly like the eagle" tabindex="5" /></p>';
echo '<p><input type="hidden" name="number_of_the_post" value="'.$post_id.'" /></p>';
if (empty($_POST['submit_comment']) || !(($post_id = (int) $_POST['submit_comment']) > 0)) {
# bad comment_ID;
}
if (isset($_POST['submit_comment'])) {
$_POST[author]=mysql_real_escape_string($_POST[author]);
$user_login=mysql_real_escape_string($user_login);
$_POST[comment]=mysql_real_escape_string($POST[comment]);
$post_id=mysql_real_escape_string($post_id);
mysql_query ("INSERT INTO wp_comments(comment_content, comment_author, comment_post_ID) VALUES ('$_POST[comment]', '$_POST[author]', '$_POST[number_of_the_post]')");
}
echo '</form>';
}
$j++;
}[/code]