我为我的网页翻译创建了“管理菜单”,包括图片,所以你在图片中。
这很简单,我将所有内容保存到数据库中,当我进行更改时,我会重新生成所需的文件,例如。/languages/english/tank_auth_lang.php
. 每当我想翻译/编辑某些内容时,我只需单击字段编辑并单击字段外 AJAX 将 POST 发送到我的控制器,该控制器负责插入/更新/添加翻译。
问题
这是我的功能,但每当我快速重新发送(重新编辑/向表中添加新翻译)时,有时我会在数据库中重复。有什么我应该避免做的吗?(比如在发送前等待或其他东西)。
如果我进行分析,它会告诉我有 2 个相同的插入(不是在一个 AJAX 调用中,而是在两个连续的快速AJAX 调用中)。
函数本身一开始很复杂,因为每当 ( strlen()
) 值比 524 长时,我都会以不同的方式保存它(我将它保存在结构TEXT
not的表中VARCHAR(524)
)。
功能
public function insert() {
//this function is used only with AJAX
if ($this->input->is_ajax_request()) {
//$this->output->enable_profiler(FALSE); //to work profiler must be turned off
$id_kw = $this->input->post('id_kw');
$id_language = $this->input->post('id_language');
$translation_text = $this->input->post('new_translation');
if (strlen($translation_text) < '524') {
//short
if ($this->general_model->isInDBWhere('layout_short', array('id_keyword' => $id_kw, 'id_language' => $id_language))){
//update short
//get id of text_short and update text field in it
$text_id = $this->general_model->_getColumnWhere('layout_short', 'id_text', array('id_keyword' => $id_kw, 'id_language' => $id_language));
$this->general_model->updateRow('text_short', $text_id, array('text' => $translation_text));
unset($text_id);
}elseif ($this->general_model->isInDBWhere('layout_long', array('id_keyword' => $id_kw, 'id_language' => $id_language))) {
$text_id = $this->general_model->_getColumnWhere('layout_long', 'id_text', array('id_keyword' => $id_kw, 'id_language' => $id_language)); //old text_id
$this->general_model->_deleteWhere('text_long', array('id' => $text_id));
$this->general_model->_deleteWhere('layout_long', array('id_keyword' => $id_kw, 'id_language' => $id_language));
//insert new entry to short
$this->_insert($table = 'short', $id_kw, $id_language, $translation_text);
}else{
//insert in short
$this->_insert($table = 'short', $id_kw, $id_language, $translation_text);
}
}else{
//long
if ($this->general_model->isInDBWhere('layout_long', array('id_keyword' => $id_kw, 'id_language' => $id_language))){
//update long
//get id of text_long and update text field in it
$text_id = $this->general_model->_getColumnWhere('layout_long', 'id_text', array('id_keyword' => $id_kw, 'id_language' => $id_language));
$this->general_model->updateRow('text_long', $text_id, array('text' => $translation_text));
unset($text_id);
}elseif ($this->general_model->isInDBWhere('layout_short', array('id_keyword' => $id_kw, 'id_language' => $id_language))) {
$text_id = $this->general_model->_getColumnWhere('layout_short', 'id_text', array('id_keyword' => $id_kw, 'id_language' => $id_language)); //old text_id
$this->general_model->_deleteWhere('text_short', array('id' => $text_id));
$this->general_model->_deleteWhere('layout_short', array('id_keyword' => $id_kw, 'id_language' => $id_language));
//insert new entry to long
$this->_insert($table = 'long', $id_kw, $id_language, $translation_text);
}else{
//insert in short
$this->_insert($table = 'long', $id_kw, $id_language, $translation_text);
}
}
echo "1";
}
}
JavaScript
$(".ajax-translate-field").on("focusout", function(){
console.log($(this).prop('name'));
console.log($(this).attr('data-language'));
console.log($(this).val());
var this_object = $(this);
if ($(this).val() === "") {
this_object.animate({ backgroundColor: '#FF9494', opacity: '0.6'}, "slow");
this_object.animate({ backgroundColor: '#ffffff', opacity: '1'}, "fast");
}else{
$.ajax({
url: _baseUrl + 'admin/language/insert',
type: 'POST',
data: {id_kw: $(this).prop('name'), id_language : $(this).attr('data-language'), new_translation: $(this).val()},
success: function (result) {
if (result == 1) {
this_object.animate({ backgroundColor: '#BCED91', opacity: '0.6'}, "slow");
this_object.animate({ backgroundColor: '#ffffff', opacity: '1'}, "fast");
}else{
//nothing happens here yet
}
}
});
}
});