Reputation: 1
Hi i am working on a code to show users of my website posts from the users they are following and the current logged in user's posts too but i get an error like this Warning: implode(): Invalid arguments passed in /Applications/XAMPP/xamppfiles/htdocs/f....e/functions.php on line 104
//on the home page to display users posts
<h4>RECENT ACTIVITY</h4>
<div class="panel panel-default">
<div class="panel-body">
<?php
$users = show_users($_SESSION['login']);
if (count($users)){
$myusers = array_keys($users);
}else{
$myusers = array();
}
$myusers[] = $_SESSION['login'];
$posts = show_posts($myusers,5);
if (count($posts)){
foreach ($posts as $key => $list){
echo $list['post_date'] ;
echo $list['user_id'];
echo $list['post_body'] ;
}
}else{
echo"<p>","<b>","You haven't posted anything yet!","</b>","</p>";
}
?>
</div>
</div>
//the function
<?php
function show_posts($user_id,$limit=0){
global $conn,$user_id;
$posts = array();
$user_string = implode(',', $user_id);
$extra = " and user_id in ($user_string)";
if ($limit > 0){
$extra = "limit $limit";
}else{
$extra = '';
}
$sql = "select user_id,post_body, post_date from users_posts
where user_id in ($user_string)
order by post_date desc $extra";
echo $sql;
$result = mysqli_query($conn,$sql);
while($data = mysqli_fetch_object($result)){
$posts[] = array( 'post_date' => $data->post_date,
'user_id' => $data->user_id,
'post_body' => $data->post_body
);
}
return $posts;
}
?>
Upvotes: 0
Views: 1629
Reputation: 1438
Change the below code
$myusers[] = $_SESSION['login'];
To
$myusers = $_SESSION['login'];
Upvotes: 0
Reputation: 62
Firstly you have to know about implode() function. Mainly The implode() function returns a string from the elements of an array.
So you must include an array as the second parameter and the separator as first parameter.
implode(separator,array)
Best solution would be you need to check the $user_id variable. If $user_id is an array the the implode function should work without error.
Upvotes: 0
Reputation: 824
Change it to
$users = show_users($_SESSION['login']);
$myusers = array();
if (count($users)) $myusers = array_keys($users);
You need to declare $myusers
before the if
statement, otherwise it only gets set in the scope of the if
and else
statements and is lost outside of them.
Upvotes: 2