Home > Enterprise >  Get user rows using their ID from an array in PHP
Get user rows using their ID from an array in PHP

Time:12-18

I have two MySql tables, one called users and the other one posts.

There are two rows in the "users" table, each containing a user ID and a username. There's also a row in the "posts" table and in one of the cells called "contributors" there are two IDs from the users in the "users table." separated with a comma, so it looks something like this: 0000000000,1111111111 (not the actual IDs, these are just for reference.).

I want to to a "foreach" for all the IDs in the "contributors" cell, basically loop through all of them and for each of them IDs display the corresponding usernames.

I tried many things including this:

function post_contributors()
{
    $contributor_ids = get_post()[8];
    $contributor_array = explode(",", $contributor_ids);
    $contributors = array_values($contributor_array);

    foreach ($contributors as $contributor) :
        $conn = include '../includes/dbh.inc.php';

        $sql = "SELECT * FROM users WHERE user_id=$contributor";
        $result = mysqli_query($conn, $sql);

        if ($row = $result->fetch_row()) {
            return $row ?? null;
        }

        return null;

        echo '<h3 >' . $result['user_username'] . '</h3>';
    endforeach;
}

This doesn't work. I think it's because of the way I get the user ID (I'm left with an improper array).

Hope this all makes sense and someone can help.

CodePudding user response:

You can rewrite your function like this.

<?php
function post_contributors($postID)
{
    global $conn;
    $contributor_info = "";

    $contributor_ids = get_post($postID)[8];
    $contributor_array = explode(",", $contributor_ids);
    //$contributors = array_values($contributor_array); <== not needed, explode is enough

    foreach ($contributor_array as $contributor) :
        //$conn = include '../includes/dbh.inc.php'; <== not needed here

        $sql = "SELECT * FROM users WHERE user_id=$contributor";
        $result = mysqli_query($conn, $sql);

        if ($row = $result->fetch_assoc()) {
            //return $row ?? null;  <== this would stop your foreach loop
            $contributor_info .= '<h3 >' . $result['user_username'] . '</h3>';
        }

        //return null;  <== this would stop your foreach loop

        //echo '<h3 >' . $result['user_username'] . '</h3>';
    endforeach;

    return $contributor_info; // or echo $contributor_info, if you only want to display it
}
?>

A few explanations.

global $conn assumes that you have included your dbh.inc.php file in such a way that the script in which you call your post_contributors function has access / knowledge of it.

$contributor_info is there to store what you would normally echo out where needed, once you call your function.

The if / else doesn't need to return anything, because it would end your loop, and you would only get the info on the first contributor (if they exist in your contributors table.

Also, I've added $postID as an argument for your function, since you'd need to know that in order to be able to get the contributors.

  • Related