Ocena wątku:
  • 0 głosów - średnia: 0
  • 1
  • 2
  • 3
  • 4
  • 5
Podział na strony + Wyszukiwarka
#1
Wink 
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?
Odpowiedz


Podobne wątki…
Wątek: Autor Odpowiedzi: Wyświetleń: Ostatni post
  podział danych za pomocą substr ppsqm 1 4,148 18-06-2014, 15:18
Ostatni post: Kartofelek
  [MySQL] wyszukiwarka problem darlowiak 3 3,556 13-06-2013, 12:58
Ostatni post: darlowiak
Exclamation [PHP, MySQL] Wyszukiwarka autobusów sebekzosw 1 2,221 06-09-2011, 22:51
Ostatni post: Pedro84
  Wyszukiwarka samochodów - problem ppg 2 2,651 28-03-2011, 23:35
Ostatni post: Pedro84
  Wyszukiwarka rekordów. zewoj 1 2,327 12-03-2011, 16:28
Ostatni post: www.skrypty.pro

Skocz do:


Użytkownicy przeglądający ten wątek:
Sponsorzy i przyjaciele
SeoHost.pl