<? function db_connect($user="root", $password="", $host="localhost") { $link = mysql_connect($host, $user, $password) or die("can't connect to db"); mysql_select_db($table) or die ("can't select db"); }
function page() { if(empty($_GET['page'])) { $page = 0; } else { if (is_numeric($_GET['page'])) { $page = $_GET['page']; } else { die("Incorrect page value"); } } return $page; } function navigation($onpage, $page, $table) { $onpage = 10; $query_count = mysql_query("select count(*) from `information` where `message` LIKE ('$fast_search%') OR `title` LIKE ('$fast_search%') OR `city` LIKE ('$fast_search%') OR `country` LIKE ('$fast_search%')"); $count = mysql_result($query_count,0,0); $pages = ceil($count/$onpage); if ($page != 0) { $prev ="<a href=\"?page=".($page-1)."\"><</a>"; } else { $prev ="<"; } if($page<round($pages-1)){ $next ="<A HREF=\"?page=".($page+1)."\">></A>"; } else { $next =">"; } for($i=0;$i<$pages;$i++) { if($i==$page){ $return.="[".($i+1)."]"; } else { $return.="<a href=\"".$_SERVER['PHP_SELF']."?page=".$i."\">".($i+1)."</a>"; } } return $prev.$return.$next; }
function search_query($onpage, $page, $table) { $onpage=10; $begin = $page*$onpage; echo $begin; $query ="select * from `information` where `message` LIKE ('%$fast_search%') OR `title` LIKE ('%$fast_search%') OR `city` LIKE ('%$fast_search%') OR `country` LIKE ('%$fast_search%') ORDER by city DESC LIMIT $begin, $onpage"; $result = mysql_query($query) or die(mysql_error()); return $result; }
<? if ($_POST or $_GET) { $fast_search = $_POST['fast_search']; $fast_search = substr($fast_search, 0, 64); $fast_search = trim(htmlspecialchars($fast_search)); $fast_search = ereg_replace(" +", " ", $fast_search); $host = "localhost"; $user = "root"; $password = ""; $link = mysql_connect($host, $user, $password) or die("Íåìîæëèâî ï³äêëþ÷èòèñü äî ÁÄ"); mysql_select_db(catalog) or die ("Íåìîæëèâî âèáðàòè òàáëèöþ"); $per_page=10; if (isset($_GET['page'])) $page=($_GET['page']-1); else $page=0; $start=abs($page*$per_page); $q="SELECT * FROM `information` where `message` LIKE ('$fast_search%') OR `title` LIKE ('$fast_search%') OR `city` LIKE ('$fast_search%') OR `country` LIKE ('$fast_search%') LIMIT $start,$per_page"; $res=mysql_query($q); while($row=mysql_fetch_array($res)) { ?> <table border=0 width="400"> <tr> <td> <b><a href=search.php?open=<?echo $row[id]; ?>><? echo $row[title]; ?><hr id="hr"></a></b> </td> </tr> <tr> <td><? echo substr($row[message], 0, 128)."..."; ?></td> </tr> <? }
$q="SELECT count(*) FROM `information` where `message` LIKE ('$fast_search%') OR `title` LIKE ('$fast_search%') OR `city` LIKE ('$fast_search%') OR `country` LIKE ('$fast_search%')"; $res=mysql_query($q); $row=mysql_fetch_row($res); $total_rows=$row[0]; $num_pages=ceil($total_rows/$per_page); for($i=1;$i<=$num_pages;$i++) { if ($i-1 == $page) { echo $i." "; } else { echo '<a href="'.$_SERVER['PHP_SELF'].'?page='.$i.'">'.$i."</a> "; } } }