Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/462.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
嵌套javascript使注释系统变慢_Javascript_Php_Ajax - Fatal编程技术网

嵌套javascript使注释系统变慢

嵌套javascript使注释系统变慢,javascript,php,ajax,Javascript,Php,Ajax,我创建了一个系统,允许用户查看照片并发表评论。如果用户单击相册,他将被发送到一个页面,其中第一张照片(带有评论和评论回复)显示在页面中心。右侧将列出当前相册的所有其他照片 这一部分是用简单的PHP代码完成的,每次加载新页面时都会用到 当用户现在单击其中一张照片(在右侧列出)时,不会加载整个页面,而只会通过Ajax加载相应的元素(照片、评论、评论回复) 到目前为止,这种方法效果良好。以下是相应的代码,以便更好地理解: PHP: 但这不起作用。因为我已经读到这可能是由于服务器或我与MAMP合作的信息

我创建了一个系统,允许用户查看照片并发表评论。如果用户单击相册,他将被发送到一个页面,其中第一张照片(带有评论和评论回复)显示在页面中心。右侧将列出当前相册的所有其他照片

这一部分是用简单的PHP代码完成的,每次加载新页面时都会用到

当用户现在单击其中一张照片(在右侧列出)时,不会加载整个页面,而只会通过Ajax加载相应的元素(照片、评论、评论回复)

到目前为止,这种方法效果良好。以下是相应的代码,以便更好地理解:

PHP:


但这不起作用。因为我已经读到这可能是由于服务器或我与MAMP合作的信息。您有什么解决方案吗?

我认为您应该在从服务器获取数据时使用json结构(),这应该避免以后在js中进行任何“拆分”操作,并且还有其他附加优势。也调查一下。 此外,当您加载页面中的所有图像时:

   $style_list_right .= '<div id="right_'.$id.'"    onclick="getPhotos(\''.$album_id.'\',\''.$id.'\',\''.$u.'\')">';
    $style_list_right .=     '<img src="'.USERFILES . $u.'/'.$filename.'" />';
    $style_list_right .= '</div>';
$style\u list\u right.='';
$style_list_right.='';
$style_list_right.='';
您可能可以:

    $style_list_right .=     '<img src="'.USERFILES . $u.'/'.$filename.'" filename=$filename photoname="$photoname" albimid="$album_id" onclick="getPhotos(this)"/>';
$style\u list\u right.='';
因此,您在第一次获取中拥有所有信息,因此当您单击它时,您可以从图像属性中获取这些细节(并且根本不进行ajax调用)。因此,只需一个ajax调用即可获得评论。
另外,只返回数据(无html标记)对ajax响应也有好处,因为您从服务器获得的数据量较少,这使得ajax响应更快,然后您可以在客户机中使用js更快地在数据周围添加这些标记。希望这有点帮助。

好的,第二个问题是什么?我不确定您在那里尝试做什么,似乎您正在尝试从javascript值设置php变量!这是不可能的,因为php在服务器上运行,javascript在客户机上运行。这对性能有好处吗?
// Ajax calls this to load the clicked Photo --> getPhotos()
if (isset($_POST["show"]) && $_POST["show"] == "photos"){
    $picstring = "";
    $photo_id = preg_replace('#[^0-9]#', '', $_POST["photo"]);
    $sql = "...";
    $query = mysqli_query($db_conx, $sql);
    while ($row = mysqli_fetch_array($query, MYSQLI_ASSOC)) {
        $filename_1 = $row["..."];
        $filename_2 = $row["..."];
        $photoname = $row["..."];
        $picstring .= "$filename_1|$filename_2|$photoname|||";
    }
    mysqli_close($db_conx);
    $picstring = trim($picstring, "|||");
    echo $picstring;
    exit();
}

// Ajax calls this to load the comments of the clicked photo --> getComments()
if (isset($_POST["show"]) && $_POST["show"] == "comments") {
    $commentstring = "";
    $photo_id = preg_replace('#[^0-9]#', '', $_POST["photo"]);
    $sql = "...";
    $query = mysqli_query($db_conx, $sql);
    while ($row = mysqli_fetch_array($query, MYSQLI_ASSOC)) {
        $commentid = $row["..."];
        $author = $row["..."];
        $postdate = $row["..."];
        $avatar = $row["..."];
        $user_image = '<img src="'.USERFILES.$author.'/'.$avatar.'" />';
        $data = $row["..."];
        $data = nl2br($data);
        $data = str_replace("&amp;","&",$data);
        $data = stripslashes($data);
        $statusDeleteButton = '';
        if($author == $log_username) {      //  || $account_name == $log_username
            $statusDeleteButton = '...';
        }
        $commentstring .= "$commentid |$author|$data|$postdate|$user_image|$statusDeleteButton|||";
    }
    mysqli_close($db_conx);
    $commentstring = trim($commentstring, "|||");
    echo $commentstring;
    exit();
}

// Ajax calls this to load the replies of the comments --> getReplies()
if (isset($_POST["show"]) && $_POST["show"] == "replies") {
    $commentstring = "";
    $comment_id = preg_replace('#[^0-9]#', '', $_POST["comment"]);
    // GATHER UP ANY STATUS REPLIES
    $status_replies = "";
    $sql = "...";
    $query_replies = mysqli_query($db_conx, "...");
    $query_replies = mysqli_query($db_conx, $sql);
    $replynumrows = mysqli_num_rows($query_replies);
    if($replynumrows > 0) {
        while ($row = mysqli_fetch_array($query_replies, MYSQLI_ASSOC)) {
            $replyid = $row["…"];
            $replyauthor = $row["..."];
            $replydata = $row["..."];
            $avatar = $row["…"];
            $user_image = '<img src="'.USERFILES.$replyauthor.'/'.$avatar.'"/>';
            $replydata = nl2br($replydata);
            $replypostdate = $row["..."];
            $replydata = str_replace("&amp;","&",$replydata);
            $replydata = stripslashes($replydata);
            $replyDeleteButton = '';
            if($replyauthor == $log_username) { 
                $replyDeleteButton = '…';
            }
            $commentstring .= "$replyid|$replyauthor|$replydata|$replypostdate|$user_image|$replyDeleteButton|||";
        }
    }
    mysqli_close($db_conx);
    $commentstring = trim($commentstring, "|||");
    echo $commentstring;
    exit();
}
$variable = "";

if (IS_AJAX) {
    $user = \'+variable[0]+\';
    $content = \'+variable[1]+\';
}

$variable .= '<div>';
$variable .=     '<a href="'.$user.'">'.$user.'</a>';
$variable .=        '<div><p>'.$content.'</p></div>';
$variable .= '</div>';
$ajax = 0;
if (isset($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest')
{
    $ajax = 1;
}
define('IS_AJAX',$ajax);
   $style_list_right .= '<div id="right_'.$id.'"    onclick="getPhotos(\''.$album_id.'\',\''.$id.'\',\''.$u.'\')">';
    $style_list_right .=     '<img src="'.USERFILES . $u.'/'.$filename.'" />';
    $style_list_right .= '</div>';
    $style_list_right .=     '<img src="'.USERFILES . $u.'/'.$filename.'" filename=$filename photoname="$photoname" albimid="$album_id" onclick="getPhotos(this)"/>';