<?php
#by FeST
#ICQ : 3452223
#Действия с таблицами

$table=chek($_GET['table']);
$sql=$_REQUEST['sql'];
$order=isset($_GET['order']) ? chek($_GET['order']) : NULL;
$ord=isset($_GET['ord']) ? chek($_GET['ord']) : NULL;
	if($order!=NULL)$order_q="ORDER BY `".$order."` ".$ord;else $order_q=NULL;
		


if(isset($_POST['go'])){
	$page=($_POST['start']*10)-10;
	header("Location:index.php?table=".$table."&mode=sql&sql=".chek($sql)."&start=".$page.($order!=NULL ? "&order=".$order : "").''.($ord!=NULL ? "&ord=".$ord : ""));
	exit;
}


	

echo "Таблица &quot;<a href='index.php?table=".$table."&amp;mode=show'><b>".$table."</b></a>&quot;-&gt;MySQL<br/>

<form method='post' action='index.php?mode=sql&amp;table=".$table."&amp;".mt_rand()."'>
MySQL : <br/>
SELECT * FROM `".$table."` WHERE<br/>
<input name='sql'  value='".chek($sql)."' /><br/>
<input name='submit' value='Запрос' type='submit' /></form>
";


	if(!empty($sql)){

$total=mysql_result(mysql_query("SELECT COUNT(*) FROM `".$table."` WHERE ".$sql),0);

	
	
	$query="SHOW FULL FIELDS FROM ".$table."";
		$arr = mysql_query($query); 
		if(!$arr) echo "Произошла ошибка: ".mysql_error(); 
			if(mysql_num_rows($arr)) 
			{ 
			 $for_count=mysql_num_rows($arr);
				echo "Результат:
				<table width='100%' border='1'><tr align='center'>";
					
					while($q=mysql_fetch_array($arr))
					echo "<td><b><a href='index.php?table=".$table."&amp;mode=sql&amp;order=".$q[0]."&amp;sql=".chek($sql)."&amp;ord=ASC'>".$q[0]."</a>
					".($ord!='ASC' ? "<a href='index.php?table=".$table."&amp;mode=sql&amp;order=".$q[0]."&amp;sql=".chek($sql)."&amp;ord=ASC'><img src='i/asc.png' /></a>" : "<a href='index.php?table=".$table."&amp;mode=sql&amp;sql=".chek($sql)."&amp;order=".$q[0]."&amp;ord=DESC'><img src='i/desc.png' /></a>")."</b></td>";
					
					echo "</tr>";
					
				$total=mysql_result(mysql_query("SELECT COUNT(*) FROM `".$table."` WHERE ".$sql),0);
				if($total>0){
				
					$quert=mysql_query("SELECT * FROM `".$table."` WHERE ".$sql." ".$order_q." LIMIT ".$start.",10");	
						
						$get=0;
						while($arr=mysql_fetch_array($quert)){
						echo "<tr class='".($get%2 ? "tr1" : "tr2")."' align='center'>";
						
							for($i=0;$i<$for_count;$i++)
							echo "<td>".chek(mb_substr($arr[$i],0,100,'UTF-8'))."</td>";
						
						echo "</tr>";
						$get++;
						}		
							}
							else
							echo "Таблица<br/>пуста";
							
							
				echo "</table>";
				if ($total > 10){
							echo str_pages('index.php?table='.$table.'&amp;mode=browse&amp;'.($order!=NULL ? "order=".$order."&amp;" : "").''.($ord!=NULL ? "ord=".$ord."&amp;" : ""), $start, $total, 10);
   echo '<form method="post" action="index.php?table='.$table.'&amp;mode=browse'.($order!=NULL ? "&amp;order=".$order : "").''.($ord!=NULL ? "&amp;ord=".$ord : "").'">
	Стр. <input type="text" name="start" value="'.(($start+10)/10).'" size="2"/>
	<input type="submit" name="go" value="&gt;&gt;" />
	</form>';}
		
				
				
			}

	}
?>
