检索db'时出现问题;注';通过php、JSON、查询和sql以样式化html div显示的信息

检索db'时出现问题;注';通过php、JSON、查询和sql以样式化html div显示的信息,php,jquery,sql,arrays,json,Php,Jquery,Sql,Arrays,Json,我正试图: 记下“note”及其id,该id存储在mysql数据库中(与用户名匹配) 使用PHP并将其存储在数组中 使用JSON使其便于客户端处理 使用关联的Notes id信息获取每个Notes实例 在单独的html div中显示每个注释,以便进行CSS样式设置 以允许用户(从数据库)删除其div中的每个注释的方式进行操作 sql成功,我可以检索注释并以以下形式显示它们: “这是注释一”“这是注释二” 但很明显,这不允许步骤4、5和6(这正是我遇到的问题——如何将所有这些都转化为有用的东西,为

我正试图:

  • 记下“note”及其id,该id存储在mysql数据库中(与用户名匹配)
  • 使用PHP并将其存储在数组中
  • 使用JSON使其便于客户端处理
  • 使用关联的Notes id信息获取每个Notes实例
  • 在单独的html div中显示每个注释,以便进行CSS样式设置
  • 以允许用户(从数据库)删除其div中的每个注释的方式进行操作
  • sql成功,我可以检索注释并以以下形式显示它们:

    “这是注释一”
    “这是注释二”

    但很明显,这不允许步骤4、5和6(这正是我遇到的问题——如何将所有这些都转化为有用的东西,为每个“注释”创建一个新的div)


    我已经研究过parse、jquery,甚至是一个接一个的数组,但我现在完全不知所措,因此非常感谢您的帮助。

    我不确定您是否问过问题。但这里有几个想法:

    除非$notes会占用内存(例如,超过10000行),否则应立即对整个集合进行编码;您现在所做的是创建一组JSON数组,但还没有将它们封装在任何可解析的结构中。如果你看一下这个脚本的输出,你就会明白我的意思。例如:

    您的脚本创建如下内容:

    {"one": 1, "two": 2, "three": 3}
    {"one": 1, "two": 2, "three": 3}
    {"one": 1, "two": 2, "three": 3}
    
    { // enclosing structure
        {"one": 1, "two": 2, "three": 3}, //elements separated by commas
        {"one": 1, "two": 2, "three": 3},
        {"one": 1, "two": 2, "three": 3}
    }
    
    $sql = "SELECT note FROM notes WHERE username = '$uname'";
    //below is for the next step where I want to also retrieve the note's id
    //$sql = "SELECT * FROM notes WHERE username = '$uname'";
    $result = mysql_query($sql,$conn);
    $i = 0;
    echo "{";
    while($row = mysql_fetch_assoc($result))
    {
        if( $i++ > 0 ) { echo ",\n"; }
        echo json_encode($row);
    }
    echo "}";
    
    您希望在单个对象中包含以下内容:

    {"one": 1, "two": 2, "three": 3}
    {"one": 1, "two": 2, "three": 3}
    {"one": 1, "two": 2, "three": 3}
    
    { // enclosing structure
        {"one": 1, "two": 2, "three": 3}, //elements separated by commas
        {"one": 1, "two": 2, "three": 3},
        {"one": 1, "two": 2, "three": 3}
    }
    
    $sql = "SELECT note FROM notes WHERE username = '$uname'";
    //below is for the next step where I want to also retrieve the note's id
    //$sql = "SELECT * FROM notes WHERE username = '$uname'";
    $result = mysql_query($sql,$conn);
    $i = 0;
    echo "{";
    while($row = mysql_fetch_assoc($result))
    {
        if( $i++ > 0 ) { echo ",\n"; }
        echo json_encode($row);
    }
    echo "}";
    
    所以试试

    $sql = "SELECT note FROM notes WHERE username = '$uname'";
    //below is for the next step where I want to also retrieve the note's id
    //$sql = "SELECT * FROM notes WHERE username = '$uname'";
    $result = mysql_query($sql,$conn);
    $rows = array();
    while($row = mysql_fetch_assoc($result))
    {
        $rows[] = $row;
    }
    echo json_encode($rows, JSON_FORCE_OBJECT);
    
    或者,如果您认为这会耗尽可用内存,则类似以下内容:

    {"one": 1, "two": 2, "three": 3}
    {"one": 1, "two": 2, "three": 3}
    {"one": 1, "two": 2, "three": 3}
    
    { // enclosing structure
        {"one": 1, "two": 2, "three": 3}, //elements separated by commas
        {"one": 1, "two": 2, "three": 3},
        {"one": 1, "two": 2, "three": 3}
    }
    
    $sql = "SELECT note FROM notes WHERE username = '$uname'";
    //below is for the next step where I want to also retrieve the note's id
    //$sql = "SELECT * FROM notes WHERE username = '$uname'";
    $result = mysql_query($sql,$conn);
    $i = 0;
    echo "{";
    while($row = mysql_fetch_assoc($result))
    {
        if( $i++ > 0 ) { echo ",\n"; }
        echo json_encode($row);
    }
    echo "}";
    
    此外,您的脚本应该包含适当的标题。它可能不需要,但这些会帮你省去一些麻烦:

    header('Cache-Control: no-cache, must-revalidate');
    header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
    header('Content-type: application/json');
    

    这是用于创建新div的jquery:

    这似乎是从我的php数组中检索客户端信息的一个很好的解决方案:


    如果我能完成其他步骤,我会为以后的访问者发布链接。

    太好了,有很多东西要看,我会继续看的。关于我的问题,我遇到的问题是尝试将每个数组索引中的内容转换成一种格式,我可以在div中使用,但保留用户删除的能力。我意识到前面可能还有很多步骤,但我想确保任何回应都是朝着正确的方向进行的。再次感谢。我会开始尝试的。