2015年8月24日月曜日

SQLITE データーベースのデーター追加(途中の記録)

SQLITE データーベースのデーター追加(途中の記録)

<?php
error_reporting(E_ALL);
date_default_timezone_set('Asia/Tokyo');
//print_r(SQLite3::version());

//
//
//
//


$x = new xtext();
echo json_encode($x->title('title',json_decode($_REQUEST['p'])));

class xtext {
public $db_name = 'xtext.db';
//
public function xtext(){
}

//
public function title($title,$paras){

$para['ad_ip'] = $_SERVER['REMOTE_ADDR'];
$para['ad_agent'] = $this->agent($_SERVER['HTTP_USER_AGENT'],"agent");
$para['ad_time'] = $_SERVER["REQUEST_TIME"];
$para['access_time'] = $_SERVER["REQUEST_TIME"];
$para['set_active'] = '1';
$para['ad_user'] = htmlentities($paras->ad_user);
$para['ad_ps'] =  htmlentities($paras->ad_ps);
$para['title'] =  htmlentities($paras->title);

$table_name = 'title';
$url_id = $this->uniq_id('title');
$this->update($table_name,$url_id,$para);

//test pint print_r($para);
return $url_id;
}


//
public function update($table,$url_id,$para ){
$db = new sqlite3($this->db_name);
foreach ($para as $key=>$val){
$insert = sprintf("update %s set %s = \"%s\" where url_id == \"%s\"" , $table,$key,$val,$url_id);
$db->exec($insert);
}
$db->close();
return 'ture';
}

// Uniquid
public function uniq_id($table_name){
$str = array(range('z','a'),range('Z','A'),range('9','0'));
$length = 11;
$str_r =array();
for ($i=0; $i<$length ;$i++){
$temp = $str[array_rand($str)];
$str_r[count($str_r)] = $temp[array_rand($temp)];
}

$db = new sqlite3($this->db_name);
do{
do{
shuffle($str_r);
$temp_id_str = implode($str_r);
$id_check = $db->querySingle(sprintf("select * from %s where url_id='%s'",$table_name,$temp_id_str),true);
if($id_check){
print_r ( $id_check);
}
}while($id_check);

$insert = sprintf("insert into %s(url_id)VALUES('%s')",$table_name,$temp_id_str);
$check = $db->exec($insert);
if($check){
$db->close();
return $temp_id_str;
}
}while(!$check);

}

// Agent
public function agent($agent,$table_name){
$db = new sqlite3($this->db_name);
$up = sprintf("update %s set count = count +1 where agent == '%s'",$table_name,$agent);
$db->exec($up);
$sch = sprintf("select ROWID from %s where agent == '%s'",$table_name,$agent);
$rowid = $db->querySingle($sch);
if(is_null($rowid)){
$new = sprintf("insert into %s (agent,count)values('%s',1)",$table_name,$agent);
$db->exec($new);
$db->close();
return 1;
}else{
$db->close();
return $rowid;
}
$db->close();
}

}


?>

0 件のコメント: