使用拖放系统来订购joomla中的项目。 从数据库中获取项目不是问题。 我使用以下代码
执行此操作`<script type="text/javascript">
$(document).ready(function(){
$(function() {
$("#contentLeft ul").sortable({ opacity: 0.6, cursor: 'move', update: function() {
var order = $(this).sortable("serialize") + '&action=updateRecordsListings';
$.post("templates/sorteren/test/updateDB.php", order, function(theResponse){
$("#contentRight").html(theResponse);
});
}
});
});
});
</script>
<?php
$db = & JFactory::getDBO();
$query = $db->getQuery(true)
->select('a.title, a.id, a.sorteren')
->from('#__k2_items as a')
->where('a.created_by =' . $user->id . ' and a.trash = 0')
->order('a.sorteren');
$db->setQuery($query);
$items = $db->loadObjectList();
foreach($items as $item) {
echo '<li id="recordsArray_' . $item->id . '">' . $item->title . '';
}?>`
但问题出现在下面的代码中,但我不知道问题可能是什么
<?php defined('_JEXEC') or die;
$db =& JFactory::getDBO();
$query = $db->getQuery(true);
$action = mysql_real_escape_string($_POST['action']);
$updateRecordsArray = $_POST['recordsArray'];
if ($action == "updateRecordsListings"){
$listingCounter = 1;
foreach ($updateRecordsArray as $recordIDValue) {
$query = "UPDATE #__k2_items SET sorteren = " . $listingCounter . " WHERE id = " . $recordIDValue;
$db->setQuery($query);
$db->query();
$listingCounter = $listingCounter + 1;
}
echo '<pre>';
print_r($updateRecordsArray);
echo '</pre>';
echo 'If you refresh the page, you will see that records will stay just as you modified.';
}
?>
更新:
我在Joomla外面使用它并且它可以工作
<script type="text/javascript" src="jquery-1.3.2.min.js"></script>
<script type="text/javascript" src="jquery-ui-1.7.1.custom.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$(function() {
$("#contentLeft ul").sortable({ opacity: 0.6, cursor: 'move', update: function() {
var order = $(this).sortable("serialize") + '&action=updateRecordsListings';
$.post("updateDB.php", order, function(theResponse){
$("#contentRight").html(theResponse);
});
}
});
});
});
</script>
<div id="contentLeft">
<ul>
<?php
$query = "SELECT * FROM kxmkw_k2_items WHERE created_by = 1000 AND trash = 0 ORDER BY sorteren ASC";
$result = mysql_query($query);
while($row = mysql_fetch_array($result, MYSQL_ASSOC))
{
?>
<li id="recordsArray_<?php echo $row['id']; ?>"><?php echo $row['title'] . "<br> " . $row['id']; ?></li>
<?php } ?>
</ul>
</div>
在joomla里面我用这个
<script type="text/javascript">
$(document).ready(function(){
$(function() {
$("#contentLeft ul").sortable({ opacity: 0.6, cursor: 'move', update: function() {
var order = $(this).sortable("serialize") + '&action=updateRecordsListings';
$.post("templates/sorteren/test/updateDB.php", order, function(theResponse){
$("#contentRight").html(theResponse);
});
}
});
});
});
</script>
<?php
$db = & JFactory::getDBO();
$query = $db->getQuery(true)
->select('a.title, a.id, a.sorteren')
->from('#__k2_items as a')
->where('a.created_by =' . $user->id . ' and a.trash = 0')
->order('a.sorteren');
$db->setQuery($query);
$items = $db->loadObjectList();
foreach($items as $item) {
echo '<li id="recordsArray_' . $item->id . '">' . $item->title . '';
}?>
答案 0 :(得分:0)
首先,你没有使用Joomla编码标准来处理所有内容,而且对于那些比特来说,它们都是旧标准。
尝试使用:
<?php
defined('_JEXEC') or die;
$db = JFactory::getDbo();
$query = $db->getQuery(true);
$input = new JInput;
$post = $input->getArray($_POST);
$action = $post["action"];
$updateRecordsArray = $post["recordsArray"];
if ($action == "updateRecordsListings"){
$listingCounter = 1;
foreach ($updateRecordsArray as $recordIDValue) {
$fields = array(
$db->quoteName('sorteren') . '=' . $listingCounter
);
$conditions = array(
$db->quoteName('id') . ' = ' . $recordIDValue
);
$query->update($db->quoteName('#__k2_items'))->set($fields)->where($conditions);
$db->setQuery($query);
$db->execute();
$listingCounter = $listingCounter + 1;
}
echo '<pre>';
print_r($updateRecordsArray);
echo '</pre>';
echo 'If you refresh the page, you will see that records will stay just as you modified.';
}
?>
如你所见,我做了一些改变:
$_POST
方法取代JInput
。$db->query()
替换为$db->execute()
,因为$db->query()
已被弃用。escape_string
函数替换为quoteName()
,用于处理此问题。有关如何在Joomla中插入或更新数据的更多信息,请阅读:
http://docs.joomla.org/Inserting,_Updating_and_Removing_data_using_JDatabase
更新2:
请尝试使用此代码:
$input = new JInput;
$post = $input->getArray($_POST);
$action = $post["action"];
$updateRecordsArray = $post["recordsArray"];
我已使用这些更改更新了完整查询。
如果这不起作用,请尝试转储变量以查看是否有如下所示的值:
var_dump($action);
var_dump($updateRecordsArray);
希望这有帮助