skip to Main Content

I want to make custom code website function into wordpress.

its continuation from My URL question here: https://stackoverflow.com/questions/65456090/change-default-user-profile-url-bbpress-plugin

I have code that insert value into database phpmyadmin

function topic_count($reply_id = 0){
    global $connection;

    $user_id= bbp_get_reply_author_id( $reply_id );
    $username= get_userdata($user_id);
    $topic_count  = bbp_get_user_topic_count_raw( $user_id);
    $reply_count = bbp_get_user_reply_count_raw( $user_id);
    $post_count   = (int) $topic_count + $reply_count;


    if(isset($user_id, $username->user_login, $topic_count, $reply_count, $post_count)){

    $query = "INSERT INTO example (id, username, topic_count, reply_count, post_count ) 
        VALUES (
            '$user_id', 
            '$username->user_login', 
            '$topic_count', 
            '$reply_count', 
            '$post_count')";
    mysqli_query($connection , $query);

    }
}
add_action('bbp_theme_after_reply_author_details', 'topic_count');

The code working well, and the data stored into phpmyadmin. See this image below:
example

Now, i want to make function to get the user data. Not listing all the data, only retrieve specific user data. The key to define what data to take only through URL. Because the url have username defined, from this code:

function user_profile_link(){
    $author_id = bbp_get_reply_author_id();
    $user_info = get_userdata($author_id);

    $url = site_url()."/profile/".$user_info->user_login;

    return $url;

}
add_filter('bbp_get_user_profile_url', 'user_profile_link');

This is the image result:

url

Im trying to get the data with this code:

$id_query = mysqli_query($connection, "SELECT * FROM example" ) ;
$test = mysqli_fetch_assoc($id_query);
echo $test["username"];

And yes, the data can be fetched. But the problem is, only fetch first data. ‘achan’. User andre and also chandra, also get ‘achan’ username.

In the nutshell, how to make code like, this url ‘http://localhost/example/profile/chandra/’ for ‘chandra’ data. this url ‘http://localhost/example/profile/andre/’ for ‘andre’ data.

All help is appreciated. Thank you web developer.

2

Answers


  1. Chosen as BEST ANSWER

    I found the solution in my own way. I'm using $_SERVER['REQUEST_URI'] the code is like this:

    $uriSegments = explode("/", parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH));
        $id_query = mysqli_query($connection, "SELECT * FROM example" ) ;
    
        foreach($id_query as $row);
    
        if($uriSegments[3] == $row["username"]){
    
        //your code, but in my case something like this
    ?>
    <table style="border-collapse: collapse; width: 100%;">
    <tbody>
    <tr>
    <td style="width: 20%; border-style: solid; border-color: #000000;">User ID</td>
    <td style="width: 20%; border-style: solid; border-color: #000000;">Username</td>
    <td style="width: 20%; border-style: solid; border-color: #000000;">Jumlah Topik</td>
    <td style="width: 20%; border-style: solid; border-color: #000000;">Jumlah 
    Balasan</td>
    <td style="width: 20%; border-style: solid; border-color: #000000;">Total</td>
    </tr>
    <tr>
    <td style="width: 20%; border-style: solid; border-color: #000000;"><?= $row["id"] ?> 
    </td>
    <td style="width: 20%; border-style: solid; border-color: #000000;"><?= 
    $row["username"] ?></td>
    <td style="width: 20%; border-style: solid; border-color: #000000;"><?= 
    $row["topic_count"] ?></td>
    <td style="width: 20%; border-style: solid; border-color: #000000;"><?= 
    $row["reply_count"] ?></td>
    <td style="width: 20%; border-style: solid; border-color: #000000;"><?= 
    $row["post_count"] ?></td>
    
    </tr>
    </tbody>
    </table>
    <br /><br />
    
    
    <?php
    

    }

    This code solve my problem. I hope this answer will be useful.


  2. use while when you fetch data and set it to array; then use foreach.

    $test_array = [];
    while($test = mysqli_fetch_assoc($id_query)){
       $test_array[] = $test;
    }
    
    foreach(test_array as $item){
       echo $item["username"];
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search