-1

I hope, I formulated my title right. I want to understood how to merge to 2 arrays into 1 array.

Simple example of what I want to do:

I have 2 database tables, like blog_posts and users.

I want to select data from blog_posts and foreach post I want to select information for "addedby" like username, user_image, etc.

With echo I managed to just show values like <?php echo $user['username']; ?> and <?php echo $blog['title']; ?>

I want to return those values, like in mvc pattern. How I was reading from different topics, I have to store data into arrays, to return them, with one array there's no problem, but I don't understand how can I return them both.

public function getBlogPostList(){
    try{

        $sortby = "SELECT * FROM blog_posts";

        $stmt = $this->connCommunity->prepare("$sortby");
        $stmt->execute();
        $result = $stmt->fetchAll();

        foreach($result as $post){

            $addedby = $post['addedby'];
            $stmt2 = $this->connUsers->prepare("SELECT * FROM users WHERE id=('$addedby')");
            $stmt2->execute();
            $result2 = $stmt2->fetchAll();

            $blog_post = array();
            foreach($result as $post) {
                $blog_post[] = $post;
            }

            foreach($result2 as $user) {
                array_merge($blog_post, $user);
            }
        }

        return $blog_post;

    }
    catch(PDOException $e) {
        echo $e->getMessage();
    }
}

And displaying arrays like:

<?php

    foreach ($BlogModel as $BlogModel)
    {
        echo '<tr><td><a href="index.php?id='.$BlogModel['id'].'">'.$BlogModel['id'].'</a></td><td>'.$BlogModel['title'].'</td><td>'.$BlogModel['username'].'</td></tr>';
    }


?>
5
  • 2
    You can select all that information in one query if you do a little research on the sql JOIN syntax. Commented Oct 9, 2018 at 15:41
  • 1
    This foreach($result as $post) { $blog_post[] = $post; } is inside another foreach which is also processing $result the second will consume all the array so the outer loop will run only once Commented Oct 9, 2018 at 15:42
  • Also variables don't need to be quoted, $this->connCommunity->prepare($sortby) and to use prepared statements effectively you should parameterize them (if you were to use this approach, but joins would be better anyway). Commented Oct 9, 2018 at 15:42
  • @RiggsFolly I think sql JOIN won't work, because data are located into 2 different databases, so I connect them seperently with connUsers and connComunity like here ($stmt2 = $this->connUsers) Commented Oct 9, 2018 at 15:49
  • 1
    Not so, look at this answer stackoverflow.com/questions/8895892/… Commented Oct 9, 2018 at 15:51

1 Answer 1

0

First of all start with a simpler query which does all the work.

This query example assumes the column with the userId in the blog_posts table is user_id, replace this one with the correct column. Replace my example databasenames communitydb and usersdb with your correct database names.

$sortby = "SELECT * FROM communitydb.blog_posts p LEFT JOIN usersdb.users u ON u.id = p.user_id";

After this you only need to return the first $stmt->fetchAll(); and you are done. So a lot of code in your function getBlogPostList() is not necessary.

With the loop you can then loop through the results. It could be you have duplicated columnnames when joining tables, if that is the case you should define which columns you want from your users table as well. such as u.id as userid

Sign up to request clarification or add additional context in comments.

5 Comments

Thanks for answer, but.. I think sql JOIN won't work, because data are located into 2 different databases, so I connect them seperently with connUsers and connComunity like here ($stmt2 = $this->connUsers)
one caveat is that the first connection user must also have read access to the 2nd database usertable please keep that in mind
Thanks, It helps a lot. Just a one single question more. I have identical data table column names. Like I have "id" in blog_posts and "id" in users. Now when I try to show blog_post "id" it returns all the time user id.
i already said that in my answer ;) i will help you a little SELECT p.*, u.id as userid, u.name as username FROM
Thank you a lot. Now I understood.

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.