I keep getting the following error below. How can I correct this error?
Warning: Invalid argument supplied for foreach() on line 25.
Line 25 is.
foreach ($parent_comment_id as $id => $comment) {
Here is the PHP & MySQL code.
function make_comments($parent_comment_id = 0, $comment_id = 0) {
global $user_id;
global $comment_order;
foreach ($parent_comment_id as $id => $comment) {
if($comment['user_id'] == $user_id && $comment['parent_comment_id'] == 0){
$comment_id = $comment['comment_id'];
echo '<div>' . $comment['comment'] . '</div>';
if($comment_id == $comment['parent_comment_id']){
if($comment['user_id'] == $user_id && $comment['parent_comment_id'] >= 1) {
$comment_id = $comment['comment_id'];
echo '<div>' . $comment['comment'] . '</div>';
} else if($comment['parent_comment_id'] >= 1) {
$comment_id = $comment['comment_id'];
echo '<div>' . $comment['comment'] . '</div>';
}
}
} else if($comment['user_id'] != $user_id && $comment['parent_comment_id'] == 0) {
$comment_id = $comment['comment_id'];
echo '<div>' . $comment['comment'] . '</div>';
if($comment_id == $comment['parent_comment_id']){
if($comment['user_id'] == $user_id && $comment['parent_comment_id'] >= 1) {
$comment_id = $comment['comment_id'];
echo '<div>' . $comment['comment'] . '</div>';
} else if($comment['parent_comment_id'] >= 1) {
$comment_id = $comment['comment_id'];
echo '<div>' .开发者_运维问答 $comment['comment'] . '</div>';
}
}
}
make_comments($comment_order[$id]);
}
}
$dbc = mysqli_query($mysqli, "SELECT articles_comments.comment_id, articles_comments.parent_comment_id, articles_comments.comment, articles_comments.user_id FROM articles_comments LEFT JOIN users ON articles_comments.user_id = users.user_id WHERE article_id = '" . $article_id . "' ORDER BY articles_comments.parent_comment_id ASC");
if (!$dbc) {
print mysqli_error();
}
$comment_order = array();
while (list($comment_id, $parent_comment_id, $comment_text, $comment_user, $comment_id) = mysqli_fetch_array($dbc)) {
$comment_order[$parent_comment_id][$comment_id] = array('parent_comment_id' => $parent_comment_id, 'comment_id' => $comment_id, 'comment' => $comment_text, 'user_id' => $comment_user, 'comment_id' => $comment_id);
}
make_comments($comment_order[0], $comment_id);
In your foreach
statement, $parent_comment_id
must be an array expression. It's probably not when the execution reach that point, hence you are getting that runtime error.
Check the code where you call your function.
From what I can see, $parent_comment_id
is a variable containing a number, and you're trying to iterate over it using a foreach
statement.
$parent_comment_id
must be an array. Currently it's a value inside an array.
I've never used MySql, but I've used foreach with arrays a lot. I don't know exactly what the variable $parent_comment_id looks like, but if it's a string perhaps you can use the function explode to change it to an array.
Your code is not good object oriented.
Besides you mess different types in single var. You should have this definition of function:
function make_comments(array $parent_comment_id, $comment_id=0) {
If you have array element ensure it is array. Do not mess integer and array in same variable!
This should be a simple fix to your code. It checks is $parent_comment_id is an array before executing the foreach loop.
if(is_array($parent_comment_id))
foreach ($parent_comment_id as $id => $comment) {
//your for each code goes here
}
You can even add an else after the foreach.
精彩评论