dppx9253 2013-10-04 10:28
浏览 43
已采纳

从mysql填充@mentions数组

So, I am using the mention.js library to add a @mentionUser functionality into my application. I am wondering what is the best way to replace the array with an array I build, fetching users from my database.

I have build the query to fetch the users, I'm just not sure how to use the resulting array in jQuery below.

PHP code (pseudocode mostly, will ofc use PDO)

$fetchUsers = mysql_query("SELECT * FROM users1 WHERE organisationId = '1'");
$results = array();
while ($row = mysql_fetch_assoc($fetchUsers)) {
    $results[] = $row['name'];
}

Thanks alot!

<textarea id="full"></textarea>

<script>
    $("#full").mention({
    delimiter: '@',
    users: 
    [{
        username: "ashley"
    }, { 
        username: "roger"
    }, { 
        username: "frecklefart123"
    }]
});
</script>
  • 写回答

1条回答 默认 最新

  • dpzjl68484 2013-10-04 11:35
    关注

    Have your PHP script return a JSON encoded array of user objects...

    $fetchUsers = mysql_query("SELECT * FROM users1 WHERE organisationId = '1'");
    $results = array();
    while ($row = mysql_fetch_assoc($fetchUsers)) {
        $results[] = array(
            'username' => $row['name']
        );
    }
    
    echo json_encode($results);
    

    And then use jQuery's $.getJSON...

    $.getJSON('/users.php', function(data) {
        $("#full").mention({
            delimiter: '@',
            users: data
        });
    });
    

    Simples :)

    To answer your second question; use some regex (note my example is very basic)...

    $string = '@matt @james @seb';
    
    if (preg_match_all('/@(\w+)/i', $string, $matches)) {
        $mentions = $matches[1];
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 请问如何在openpcdet上对KITTI数据集的测试集进行结果评估?
  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源
  • ¥15 安卓JNI项目使用lua上的问题
  • ¥20 RL+GNN解决人员排班问题时梯度消失
  • ¥60 要数控稳压电源测试数据
  • ¥15 能帮我写下这个编程吗
  • ¥15 ikuai客户端l2tp协议链接报终止15信号和无法将p.p.p6转换为我的l2tp线路
  • ¥15 phython读取excel表格报错 ^7个 SyntaxError: invalid syntax 语句报错