Ik ben al een tijdje opzoek nar een (het liefst) kant en klaar script waarmee ik, zoals de titel al aangeeft, een tabel kan weergeven, waarbij de gegevens gesorteerd kunnen worden en de resultaten over pagina's verdeeld kunnen worden, met behulp van paginanummers en vorige-volgende-linkjes.
Je leest vaak dat er veel kant en klare scripts op het internet rondzwerven. Echter ik kom ze niet tegen na dagen zoeken. Weet 1 van jullie misschien soort gelijken alternatieven die ik eens uit kan proberen. Of weet iemand de oplossing voor mijn probleem?
In mijn zoektocht kwam ik een script tegen ( drale_dbtableview http://blog.drale.com/mysql-table-viewer-with-pagination-and-sorting/ ) wat op eerste oog goed werkt als ik alleen mijn tabel wil weergeven. Echter als ik mijn zoekopdrachten er op los laat en op de tweede pagina van de resultaten wil bekijken krijg ik de complete tabel te zien en is hij schijnbaar de opgegeven zoekopdracht vergeten.
In zoeken2.php heb ik de zoektitel toegevoegd in de SQL query's. Verder heb ik in zoeken1.php geprobeerd een verborgen veld mee te geven met een sorteer opdracht om het probleem op te lossen, maar ook dit helpt niet. Ik ben nu de weg kwijt. Hoe kan ik dit script omtoveren naar een zoekopdracht weergave script?
Ik hoop dat iemand me hier mee kan helpen of een ander wel werkend alternatief weet. Anders hoop ik dat iemand anders nog wat aan dit script heeft, want het lijkt mij een heel mooi script.
zoeken.php
<?php session_start(); ?>
<HTML>
<head>
<title></title>
</head>
<BODY>
<?php
if(!isset($_SESSION["gebgn"]))
{
$text = "<A HREF='/test/registreren.php'>Registreer</A><BR>
<A HREF='/test/login.php'>Log in</A><BR>
<A HREF='/test/form/contact.php'>Contact</A>";
echo($text);
}
else
{
?>
<!--
begin van de beveiligde inhoud
-->
Welkom, <font color="red"> <?php echo($_SESSION["gebgn"]); ?></font>
<A HREF="/test/loguit.php">Log hier uit!</A>
<BR>
<BR>
Dit is informatie die alleen zichtbaar is voor ingelogde gebruikers
<BR>
<BR>
<A HREF="/test/index.php">HOME</A><BR>
<A HREF="/test/zoeken.php">Zoeken</A><BR>
<A HREF="/test/verkopen.php">Verkopen</A><BR>
<A HREF="/test/form/contact.php">Contact</A><BR>
<br>
<A HREF="/test/registreren.php">Registreren</A><BR>
<A HREF="/test/alles.php">Alle boeken weergeven</A><BR>
<BR><BR>
<form method="post" action="zoeken2.php">
<input type ="text" name="titel"><BR>
<!-- <input type ="hidden" name="orderby" value="bvraagprijs"> toegevoegd voor drale_dbtableview -->
<input type ="submit" value ="Zoeken">
<input type ="reset" value ="Wissen">
</form>
<!--
einde van de beveiligde inhoud
-->
<?php
}
?>
</BODY>
</HTML>
zoeken2.php
<?php
/*
Drale DBTableViewer v100123
Copyright 2009 http://www.drale.com
Darrell is not responsible for any abnormal behavior of this program
This file is part of Drale DBTableViewer.
Drale DBTableViewer is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Drale DBTableViewer is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Drale DBTableViewer. If not, see <http://www.gnu.org/licenses/gpl.txt>.
drale.com is not responsible for any abnormal behavior of this program
******************************************
version 100123:
rebuilt columnSortArrows()
included tableviewdemo dummy data
version 100120:
added mysql_real_escape_string
only allow ASC and DESC in $_GET['sort']
version 100119:
initial build
*/
$zoektitel = $_POST['titel'];
?>
<!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>
<title></title>
<link rel="stylesheet" type="text/css" href="reset.css" />
<link rel="stylesheet" type="text/css" href="typography.css" />
<link rel="stylesheet" type="text/css" href="style.css" />
</head>
<body>
<!--<div align="center"><font color="grey" size="6">Zoekresultaten voor <?php #echo $zoektitel ?>:</font></div>-->
<?php
//DATABASE SETTINGS
require_once 'connection.php';
$config['table'] = "boeken";
$config['nicefields'] = true; //true or false | "Field Name" or "field_name"
$config['perpage'] = 2;
$config['showpagenumbers'] = true; //true or false
$config['showprevnext'] = true; //true or false
/******************************************/
//SHOULDN'T HAVE TO TOUCH ANYTHING BELOW...
//except maybe the html echos for pagination and arrow image file near end of file.
include './Pagination.php';
$Pagination = new Pagination();
//CONNECT
//get total rows
$totalrows = mysql_fetch_array(mysql_query("SELECT count(*) as total FROM `".$config['table']."` WHERE titel LIKE '%$zoektitel%' "));
//limit per page, what is current page, define first record for page
$limit = $config['perpage'];
if(isset($_GET['page']) && is_numeric(trim($_GET['page']))){$page = mysql_real_escape_string($_GET['page']);}else{$page = 1;}
$startrow = $Pagination->getStartRow($page,$limit);
//create page links
if($config['showpagenumbers'] == true){
$pagination_links = $Pagination->showPageNumbers($totalrows['total'],$page,$limit);
}else{$pagination_links=null;}
if($config['showprevnext'] == true){
$prev_link = $Pagination->showPrev($totalrows['total'],$page,$limit);
$next_link = $Pagination->showNext($totalrows['total'],$page,$limit);
}else{$prev_link=null;$next_link=null;}
//IF ORDERBY NOT SET, SET DEFAULT
if(!isset($_GET['orderby']) OR trim($_GET['orderby']) == ""){
//GET FIRST FIELD IN TABLE TO BE DEFAULT SORT
$sql = "SELECT * FROM `".$config['table']."` WHERE titel LIKE '%$zoektitel%' LIMIT 1";
$result = mysql_query($sql) or die(mysql_error());
$array = mysql_fetch_assoc($result);
//first field
$i = 0;
foreach($array as $key=>$value){
if($i > 0){break;}else{
$orderby=$key;}
$i++;
}
//default sort
$sort="ASC";
}else{
$orderby=mysql_real_escape_string($_GET['orderby']);
}
//IF SORT NOT SET OR VALID, SET DEFAULT
if(!isset($_GET['sort']) OR ($_GET['sort'] != "ASC" AND $_GET['sort'] != "DESC")){
//default sort
$sort="ASC";
}else{
$sort=mysql_real_escape_string($_GET['sort']);
}
//GET DATA
$sql = "SELECT * FROM `".$config['table']."` WHERE titel LIKE '%$zoektitel%' ORDER BY $orderby $sort LIMIT $startrow,$limit";
$result = mysql_query($sql) or die(mysql_error());
//START TABLE AND TABLE HEADER
echo "<table>\n<tr>";
$array = mysql_fetch_assoc($result);
foreach ($array as $key=>$value) {
if($config['nicefields']){
$field = str_replace("_"," ",$key);
$field = ucwords($field);
}
$field = columnSortArrows($key,$field,$orderby,$sort);
echo "<th>" . $field . "</th>\n";
}
echo "</tr>\n";
//reset result pointer
mysql_data_seek($result,0);
//start first row style
$tr_class = "class='odd'";
//LOOP TABLE ROWS
while($row = mysql_fetch_assoc($result)){
echo "<tr ".$tr_class.">\n";
foreach ($row as $field=>$value) {
echo "<td>" . $value . "</td>\n";
}
echo "</tr>\n";
//switch row style
if($tr_class == "class='odd'"){
$tr_class = "class='even'";
}else{
$tr_class = "class='odd'";
}
}
//END TABLE
echo "</table>\n";
if(!($prev_link==null && $next_link==null && $pagination_links==null)){
echo '<div class="pagination">'."\n";
echo $prev_link;
echo $pagination_links;
echo $next_link;
echo '<div style="clear:both;"></div>'."\n";
echo "</div>\n";
}
/*FUNCTIONS*/
function columnSortArrows($field,$text,$currentfield=null,$currentsort=null){
//defaults all field links to SORT ASC
//if field link is current ORDERBY then make arrow and opposite current SORT
$sortquery = "sort=ASC";
$orderquery = "orderby=".$field;
if($currentsort == "ASC"){
$sortquery = "sort=DESC";
$sortarrow = '<img src="arrow_up.png" />';
}
if($currentsort == "DESC"){
$sortquery = "sort=ASC";
$sortarrow = '<img src="arrow_down.png" />';
}
if($currentfield == $field){
$orderquery = "orderby=".$field;
}else{
$sortarrow = null;
}
return '<a href="?'.$orderquery.'&'.$sortquery.'">'.$text.'</a> '. $sortarrow;
}
?>
</body>
</html>
Pagination.php
<?php
//source unknown for logic of showPageNumbers()
//modified by drale.com - 1-19-2010
//added query_string reproduction and divs
//added showNext() and showPrev()
class Pagination {
function getStartRow($page,$limit){
$startrow = $page * $limit - ($limit);
return $startrow;
}
function showPageNumbers($totalrows,$page,$limit){
$query_string = $this->queryString();
$pagination_links = null;
/*
PAGINATION SCRIPT
seperates the list into pages
*/
$numofpages = $totalrows / $limit;
/* We divide our total amount of rows (for example 102) by the limit (25). This
will yield 4.08, which we can round down to 4. In the next few lines, we'll
create 4 pages, and then check to see if we have extra rows remaining for a 5th
page. */
for($i = 1; $i <= $numofpages; $i++){
/* This for loop will add 1 to $i at the end of each pass until $i is greater
than $numofpages (4.08). */
if($i == $page){
$pagination_links .= '<div class="page-link"><span>'.$i.'</span></div> ';
}else{
$pagination_links .= '<div class="page-link"><a href="?page='.$i.'&'.$query_string.'">'.$i.'</a></div> ';
}
/* This if statement will not make the current page number available in
link form. It will, however, make all other pages available in link form. */
} // This ends the for loop
if(($totalrows % $limit) != 0){
/* The above statement is the key to knowing if there are remainders, and it's
all because of the %. In PHP, C++, and other languages, the % is known as a
Modulus. It returns the remainder after dividing two numbers. If there is no
remainder, it returns zero. In our example, it will return 0.8 */
if($i == $page){
$pagination_links .= '<div class="page-link"><span>'.$i.'</span></div> ';
}else{
$pagination_links .= '<div class="page-link"><a href="?page='.$i.'&'.$query_string.'">'.$i.'</a></div> ';
}
/* This is the exact statement that turns pages into link form that is used above */
} // Ends the if statement
return $pagination_links;
}
//added by drale.com - 1-19-2010
function showNext($totalrows,$page,$limit,$text="next »"){
$next_link = null;
$numofpages = $totalrows / $limit;
if($page < $numofpages){
$page++;
$next_link = '<div class="page-link"><a href="?page='.$page.'&'.$query_string.'">'.$text.'</a></div>';
}
return $next_link;
}
function showPrev($totalrows,$page,$limit,$text="« prev"){
$next_link = null;
$numofpages = $totalrows / $limit;
if($page > 1){
$page--;
$prev_link = '<div class="page-link"><a href="?page='.$page.'&'.$query_string.'">'.$text.'</a></div>';
}
return $prev_link;
}
function queryString(){
//matches up to 10 digits in page number
$query_string = eregi_replace("page=[0-9]{0,10}&","",$_SERVER['QUERY_STRING']);
return $query_string;
}
}
?>