1

I am currently creating a function that accepts a users id and based on that id it should return all values from the posts database that contains the users id. I have a seperate php file that I saved the function in since I want to use this on many pages. In the functions.php file I have:

class getposts
{
    public function getpostcontent($userid){
    include('db-conx.php');//Connects to Db
    $getval = "SELECT `content`,`date` FROM posts WHERE userid = ?";
    $stmt = $conn->stmt_init();
    if ($stmt->prepare($getval))
    {
        $userid = $_SESSION['userid'];
        $stmt->bind_param("s", $userid);
        $stmt->execute();
        $stmt->bind_result($content, $date);
        while ($stmt->fetch()) {
            $displayname = "Tom";
            $array = [
            "content" => "$content",
            "date" => "$date",
            "displayname" => "$displayname",
            ];
            return $array;
        }
    }
}

and call it using in Posts.php:

$posts = new getposts();
echo $posts ->getpostcontent($userid);

The problem is that the user has multiple rows in the posts database and the code only runs once. How would I go about looping it to show values from each row upon calling it? I may be overthinking it and searched all around but can't seem to get it to work.

1

1 Answer 1

1

You can insert a new record to the array with each iteration - then return the whole array:

    while ($stmt->fetch()) {
        $displayname = "Tom";
        $array[] = array(
        "content" => "$content",
        "date" => "$date",
        "displayname" => "$displayname"
        );

    }
    return $array;

In your code, the array was being over-written each time and only returning the last row from the database. Now it will simply add to the array inside the while loop and then after it is done return it all.

Edit: I normally use a $result to pull the data back from the database - not sure if your approach will work - but if it doesn't look into that :)

Edit 2:

In the code, you now have an array of arrays. You can call out each element like this:

echo $array[0]['content'];

This will echo out the contents of content from the first record, $array[1]['content'] has the second row from the database and so on.

Edit 3:

You are returning an array - not an object, so you can do it like this:

$posts = new getposts();
// You make an object of the class.

$returned=$posts->getpostcontent($userid);
// Now you run the query against the userID and return the array into $returned

foeach($returned as $val)
{
    print_r($val);
    // This is showing you the structure of each array inside the main array.

    // Or you can access each bit as needed:

    echo 'The date is '.$val['date'].'<br>';
    echo 'The content is is '.$val['content'].'<br>';
}
Sign up to request clarification or add additional context in comments.

8 Comments

Thanks for the fast reply. I tried that and I can confirm that it does loop now. However I do get an 'Array to string conversion' error when calling it. Hopefully it shouldn't be too hard to fix and thanks I'll look into storing in the $result variable. Thanks again :)
Thanks how could I get it to loop a value for each array from the class on the functions.php file
Not working :(. Illegal string offset error. Probably something in my code I need to look into
I did have a typo in there, though I have fixed it now.
Still no luck unfortunately. I'll continue to look into this. Based on the error I'm guessing that string is being returned instead of array.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.