Podział na strony + Wyszukiwarka - Wersja do druku
+- Forum Webmastera, HTML, CSS, PHP, MySQL, Hosting, Domeny - Forum dla Webmasterów (https://www.webmastertalk.pl)
+-- Dział: Technologie internetowe - tworzenie stron WWW (https://www.webmastertalk.pl/forum-technologie-internetowe-tworzenie-stron-www)
+--- Dział: Programowanie, Bazy danych (https://www.webmastertalk.pl/forum-programowanie-bazy-danych)
+--- Wątek: Podział na strony + Wyszukiwarka (/thread-podzial-na-strony-wyszukiwarka)
|
Podział na strony + Wyszukiwarka - seba199696 - 31-10-2011
Cześć mam problem z podziałem na strony... wyszukiwarka działa dobrze lecz nie podział. Daję kod:
index. php
Kod: <?php
include 'func.inc.php';
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Search</title>
</head>
<body>
<h2> Search </h2>
<form action='' method="post">
<p>
<input type="text" name="keywords"/> <input type="submit" value="Search"/>
</p>
</form>
<?php
if (isset($_POST["keywords"]))
{
$keywords = mysql_real_escape_string(htmlentities(trim($_POST['keywords'])));
$suffix = "";
$errors = array();
if (empty($keywords)){
$errors[] = '<p><em> - Please enter a search term</em></p>';
} else if (strlen($keywords)<3){
$errors[] = '<p><em> - Your search term must be three or more characters</em></p>';
} else if (search_results($keywords) === false){
$errors[] = '<p><em> - Your search returned no results</em></p>';
}
if (empty($errors))
{
$results = search_results($keywords);
$results_num = count($results) - 1;
$suffix = ($results_num != 1) ? 's' : '';
echo '<p>Your search for <strong>', $keywords, '</strong> returned <strong>', $results_num, ' </strong> result', $suffix,'</p>';
foreach($results as $result)
{
echo '<p><strong><a href="',$result['url'],'">', $result['title'], '</a></strong><br>', $result['description'], '... <br>', $result['url'],'... <br>';
}
if($results['pages'] >= 1)
{
for ($x = 1; $x<=$results['pages']; $x++)
{echo '<a href="?page='.$x.'">'.$x.'</a>';}
}
else
{
foreach($errors as $error)
{echo $error, '</br>';}
}
}
}
?>
</body>
</html>
func.inc.php
Kod: <?php
include 'db.php'; // connect
function search_results($keywords){
//code in here...
$returned_results = array();
$where = "";
$keywords = preg_split("/[\s]+/", $keywords);
$total_keywords = count($keywords);
foreach($keywords as $key=>$keyword){
$where .= "`keywords` LIKE '%$keyword%'";
if($key != ($total_keywords - 1)){
$where .= " AND ";
}
}
$per_page = 2;
$page = (isset($_GET['page'])) ? (int)$_GET['page']: 1;
$start = ($page -1) * $per_page;
$results = "SELECT `title`, LEFT(`description`, 70) as `description`, `url` FROM `articles` WHERE $where LIMIT $start, $per_page";
$results_num = ($results = mysql_query($results) or die(mysql_error())) ? mysql_num_rows($results): 0;
if ($results_num === 0) {
return false;
}
else
{
$pages_query = mysql_query("SELECT COUNT(`article_id`) FROM `articles`");
$pages = ceil(mysql_result($pages_query, 0) / $per_page);
while ($results_row = mysql_fetch_assoc($results))
{ $returned_results[] = $results_row; }
$returned_results['pages'] = $pages;
return $returned_results;
}
}
?>
Co jest nie tak? Dlaczego stronicowanie nie działa?
|