简体   繁体   English

在JavaScript文件中获取buddypress用户名

[英]Getting buddypress user name in a javascript file

From within my buddypress PHP templates I usually get the username of any given user by referencing the user ID like this:- 通常从我的buddypress PHP模板中,通过引用用户ID来获取任何给定用户的用户名,如下所示:-

<?php echo bp_core_get_username( '{{userID}}' ) ?>

However I need to retrieve the username from within an external Javascript file. 但是,我需要从外部Javascript文件中检索用户名。

The user ID is already passed in as var aData[11]. 用户ID已作为var aData [11]传入。 I have tried the following with no luck:- 我没有运气就尝试过以下方法:-

 $('td:eq(3)', nRow).html( '<div class="table-row"><a href="http://www.mysite.com/members/' + <?php echo bp_core_get_username( '"' + aData[11] + '"' ) ?> + '/song/?songsID=' + aData[10] + '"><h4 class="nomargin">' + aData[0] + '</h4></a>' + aData[1] + '</div>' );

I am trying to return a URL... This cirrently gives me: 我正在尝试返回一个URL ...目前,这给了我:

http://www.mysite.com/members//song/?songsID=6 http://www.mysite.com/members//song/?songsID=6

Whe it should be: 应该是:

http://www.mysite.com/members/ username /song/?songsID=6 http://www.mysite.com/members/ 用户名 / song /?songsID = 6

Any ideas? 有任何想法吗?

Edit: Below is the server side code that I am using to ouput the JSON 编辑:以下是我用来输出JSON的服务器端代码

<?php

    $aColumns = array( 'song_name', 'artist_band_name', 'author', 'song_artwork', 'song_file', 'genre', 'song_description', 'uploaded_time', 'emotion', 'tempo', 'songsID', 'user' );


    $sIndexColumn = "songsID";

    /* DB table to use */
    $sTable = "wp_dbt_songs";

    /* Database connection information */
    $gaSql['user']       = "";
    $gaSql['password']   = "";
    $gaSql['db']         = "";
    $gaSql['server']     = "";



    $gaSql['link'] =  mysql_pconnect( $gaSql['server'], $gaSql['user'], $gaSql['password']  ) or
        die( 'Could not open connection to server' );

    mysql_select_db( $gaSql['db'], $gaSql['link'] ) or 
        die( 'Could not select database '. $gaSql['db'] );



    $sLimit = "";
    if ( isset( $_GET['iDisplayStart'] ) && $_GET['iDisplayLength'] != '-1' )
    {
        $sLimit = "LIMIT ".mysql_real_escape_string( $_GET['iDisplayStart'] ).", ".
            mysql_real_escape_string( $_GET['iDisplayLength'] );
    }



    if ( isset( $_GET['iSortCol_0'] ) )
    {
        $sOrder = "ORDER BY  ";
        for ( $i=0 ; $i<intval( $_GET['iSortingCols'] ) ; $i++ )
        {
            if ( $_GET[ 'bSortable_'.intval($_GET['iSortCol_'.$i]) ] == "true" )
            {
                $sOrder .= $aColumns[ intval( $_GET['iSortCol_'.$i] ) ]."
                    ".mysql_real_escape_string( $_GET['sSortDir_'.$i] ) .", ";
            }
        }

        $sOrder = substr_replace( $sOrder, "", -2 );
        if ( $sOrder == "ORDER BY" )
        {
            $sOrder = "";
        }
    }



    $sWhere = "";
    if ( $_GET['sSearch'] != "" )
    {
        $sWhere = "WHERE (";
        for ( $i=0 ; $i<count($aColumns) ; $i++ )
        {
            $sWhere .= $aColumns[$i]." LIKE '%".mysql_real_escape_string( $_GET['sSearch'] )."%' OR ";
        }
        $sWhere = substr_replace( $sWhere, "", -3 );
        $sWhere .= ')';
    }

    /* Individual column filtering */
    for ( $i=0 ; $i<count($aColumns) ; $i++ )
    {
        if ( $_GET['bSearchable_'.$i] == "true" && $_GET['sSearch_'.$i] != '' )
        {
            if ( $sWhere == "" )
            {
                $sWhere = "WHERE ";
            }
            else
            {
                $sWhere .= " AND ";
            }
            $sWhere .= $aColumns[$i]." LIKE '%".mysql_real_escape_string($_GET['sSearch_'.$i])."%' ";
        }
    }



    $sQuery = "
        SELECT SQL_CALC_FOUND_ROWS ".str_replace(" , ", " ", implode(", ", $aColumns))."
        FROM   $sTable
        $sWhere
        $sOrder
        $sLimit
    ";
    $rResult = mysql_query( $sQuery, $gaSql['link'] ) or die(mysql_error());

    /* Data set length after filtering */
    $sQuery = "
        SELECT FOUND_ROWS()
    ";
    $rResultFilterTotal = mysql_query( $sQuery, $gaSql['link'] ) or die(mysql_error());
    $aResultFilterTotal = mysql_fetch_array($rResultFilterTotal);
    $iFilteredTotal = $aResultFilterTotal[0];

    /* Total data set length */
    $sQuery = "
        SELECT COUNT(".$sIndexColumn.")
        FROM   $sTable
    ";
    $rResultTotal = mysql_query( $sQuery, $gaSql['link'] ) or die(mysql_error());
    $aResultTotal = mysql_fetch_array($rResultTotal);
    $iTotal = $aResultTotal[0];


    $output = array(
        "sEcho" => intval($_GET['sEcho']),
        "iTotalRecords" => $iTotal,
        "iTotalDisplayRecords" => $iFilteredTotal,
        "aaData" => array()
    );

    while ( $aRow = mysql_fetch_array( $rResult ) )
    {
        $row = array();
        for ( $i=0 ; $i<count($aColumns) ; $i++ )
        {
            if ( $aColumns[$i] == "version" )
            {
                /* Special output formatting for 'version' column */
                $row[] = ($aRow[ $aColumns[$i] ]=="0") ? '-' : $aRow[ $aColumns[$i] ];
            }
            else if ( $aColumns[$i] != ' ' )
            {
                /* General output */
                $row[] = $aRow[ $aColumns[$i] ];
            }
        }
        $output['aaData'][] = $row;
    }

    echo json_encode( $output );
?>

The html you're setting is being set on the client side, not on the server side, meaning that the php code will not be evaluated. 您正在设置的html是在客户端设置的,而不是在服务器端设置的,这意味着将不会评估php代码。

I guess what you can do is either put the already-evaluated username in a hidden tag and get that using jquery or expose it to the javascript on the page within script tags. 我猜您可以将已评估的用户名放在隐藏标签中,然后使用jquery将该用户名公开,或者将其公开给脚本标签内页面上的javascript。

EDIT : So is the userID or the username in the json as well? 编辑 :那么json中的userID或用户名也是吗? If not, you could probably put it there. 如果没有,您可能会把它放在那里。 I'm still not so sure what the situation is. 我仍然不确定情况如何。 But if you're currently passing the userID in the json and actually want the username as bp_core_get_username would return it, why not just put the already retrieved (with bp_core_get_username ) username into the json as well? 但是,如果您当前正在json中传递userID ,并且实际上希望将用户名作为bp_core_get_username返回,那么为什么不将已获取的用户名(使用bp_core_get_username )也放到json中呢?

In other words, in your php code, figure out what the username is based on the userid with bp_core_get_username and then pass that into the json. 换句话说,在您的php代码中,使用bp_core_get_username基于userid的用户名,然后将其传递给json。 That is, if I'm understanding the situation correctly. 也就是说,如果我正确地了解了情况。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM