在我的应用程序中,我想将数据从一台服务器传输到许多客户端,反之亦然。 我是通过在服务器端制作一个名为“ syncs”的控制器来完成此操作的,该控制器包含一个名为“ export_data”的方法,该方法将数据转换为可传输格式,现在我将该数据传输到一个名为“ post_data”的方法,该方法包含客户端控制器“ sync”的链接如果现在服务器上的“ is_sync”列设置为“ 0”(如果其值为“ 1”),则此import_data方法将开始将数据插入表中,如果该值的值为“ 1”,则该数据已显示为已同步数据作为警报消息。
但是当我从服务器向客户端发送数据时什么也没发生。
all_set.php
$.post('<?=base_url("syncs/export_data")?>',{ tablename :
'sma_class_set', ids : arr , async : true , cache : false},
function(ret){
alert(arr);
});
syncs.php
function export_data(){
$data = NULL;
if ( $_POST ) {
$table = $this->db->escape_str($_POST['tablename']);
$sales_id = $_POST['ids'];
$in_ids = implode(",", $sales_id);
if ( isset( $sales_id ) && !empty( $sales_id ) ) {
$sql = $this->db->query("SELECT * FROM `$table` WHERE `is_synced` = '0' AND `".$this->tables[$table]['exclude']."` IN($in_ids) ");
} else {
$sql = $this->db->query("SELECT * FROM `$table` WHERE `is_synced` = '0' ");
}
if ( !empty($sql->result_array()) ) {
$final = $str_array = array();
foreach ( $sql->result_array() as $sk => $sv){
$col = array();
foreach ($sv as $svk => $svl) {
if ( $svk != $this->tables[$table]['exclude'] ) {
array_push($col, "'$svl'");
}
}
array_push($final, explode("|", "(".implode(",", $col).")|"));
}
foreach ($final as $fk => $fv) {
array_push($str_array, $fv[0]);
}
$str = implode(",", $str_array);
$output = "INSERT INTO `$table`(".$this->tables[$table]['include'].") VALUES $str";
if ( filter_var($this->post_data($output), FILTER_VALIDATE_INT) === false ) {
$data = $this->post_data($output);
} else {
$this->db->query("UPDATE `$table` SET `is_synced` = '1' WHERE `".$this->tables[$table]['exclude']."` IN($in_ids) ");
$data = "Data Synced Successfully";
}
} else {
$data = "Already synced data";
}
} else {
$data = "Error no values posted";
}
echo $data;
}
function post_data($qry){
$qry = htmlspecialchars(urlencode($qry));
$data = "qry=" . $qry;
$ch = curl_init();
curl_setopt( $ch, CURLOPT_URL,"http://localhost/BDS/pos_app/sync/import_data" );//Localhost url here
curl_setopt( $ch, CURLOPT_AUTOREFERER, 1);
curl_setopt ( $ch, CURLOPT_HTTPHEADER, array ( 'Content-length: ' . strlen($qry)+1 ) );
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt( $ch, CURLOPT_POST,1);
curl_setopt( $ch, CURLOPT_POSTFIELDS,$data);
curl_setopt( $ch, CURLOPT_CRLF, 1);
$result = curl_exec($ch);
curl_close($ch);
return $result;
}
客户端“ sync.php”现在使用“ import_data”功能导入数据
sync.php
function import_data(){
$return = "";
if ( isset( $_POST['qry'] ) && !empty( $_POST['qry'] ) ) {
$qry = htmlspecialchars_decode( urldecode( $_POST['qry']));
$this->db->trans_start();
$this->db->query($qry);
$insert_id = $this->db->insert_id();
$this->db->trans_complete();
if ($this->db->trans_status() === FALSE) {
# Something went wrong.
$this->db->trans_rollback();
$return = "Error transaction could not be completed";
}
else {
# Everything is Perfect.
# Committing data to the database.
$this->db->trans_commit();
$return = $insert_id;
}
}else{
$return = "Data do not received in proper way";
}
echo $return;
}