Invalid argument supplied for foreach on line 25 Error in my php and mysql

0 votes

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);
Aug 4, 2020 in PHP by viven
• 120 points

recategorized Aug 4, 2020 by Niroj 566 views

1 answer to this question.

0 votes

Hello @viven ,

This should be a simple fix to your code. It checks  $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.

Try this

Hope it works!!

Thank you!!

answered Aug 4, 2020 by Niroj
• 82,680 points
Thank you man..your tricks works

Hey, 

Glad to help you, please upvote the answer if you find it helpful. You can come to the community regarding any tech queries to gain points and for further contributions. You may ask questions, answer, upvote, and downvote an answer. Each of these would fetch you points and you could be among the top contributors and win exciting merchandise from Edureka.

Cheers!

Related Questions In PHP

0 votes
1 answer
0 votes
1 answer

Error:Undefined index: barangay in C:\xampp\htdocs\TextBlast\homepage\insert.php on line 22

Hello @ Regina, First checks whether a variable is ...READ MORE

answered Aug 14, 2020 in PHP by Niroj
• 82,680 points
1,873 views
0 votes
1 answer
0 votes
1 answer

Error:Cannot modify header information - headers already sent by (output started at /some/file.php:12) in /some/file.php on line 23

Hello @kartik, This error message gets triggered when anything is ...READ MORE

answered Sep 16, 2020 in PHP by Niroj
• 82,680 points
1,193 views
0 votes
2 answers

Define a SQL query? What is the difference between SELECT and UPDATE Query? How do you use SQL in SAS?

HI.. SQL is Structured Query Language, which is ...READ MORE

answered Aug 8, 2020 in PHP by anonymous
2,246 views
0 votes
1 answer

Why is not preferable to use mysql_* functions in PHP?

The reasons are as follows: The MySQL extension: Does ...READ MORE

answered Sep 7, 2018 in Database by DataKing99
• 8,240 points
170 views
0 votes
2 answers
0 votes
1 answer
0 votes
1 answer

Error: Maximum execution time of 60 seconds exceeded in C:\xampp\phpmyadmin\libraries\dbi\mysql.dbi.lib.php on line 140

Hello @kartik, Go to: xampp\phpMyAdmin\libraries\config.default.php Look for : $cfg['ExecTimeLimit'] = 600; You ...READ MORE

answered Sep 1, 2020 in PHP by Niroj
• 82,680 points
1,394 views