将代码片段移动到另一个PHP文件

将代码片段移动到另一个PHP文件,php,ajax,Php,Ajax,我有一个ajax调用,它可以正常工作,它调用一个PHP准备好的语句方法来进行数据库调用和echo的一些东西。问题是,它会在其他地方重复它,因此我必须将代码移动到另一个文件中,并且只移动数据库代码片段。我的第一个文件是这样的:(注释之间的代码“//特别是这个代码…”应该出现在另一个页面上,或者至少echo语句应该出现 <?php if ($_SERVER['REQUEST_METHOD'] === 'POST') { if(isset($_POST['item_id

我有一个ajax调用,它可以正常工作,它调用一个PHP准备好的语句方法来进行数据库调用和echo的一些东西。问题是,它会在其他地方重复它,因此我必须将代码移动到另一个文件中,并且只移动数据库代码片段。我的第一个文件是这样的:(注释之间的代码“//特别是这个代码…”应该出现在另一个页面上,或者至少echo语句应该出现

<?php
    if ($_SERVER['REQUEST_METHOD'] === 'POST') {

        if(isset($_POST['item_id'])){

        $item_number = $_POST['item_id'];

            require('../includes/db_connect.php');

        /* Register a prepared statement */
        if ($stmt = $mysqli->prepare('UPDATE house_room1 SET rotation = (rotation + 1) % 4 WHERE ref_id = ?')) {

                /* Bind parametres */
                $stmt->bind_param('i', $item_number);

                /* Execute the query */
                $stmt->execute();

                $stmt->bind_result($rotation);

                $stmt->fetch();

                /* Close statement */
                $stmt->close();

            } else {
                /* Something went wrong */
                echo 'Something went terribly wrong'     . $mysqli->error;
            }

            //Specifically this code has to be moved to another file, but be called here. ->

            if ($stmt = $mysqli->prepare('SELECT x, y, z, src, rotation, link, div_id FROM house_room1 INNER JOIN objects ON house_room1.object_id=objects.object_id WHERE house_room1.ref_id = ?')) {

                $stmt->bind_param('i', $item_number);

                $stmt->execute();

                $stmt->bind_result($x, $y, $z, $src, $rotation, $link, $div_id);
                while($stmt->fetch()) {
                    if ($link != "") { 
                        echo '<a href="' . $link . '"> ';
                    }
                    if ($div_id != "") { 
                        echo '<a href="#" onClick="' . $div_id . '"> '; 
                    }
                    echo '<img src="' . $src . $rotation .'.png" class="item' . $item_number . '" style="position:absolute; left:' . $x . 'px; top:' . $y . 'px; z-index:'. $z . ';">'; if ($x != 0) { echo'</a>'; }
                }
             } else {
                echo 'Something went terrible wrong' . $mysqli->error;
            }
            $stmt->close();


            // <- Specifically this code has to be moved to another file, but be called here.
        }
    }
?>

我想您可能想看看php
include
方法,它基本上是用另一个php文件的内容替换自己


我不能将其用于此目的,因为它将包括整个文件,而不仅仅是代码片段。@Owwyes,您可以使用其他文件中的特定代码(
snippet.php
)来生成函数。然后在脚本的开头包含
snippet.php
,并在任何需要的地方调用函数。但这样一来,它就不是ajax的一部分,也就不重要了。嗯,它不起作用了。@CameronLittle是的,很抱歉。出于某种原因,我没有看到您的响应。
 <?php if (login_check($mysqli) == true): ?>
    <?php
        require('database/refresh_house.php');

            //the echo statement shall be echoed here !


    ?>

    <!-- <div id="start"></div> -->
    <!-- <div id="stop"></div> -->

            <ul>
                <li id="posX"></li>
                <li id="posY"></li>
            </ul>
function rotateObject(e)
{
    //e is handler which contains info about the item clicked. From that we can obtain the image id.
    //since the id are of the form img_123(some number), we need to extract only the number.
    var img_id = e.id.split("_")[1];
    var xmlhttp;
    if (window.XMLHttpRequest)
    {// code for IE7+, Firefox, Chrome, Opera, Safari
        xmlhttp=new XMLHttpRequest();
    }
    else
    {// code for IE6, IE5
        xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
    }
    xmlhttp.onreadystatechange=function(){
        if (xmlhttp.readyState==4 && xmlhttp.status==200)
        {
            document.getElementById("item-2").innerHTML=xmlhttp.responseText;
        }
    }
    xmlhttp.open("POST","database/update_settings_rotate.php",true);
    xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    xmlhttp.send("item_id="+encodeURIComponent(img_id));
}