2013-11-22 35 views
1

使用拖放系統在joomla內部訂購商品。 從數據庫獲取項目不成問題。 我做這個用下面的代碼Joomla 3更新dabatase

`<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.'; 
} 
?> 

有沒有誰可以推我在好的方向? 將是巨大的:)

UPDATE:

我用這個外面的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

首先,你沒有使用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.'; 
} 
?> 

正如你所看到的,我已經做了一些改變:

  • JInput方法替換$_POST
  • 替代$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); 

希望這有助於

+0

Lodder, 謝謝您的快速答覆和解釋,但這也沒有更新數據庫。 – user3021953

+0

@ user3021953 - 查看我的更新回答 – Lodder

+0

如果我添加var_dump,它會給我返回null null。外joomla它正在工作,但在joomla裏面,它出錯了。對我有更多提示? :)乾杯\ – user3021953