Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: Porcjowanie zapytań w bazie mysql
Forum PHP.pl > Forum > PHP
sendi16
w jaki sposob mozna podzielic wynyki np. wyszukiwania na kilka stron
QbAWoLNY
tutaj cos o tym mas http://www.webserwis.org/?id=p31
ale ja to zrobilem inaczej:
Cytat
$zap = "select data from ksiega";
$wynik = mysql_query($zap);
$ogolem = mysql_num_rows($wynik);
if (!$limit)
{
$limit = "LIMIT 0, 25";
}
$zap = "select data, imie, email, www, wpis from ksiega order by data desc ".$limit;
$wynik = mysql_query($zap);
$ile = mysql_num_rows($wynik);

$liczba = $ogolem / 25;
$liczba = $liczba + 0.5;
$liczba = (int)$liczba;
for ($i=0; $i<$liczba; $i++)
{
$limitek = $i * 25;
$limitek2 = $limitek + 25;
$limitek3 = $limitek+1;
if ($limitek2 > $ogolem)
{
$limitek2 = $ogolem;
}
echo "<A href="?id=ksiega&limit=LIMIT $limitek, $limitek2">$limitek3 - $limitek2</A> ";
}

for($i=1; $i <= $ile; $i++)
{
//wyswietlanie
}
}
sendi16
thanks ale jest taki maly problem.
ten skrypt wyswietla mi tylko pierwsze 25 rekordow a pozostalych nie na osobnych stronach
DeyV
W tym tygodniu po raz 4 to piszę sad.gif. SZUKAJ - "porcjowanie wyników" - i bardzo dobre, działające rozwiązania są Twoje
scanner
DeyV: ciesz się.. tydzień się kończy.
Teodor
witam
ja zrobilem to tak:
[php:1:ea2d968f5b]<?php
/*
$news_poz - to ile pozycji na strone
$s - to pozycja od ktorej zaczynasz wyswietlanie
*/
$news_poz=10;
if(!$s){
$s="0";
}
$connection = mysql_connect($config["host"],$config["user"],$config["password"]) or die( db_error() );
$db = mysql_select_db($config["database"],$connection);
$sql = "SELECT id, dodal, email, data, temat, wiad FROM news ORDER BY id DESC LIMIT $s,$news_poz";
$sql_result = mysql_query($sql,$connection);
list($ile)=mysql_fetch_row(mysql_db_query($config["database"],"SELECT count(*) FROM news"));
/* teraz wyswietlasz to co pobrales z bazy */
while ($row = mysql_fetch_array($sql_result)) {
$dodal = $row["dodal"];
$data = $row["data"];
$temat= $row["temat"];
$nr = $row["id"];
$wiad= $row["wiad"];
$email = $row["email"];
echo"$dodal - $data - $temat -".nl2br( $wiad)." - $email";
}
/* a ponizej generowanie linkow do nastepnych stron */
for($s=0; $s<$ile; $s+=$news_poz) {
$y=($ile-$s)/$news_poz;
$x=ceil($y);
echo"<a href="stronki.php?s=$s">$x</a> "; #to wyswietli linki w postaci 4 3 2 1
}
/* LUB */
for($s=0; $s<$ile; $s+=$news_poz) {
$z=$s/$news_poz+1;
echo"<a href="stronki.php?s=$s">$z</a> "; #to wyswietli linki w postaci 1 2 3 4
}


?>[/php:1:ea2d968f5b]
To jest dosc proste wyswietlanie - mozna sie jeszcze pobawic i link do strony na ktorej sie jest zrobic nieaktywnym, dodac linki nastepna/poprzednia itp.
-----------------
wszelkie komentarze z checia przyjme - poczatkujacy jestem i do tego samouk tongue.gif
sendi16
wszystko dziala oprocz jednej rzeczy.
wyswietlania linkow na nastepne strony.
poradzicie cos na to??
stal-sw
Jak zauwazylem bardzo wiele osob ma problemy z porcjowaniem wynikow z bazy danych (ja tez z tym mialem problem).
Ale po dlugich poszukiwaniach znalazlem bardzo ciekawa klase do porcjowania (na http://www.phpclasses.org/)
Sklada sie ona z 2 plikow:
page.inc.php (tego pliku nie trzeba nic modyfikowac)
example.php (ten plik wyswietla rezultaty z bazy i je porcjuje)

Mysle ze kazdy majac te pliki poradzi sobie z porcjowaniem wynikow !

page.inc.php
[php:1:29cdbc21f1]
<?php
/**
* A Class use to Mysql page
*
* @author Avenger <avenger@php.net>
* @version 1.02
* @update 2003-04-27 23:11:29
*
*
* Useage:
* $p = new show_page; // Create the Object
* $p->file="ttt.php"; // Set used the class name,default to the PHP_SELF
* $p->pvar="pagecount"; // Set the page parameter,default to "p"
* $p->setvar(array("a" => '1', "b" => '2')); // Set the parameter u wanta pass, Note:used it above the $p->set
* $p->set(20,2000,1); // Set the object parameter,total 3,1st is the pagesize,2nd is the total record count,3rd is the currerent page count,default is will auto read the GET variable
* $p->output(0); // Output,the parameter is true will return a string
* echo $p->limit(0); // Out put the limit.eg. "SELECT * FROM TABLE LIMIT {$p->limit()}"; set it true,will return a array
*
*/
class show_page {
/**
* output
*
* @var string
*/
var $output;
/**
* used the class's page
*
* @var string
*/
var $file;
/**
* page pass parameter
*
* @var string
*/
var $pvar = "p";
/**
* pagesize
*
* @var integer
*/
var $psize;
/**
* currerent page
*
* @var ingeger
*/
var $curr;
/**
* pass array
*
* @var array
*/
var $varstr;
/**
* total page count
*
* @var integer
*/
var $tpage;
/**
* page set
*
* @access public
* @param int $pagesize The pagesize
* @param int $total The toal records count
* @param int $current Current page,keep empty will auto read the get variable
* @return void
*/
function set($pagesize=20,$total,$current=false) {
global $HTTP_SERVER_VARS,$HTTP_GET_VARS;
$this->tpage = ceil($total/$pagesize);
if (!$current) {$current = $HTTP_GET_VARS[$this->pvar];}
if ($current>$this->tpage) {$current = $this->tpage;}
if ($current<1) {$current = 1;}
$this->curr = $current;
$this->psize = $pagesize;
if (!$this->file) {
$this->file = $HTTP_SERVER_VARS['PHP_SELF'];
}
strstr($this->file,'?') ? $middle = '&amp;' : $middle = '?';
if ($this->tpage > 1) {
if ($current>10) {
$this->output.='<a class="en1" href='.$this->file.$middle.$this->pvar.'='.($current-10).($this->varstr).' title="Previous 10">&lt;&lt;&lt;</a>&nbsp;';
}
if ($current>1) {
$this->output.='<a class="en1" href='.$this->file.$middle.$this->pvar.'='.($current-1).($this->varstr).' title="Previous">&lt;&lt;</a>&nbsp;';
}
$start = floor($current/10)*10;
$end = $start+9;
if ($start<1) $start=1;
if ($end>$this->tpage) $end=$this->tpage;
for ($i=$start; $i<=$end; $i++) {
if ($current==$i) {
$this->output.='<font color="red" class="en1">'.$i.'</font>&nbsp;';
} else {
$this->output.='<a class="en1" href="'.$this->file.$middle.$this->pvar.'='.$i.$this->varstr.'">['.$i.']</a>&nbsp;';
}
}
if ($current<$this->tpage) {
$this->output.='<a class="en1" href='.$this->file.$middle.$this->pvar.'='.($current+1).($this->varstr).' title="Next">&gt;&gt;</a>&nbsp;';
}
if ($this->tpage>10 && ($this->tpage-$current)>=10 ) {
$this->output.='<a class="en1" href='.$this->file.$middle.$this->pvar.'='.($current+10).($this->varstr).' title="Next 10">&gt;&gt;&gt;</a>';
}
}
}
/**
* passed variable set
*
* @access public
* @param array $data The parameter u wanta passed,see the example above.
* @return void
*/
function setvar($data) {
foreach ($data as $k=>$v) {
$this->varstr.='&amp;'.$k.'='.urlencode($v);
}
}
/**
* Output
*
* @access public
* @param bool $return Set it true will return a string,otherwish will output automatic
* @return string
*/
function output($return = false) {
if ($return) {
return $this->output;
} else {
echo $this->output;
}
}
/**
* Make the limit
*
* @access public
* @return string
*/
function limit($arr=0) {
if ($arr) {
settype($arr,"array");
$arr[1] = ($this->curr-1)*$this->psize;
$arr[2] = $this->psize;
return $arr;
} else {
return (($this->curr-1)*$this->psize).','.$this->psize;
}
}
} //End Class
?>[/php:1:29cdbc21f1]

example.php
[php:1:29cdbc21f1]
<?php
/**
* A Example to use show_page class
*
* @author Avenger <avenger@php.net>
* @version $Id 2003-04-30 9:09:29 $
*/
/*
At first,assume we have a table like this:
table: user
Records:
Id Name Sex Age
====================================
1 Jim M 18
2 Bill M 26
3 Ted M 30
4 Mary F 16
5 Avenger M 19
6 Meow F 12
U can run the SQL below create the test database automatic
CREATE TABLE `user` (`Id` int(11) NOT NULL auto_increment,`Name` varchar(100) NOT NULL default '',`Sex` enum('M','F') default NULL,`Age` smallint(3) default NULL,PRIMARY KEY (`Id`)) TYPE=MyISAM;
INSERT INTO `user` VALUES("1", "Jim", "M", "18");
INSERT INTO `user` VALUES("2", "Bill", "M", "26");
INSERT INTO `user` VALUES("3", "Ted", "M", "30");
INSERT INTO `user` VALUES("4", "Mary", "F", "16");
INSERT INTO `user` VALUES("5", "Avenger", "M", "19");
INSERT INTO `user` VALUES("6", "Meow", "F", "12");
Note: Change the username and password to your's in the above script part of connect database.
*/
require_once 'page.inc.php';
$pg = new show_page;
$conn = mysql_connect("localhost", "pawel", "xx") or die("Could not connect: " . mysql_error());
mysql_select_db("xx") or die(mysql_error());
// Recive the order variable
$_GET['order'] ? $order=$_GET['order'] : $order='Id';
// Count the total records
$result = mysql_query("SELECT COUNT(*) FROM user") or die("Invalid query: " . mysql_error());
$total_records = mysql_result($result,0);
// Page Set
$_GET['pagesize'] ? $pagesize=$_GET['pagesize'] : $pagesize = 2;
$pg->setvar(array('order'=>$order,'pagesize'=>$pagesize));
$pg->set($pagesize,$total_records);
/* u can used this script to limit too:
$limit = $pg->limit(1);
$SQL = "SELECT * FROM user ORDER BY $order LIMIT {$limit[1]},{$limit[2]}";
*/
$SQL = "SELECT * FROM user ORDER BY $order LIMIT ".$pg->limit();
$result = mysql_query($SQL) or die("Invalid query: " . mysql_error());
echo "<h1>Galeria</h1>";
$pg->output();
echo "<table border='1' width='300'>n";
echo "<tr><th><a href='example.php?order=Id&pagesize=$pagesize'>Id</a></th><th><a href='example.php?order=Name&pagesize=$pagesize'>Name</a></th><th><a href='example.php?order=Sex&pagesize=$pagesize'>Sex</a></th><th><a href='example.php?order=Age&pagesize=$pagesize'>Age</a></th></tr>n";
while ($row = mysql_fetch_object($result)) {
printf("<tr><td>%d</td><td>%s</td><td>%s</td><td>%s</td></tr>", $row->Id,$row->Name,$row->Sex,$row->Age);
}
echo "</table><br>";
echo "<b>Result:</b><br>n";
echo "<blockquote>
TotalRecord:{$total_records}<br>
TotalPage:{$pg->tpage}<br>
PageSize:{$pg->psize}<br>
CurrerentPage:{$pg->curr}<br>
PassedParameter:
";
print_r($pg->varstr);
echo "</blockquote>";
echo "<form action=''>Change pagesize:<input type='text' name='pagesize' size='2' value='{$pagesize}'> <input type='submit' value='change'></form>";
echo "<hr>";
$pg->output();
mysql_close($conn);
?>[/php:1:29cdbc21f1]
Indianin
[php:1:44852750b1]<?php

$page = $_REQUEST['page'];
$query = $_POST['query'];



// check to see if $page is set
if (!$page) {
$page = 1;
}

// Change $query to a request super global after the first page.
if($page > 1){
$query = $_REQUEST['query'];
}


//set up some limits
$limit = $page * 10;
$limit = $limit - 10;

//get the count from the database table
$sql_num = mysql_query("SELECT * FROM table WHERE column LIKE %query%");
$num = mysql_num_rows($sql_num);

// query your database for the final results
$sql_results = mysql_query("SELECT something FROM table WHERE column LIKE %query% LIMIT $limit,10");


if($num < 10){
$through = $num;
} else {
$through = $limit + 10;
}
if ($through > $num){
$through = $total;
}

if($page > 1){
$from = $limit +1;
} else {
$from = $limit;
}
if($from == 0){
$from = $from +1;
}

echo "Search Results";
echo "<p align="right">";
if ($page > 1) {
echo "<a
href="$PHP_SELF?query=$query&page=".($page -1)."">Previous</a>&nbsp;&nbsp;";
}

if (($num > 10) && (($limit + 10) < $num)) {
echo "<a href="$PHP_SELF?query=$query&page=".($page +1)."">Next</a>";
}

// Build the list from the $sql query above and display results

while(list($results)=mysql_fetch_array($sql_results)){
echo "Results: $results";
}

if ($page > 1) {
echo "<a
href="$PHP_SELF?query=$query&page=".($page -1)."">Previous</a>&nbsp;&nbsp;";
}

if (($num > 10) && (($limit + 10) < $num)) {
echo "<a href="$PHP_SELF?query=$query&page=".($page +1)."">Next</a>";
}

?>[/php:1:44852750b1]
To jest wersja lo-fi głównej zawartości. Aby zobaczyć pełną wersję z większą zawartością, obrazkami i formatowaniem proszę kliknij tutaj.
Invision Power Board © 2001-2025 Invision Power Services, Inc.