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

时间:2014-03-09 16:52:16

标签: php ajax

我有一个可以正常工作的ajax调用,它调用一个PHP Prepared语句方法来进行数据库调用和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.
        }
    }
?>

我想要它出现的地方是注释“// echo语句...”这个文件中未显示的其余部分不再被调用,只有echo语句才会被更新这里。

 <?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));
}

1 个答案:

答案 0 :(得分:0)

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

http://www.php.net/manual/en/function.include.php