view search/search_locust_temple.php @ 0:b12c99b7c3f0

commit for previous development
author Zoe Hong <zhong@mpiwg-berlin.mpg.de>
date Mon, 19 Jan 2015 17:13:49 +0100
parents
children
line wrap: on
line source

<?php

include_once('../interface/Lib_mb_utf8.php');
include_once('../interface/config.php');
include_once('search_function.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());
}
function trimKeyword($keywords){
	$token=strtok($keywords,",");
	while($token!=false){
		$token=trim($token);
		$keywordArray[]=$token;
		$token=strtok(",");
	}
	return $keywordArray;
}

function search($keywordArray){
	$condition="";
	foreach($keywordArray as $i=>$keyword){
		if($i!=0){
			$condition.=" OR ";
		}
		$condition.=" contents.content LIKE  '%".$keyword."%' ";
	}
	//$query="SELECT books.name AS book_name, books.level1, books.level2, books.period, contents.books_id, contents.line, contents.content FROM contents JOIN books ON contents.books_id = books.id WHERE MATCH (content) AGAINST ('蝗神廟' IN BOOLEAN MODE) ORDER BY contents.books_id, contents.line";

	$query="SELECT books.name AS book_name, books.level1, books.level2, books.period, contents.books_id, contents.line, contents.content 
                FROM contents
                JOIN books ON contents.books_id = books.id
                WHERE ".$condition."
                ORDER BY contents.books_id, contents.line";
                //WHERE contents.content LIKE  '%".$keyword."%'
	$result = mysql_query($query);
	//echo "result length: ".mysql_num_rows($result)."<br>";
	//go through each page
	while ($row = mysql_fetch_assoc($result)) {
        	//find the sections that includes this page
		$subQuery="SELECT id, name, start_page, end_page 
			FROM sections
			WHERE books_id=".$row['books_id']." AND start_page<=".$row['line']." AND end_page>=".$row['line'];
		$subResult=mysql_query($subQuery);
		$sectionArray=array();	
		while($subRow=mysql_fetch_assoc($subResult)){
			$sectionArray[]=$subRow;
		}
		$row['section']=$sectionArray;

		//find the next page and concatenate
		$subQuery="SELECT content
			FROM contents
			WHERE books_id=".$row['books_id']." AND line=".($row['line']+1);
		$subResult=mysql_query($subQuery);
		$subRow=mysql_fetch_assoc($subResult);
		$row['content'].=$subRow['content'];
		
		$resultArray[]=$row;
	}
	return $resultArray;
}

function printTable($array,$keywordArray,$filename){
	//var_dump($array);
	//return;
	$header='<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
		<html>
        	<head>
                <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
                <link href="../search.css" type="text/css" rel="stylesheet"/>
                <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js" type="text/javascript"></script>
                <script src="../search.js" charset="utf-8"></script>
        	</head>
        	<body>';
	$keywordStr="";
	foreach($keywordArray as $i=>$keyword){
		if($i!=0){
			$keywordStr.=", ";	
		}
		$keywordStr.=$keyword;
	}
	$fp=fopen("./search_results/".$filename.".html","w");
	fwrite($fp,$header);
	echo sizeof($array)." result(s) of \"".$keywordStr."\" ";
	fwrite($fp,sizeof($array)." result(s) of \"".$keywordStr."\"<br><br>");
	$str="<table>";
	$str.="<tr>";
	$str.="<td class='sequence'>#<td class='bookId'>book id<td class='bookName'>book name<td class='level1'>level1<td class='level2'>level2<td class='period'>period<td class='sectionName'>section info<td class='page'>page<td class='content'>content";
	fwrite($fp,$str);
	echo $str;
	$i=1;
	foreach($array as $row){
		$str="<tr>";
		$str.="<td>".$i;
		$str.="<td>".$row['books_id'];
		$str.="<td>".$row['book_name'];
		$str.="<td>".$row['level1'];
		$str.="<td>".$row['level2'];
		$str.="<td>".$row['period'];
		$str.="<td>";
		fwrite($fp,$str);
		echo $str;
		foreach($row['section'] as $section){
			$str="<div class='section'><a href='tagging_text.php?id=".$section['id']."' target='_blank'>".$section['name']."</a>p".$section['start_page']."-".$section['end_page']."</div>";
			echo $str;
			$str="<div class='section'><a href='../tagging_text.php?id=".$section['id']."' target='_blank'>".$section['name']."</a>p".$section['start_page']."-".$section['end_page']."</div>";
			fwrite($fp,$str);
		}
		$str="<td>".$row['line'];
		//$str.="<td>".$row['content'];
		$str.="<td>".findLocustTempleDescription($row['content'],$keywordArray,0);
		fwrite($fp,$str);
		echo $str;
		$i++;
	}
	
	echo "</table>";
	fwrite($fp,"</table></body></html>");
	fclose($fp);
}
*/

?>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
		<link href="search.css" type="text/css" rel="stylesheet"/>
		<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js" type="text/javascript"></script>
		<script src="search.js" charset="utf-8"></script>
	</head>
	<body>
		<form action='search_locust_temple.php' method='POST'>
			關鍵字: <input type="text" name="keyword"> (若要搜尋多筆關鍵字請用半型逗號,隔開)<br>
			將結果輸出至: <input type="text" name="filename">.html<br>
			<input type="submit" name="search" value="搜尋">
		</form>
		<br>
		<div id="search_result">
		<?php
			if(isset($_POST['search']) && $_POST['keyword']!='' && $_POST['filename']!=''){
				$keywordArray=trimKeyword($_POST['keyword']);
				$array=search($keywordArray);
				writeCsvFile($array,$_POST['filename']);
				printTable($array,$keywordArray,$_POST['filename'],$LOCUST_TEMPLE);
			}			
		?>
		</div>
	</body>
</html>