view interface/exportTable.php @ 17:82f8c6bd2b69

add new tag will based on the current topic_id, insert new element into (1)taglist table and (2)topic_tag_relation table
author Zoe Hong <zhong@mpiwg-berlin.mpg.de>
date Fri, 20 Feb 2015 17:57:02 +0100
parents b12c99b7c3f0
children
line wrap: on
line source

<?php
header("Content-Type: text/html;charset=utf-8");

if (isset($_POST['content'])) {
	$content = $_POST['content'];
}
if (isset($_POST['sectionid'])) {
	$sectionid = $_POST['sectionid'];
}

include_once('./Lib_mb_utf8.php');
include_once('config.php');
   
set_time_limit(0);
ini_set('memory_limit', '-1');

$link_mysql = mysql_connect($mysql_server, $mysql_user, $mysql_password);
mysql_query("SET NAMES utf8");

if (!$link_mysql) {
    die('Could not connect: ' . mysql_error());
}

$db_selected = mysql_select_db($mysql_database, $link_mysql);
if (!$db_selected) {
    die ('Can\'t use foo : ' . mysql_error());
}

if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
{
  $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;

  $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

  switch ($theType) {
    case "text":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;    
    case "long":
    case "int":
      $theValue = ($theValue != "") ? intval($theValue) : "NULL";
      break;
    case "double":
      $theValue = ($theValue != "") ? "'" . doubleval($theValue) . "'" : "NULL";
      break;
    case "date":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;
    case "defined":
      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
      break;
  }
  return $theValue;
}
}

$query = sprintf("SELECT * FROM `sections` WHERE id=\"%s\"", $sectionid);
$result = mysql_query($query);
while ($row = mysql_fetch_assoc($result)) {
	$sectionName = $row['name'];
	$bookId = $row['books_id'];
}

$query = sprintf("SELECT * FROM `books` WHERE id=\"%s\"", $bookId);
$result = mysql_query($query);
while ($row = mysql_fetch_assoc($result)) {
	$bookName = $row['name'];
}

$outputTableArray = array();

$taglistArray="";
$query = sprintf("SELECT * FROM `taglist` ORDER BY `tag` ASC");
$result = mysql_query($query);
while ($row = mysql_fetch_assoc($result)) {
	$taglistArray[] = array( $row['id'], $row['name'], $row['tag'], $row['color'] );
}


$outputTableArray[0]=array();
$outputTableArray[0][0]=array();
$outputTableArray[0][1]=array();
foreach ( $taglistArray as $value ) {
	$outputTableArray[0][0][$value[2]] = $value[1];
	$outputTableArray[0][1][$value[2]] = $value[1]."(Title)";
}
$outputTableArray[0]["other"] = "其他";
$outputTableArray[0]["page"] = "頁數";
$outputTableArray[0]["full"] = "全文";

foreach ( $taglistArray as $tagValue ) {
	$content = preg_replace("/<\/".$tagValue[2].">○*<".$tagValue[2].">/u", "", $content);
	$content = preg_replace("/<".$tagValue[2].">[ ]*<\/".$tagValue[2].">/u", "", $content);
}

$contentLineArray = explode( "<br>", $content );

$count=0;
$pageNow=NULL;
foreach ( $contentLineArray as $value ) {
	$count++;
	$recordString = $value;
	$otherString = $recordString;
	//echo $recordString."<br>\n";
	if ( preg_match("/【<a(.*?)>(.*?)<\/a>】/u", $recordString, $matches) ) {
		$pageNow = $matches[2];
	}
	foreach ( $taglistArray as $tagValue ) {
		if ( preg_match_all("/<".$tagValue[2].">(.*?)<\/".$tagValue[2].">/u", $recordString, $matches, PREG_SET_ORDER) ) {
			foreach ( $matches as $matchesValue ) {
				$matchesValue[1] = preg_replace("/○/u", "", $matchesValue[1]);
				if ( preg_match_all("/〈(.*?)〉/u", $matchesValue[1], $matches2, PREG_SET_ORDER) ) {
					foreach ( $matches2 as $matches2Value ) {
						if ( isset($outputTableArray[$count][0][$tagValue[2]]) ) {
							$outputTableArray[$count][0][$tagValue[2]] .= ";".$matches2Value[1];
						} else {
							$outputTableArray[$count][0][$tagValue[2]] = $matches2Value[1];
						}
					}
				} else {
					if ( isset($outputTableArray[$count][0][$tagValue[2]]) ) {
						$outputTableArray[$count][0][$tagValue[2]] .= ";".$matchesValue[1];
					} else {
						$outputTableArray[$count][0][$tagValue[2]] = $matchesValue[1];
					}
				}
			}
			$otherString = preg_replace("/<".$tagValue[2].">(.*?)<\/".$tagValue[2].">/u", " ", $otherString);	
		}
	}
	$otherString = preg_replace("/○/u", "", $otherString);
	$outputTableArray[$count]["other"] = $otherString;
	$outputTableArray[$count]["page"] = $pageNow;
	$value = preg_replace("/>/u", "&gt;", $value);
	$value = preg_replace("/</u", "&lt;", $value);
	$outputTableArray[$count]["full"] = $value;
}

foreach ( $outputTableArray as $arrayIndex => $arrayValue ) {
	if ( !isset($arrayValue[0]["person"]) ) {
		unset($outputTableArray[$arrayIndex]);
	}
}
?>

<html>
<head>
<title></title>
<script src="./jquery-1.10.2.min.js"></script>

</head>

<body>
<table width="100%" border="1" id="tableMain">
	
<?php
$count=1;
	foreach ( $outputTableArray as $trIndex =>$trValue ) {
		echo "<tr>";
		if ( $trIndex==0 ) {
			echo "<td>#</td>";
			echo "<td>Book ID</td>";
			echo "<td>Section ID</td>";
			echo "<td>方志</td>";
			echo "<td>部</td>";
		} else {
			echo "<td>".$count++."</td>";
			echo "<td>".$bookId."</td>";
			echo "<td>".$sectionid."</td>";
			echo "<td>".$bookName."</td>";
			echo "<td>".$sectionName."</td>";
		}
		if ( isset( $trValue["page"] ) ) {
			echo "<td>".$trValue["page"]."</td>";
		} else {
			echo "<td>&nbsp;</td>";
		}
		/*
		foreach ( $outputTableArray[0][1] as $index => $value ) {
			if ( isset( $trValue[1][$index] ) ) {
				echo "<td>".$trValue[1][$index]."</td>";
			} else {
				echo "<td>&nbsp;</td>";
			}
		}
		*/
		foreach ( $outputTableArray[0][0] as $index => $value ) {
			if ( $index == "time" ) {
				if ( isset( $trValue[0][$index] ) ) {
					echo "<td>".$trValue[0][$index]."</td>";
					echo "<td>".$trValue[0][$index]."</td>";
				} else {
					echo "<td>&nbsp;</td>";
					echo "<td>&nbsp;</td>";
				}
			} else {
				if ( isset( $trValue[0][$index] ) ) {
					echo "<td>".$trValue[0][$index]."</td>";
				} else {
					echo "<td>&nbsp;</td>";
				}
			}
		}
		if ( isset( $trValue["other"] ) ) {
			echo "<td>".$trValue["other"]."</td>";
		} else {
			echo "<td>&nbsp;</td>";
		}
		if ( isset( $trValue["full"] ) ) {
			echo "<td>".$trValue["full"]."</td>";
		} else {
			echo "<td>&nbsp;</td>";
		}
		echo "</tr>";
	}
?>
</table>
</body>
</html>