我有一个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语句才会被更新这里。它不一定必须是应该移动的整个查询,只需要是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>
我希望它有道理,我认为这有点奇怪。我希望有一些东西可以将echo语句引用到另一个div并通过仍然使用ajax显示在那里。
编辑:ajax: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));
}
EDIT2:抱歉,不知道所有这些代码都是必要的。谢谢你的帮助。这就是为什么ajax说“第2项”的原因。但是,第2项只是一个示例,它可以是第1项或第6项或其他任何项目,具体取决于存在的项目数量。这是代码:
$item_number = 0;
$rowsize = 12;
$itemArray = array();
$finalArray = array();
$results = 0;
for ($i = 0; $i < $rowsize; $i++) {
$stmt = $mysqli->stmt_init();
$stmt->prepare('SELECT z, name FROM house_room1 INNER JOIN objects ON house_room1.object_id=objects.object_id WHERE house_room1.ref_id = ?');
$stmt->bind_param('i', $i
);
if ($stmt->execute()) {
$stmt->bind_result($z, $name);
while($stmt->fetch()) {
$results = 1;
$itemArray['number'] = $item_number;
$itemArray['name'] = $name;
$itemArray['ref_id'] = $z;
array_push($finalArray, $itemArray);
}
}
else {
echo 'Something went terribly wrong' . $mysqli->error;
}
$stmt->close();
$item_number++;
}
if($results == 1){
aasort($finalArray,"ref_id");
foreach($finalArray as $arr){
echo '<li id="item-' . $arr['number'] . '" class="ui-state-default"><span class="ui-icon ui-icon-arrowthick-2-n-s"></span>' . $arr['name'] . '
<img class="rotate" id="img_'.$arr['number'].'" src="images/house/other/settings.jpg" onclick="rotateObject(this)">';
}
}
//create a function for sorting
function aasort (&$array, $key) {
$sorter=array();
$ret=array();
reset($array);
foreach ($array as $ii => $va) {
$sorter[$ii]=$va[$key];
}
asort($sorter);
foreach ($sorter as $ii => $va) {
$ret[$ii]=$array[$ii];
}
$array=$ret;
}