[PHP] Meerdere pagina's maken van een mysql query

Pagina: 1
Acties:
  • 55 views sinds 30-01-2008

  • Godjira
  • Registratie: Februari 2003
  • Laatst online: 16-05 15:20

Godjira

To infinity and beyond!

Topicstarter
Al enige tijd ben ik bezig met ontwikkelen van mijn pagina, geheel in PHP. Ik ben al een behoorlijk eind gekomen, maar nu kom ik er toch echt niet meer uit.

Even korte uitleg over wat de bedoeling precies is:
Ik heb een dbase met daarin al mijn DVD's die ik bezit. Deze heb ik netjes in een mysql query laten uitlezen zodat je netjes alles op een rij in een tabel te zien krijgt, zoals je hier.

Nu wil ik het allemaal wat overzichtelijker maken door het totale aantal DVD's te verspreiden over meerdere pagina's met bijvoorbeeld 20 rows per pagina. Goed, ik ben zover dat mijn script uitrekend hoeveel pagina's er nodig zijn, laat dat ook netjes zien (kijk maar hier, maar let niet op de lay-out, het is puur een test, de lay-out komt in de final versie van het script erbij).

Ik heb gezocht op een aantal PHP sites, HotScripts (voorbeelden), Google en de MySQL site, maar ik kom er niet uit. Waarschijnlijk maak ik gewoon een hele domme en simpele fout, maar goed.

Hieronder de code van mijn pagina:
PHP:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
<?php

#Include the sqlconnect.php file to be able to connect to the database.
    include ("../sqlconnect.php");


#Connecting to the database.
        $db = mysql_connect($db_hostname,$db_user,$db_password);
        mysql_select_db($db_database);

#Setting the type.
        $type = "DVD Movies";

#Start creating the website.
            echo "<html>";
            echo "<head><title>" . $type . "</title><link rel='stylesheet' type='text/css' href='../scripts/main.css'></head>";
            echo "<body>";

#Make the heading.
            echo "<center><font size='3'><b>" . $type . "</b></font><br><br>";

#Start creating the table we want to put all the data in.
            echo "<table width='950' border='1' cellspacing='0' cellpadding='0' 
bordercolor='#858585' style='border-collapse: collapse; border-style: solid; 
border-width: 1px'>";

#Make the table headers and links to be able to sort the data.
            echo "<tr><td class='head'><a class='top' 
href='dvd.php?order=Title'><b>Title</b></a></a></td>";
            echo "<td class='head'><a class='top' 
href='dvd.php?order=Publisher'><b>Publisher</b></a></td>";
            echo "<td class='head'><a class='top' 
href='dvd.php?order=Director'><b>Director</b></a></td>";
            echo "<td class='head'><b>Release date</b></td></tr>";

#Set the default ordering.
    if (isset($_GET["order"])){
        $order = $_GET["order"];
        } else {
        $order = "Title";
        }

#Syntaxes to use in the query.
        $page_count = 20; #Results per page
        $cut_off = 10; #Pages displayed at once
        if(!isset($start))$start = 0;

#Start retreiving the information wanted out of the database.
        $result = mysql_query("SELECT * FROM `" . $type . "` ORDER BY `" . $order . "`");
#       $result = mysql_query("SELECT * FROM `" . $type . "` DESC LIMIT $start, $page_count"`);
    if (!$result) {
        die('Invalid query: ' . mysql_error());
        }
        $num = mysql_num_rows($result);

#Get todays date, used to make a diffrence between pre-orderd and owned dvd's.
        $currentdate = date("Y-m-d");

#Retreive information from our database
    while ($data = mysql_fetch_assoc($result)) {
        $ID = $data["ID"];
        $title = $data["Title"];
        $publisher = $data["Publisher"];
        $director = $data["Director"];
        $release = $data["Release date"];

#Make the date in our own wanted format dd/mm/yyyy
        $data = explode ("-", $release);

#Replace unsopported caracters from the data, so the browsers will know what we
 mean.
        $img = str_replace("'", "", $title);
        $img = str_replace("?", "", $img);
        $img = str_replace("&", "%26", $img);
        $img = str_replace(":", "%20-", $img);

#Remove all the slashes in the image names.
        $img = stripslashes($img);

#Start creating our table which contains all the data we retreived from the 
database.
            echo "<tr><td class='list'><a class ='alist' href='details_dvd.php?ID=" 
. $ID . "' title='Click to view the details'>"  . $title . "</a></td><td class='list'>" . 
$publisher . "</td><td class='list'>" . $director . "</td><td class='list'>" . $data[2] .
 "-" . $data[1] . "-" . $data[0] . "</td>";
            }
            echo "</table></center>";

#Function to calculate the total amount of needed pages and create the links.
    function pagination($page_count,$num,$start,$PHP_SELF,$cut_off){

    $newnum = $num / $page_count;
    $newnum = ceil($newnum);
        if(!isset($page))$page = 1;

        if($newnum >= 2){
            echo "<table border=\"0\" cellpadding=\"3\" cellspacing=\"0\" 
bgcolor=\"#eeeeee\" align=\"center\"><tr><td style=\"border: 0; font-family: 
Verdana; font-size: 11px;\" nowrap>";

        if(isset($start) && $start != 0){
            echo "&laquo;  <a href=\"$PHP_SELF?start="; echo $start - 
$page_count; echo "\">Back</a> ";
        }
        else{
            echo "&laquo; Back ";
        }

    $total_pages = $newnum;
        if($newnum > $cut_off)$newnum = $cut_off;

    $cur_page = ($start + $page_count) / $page_count;

        if($cur_page > $cut_off)$page = $cur_page - $cut_off + 1;

        if($cur_page > $cut_off){
    $start_page = $page * $page_count - $page_count;
    }
        else{
    $start_page = 0;
    }

    for($i=0; $i<$newnum;$i++){
        if($start == ($page * $page_count) - $page_count){
            echo "<b><font size='3' color='#000066\'>$page</font></b> ";
        }
        else{
            echo "<a href=\"$PHP_SELF?start=$start_page\">$page</a> ";
        }
            $page++;
            $start_page = $start_page + $page_count;
        }

        if($newnum >= 2 && $cur_page < $newnum && $cur_page <= $total_pages){
            echo " <a href=\"$PHP_SELF?start="; echo $start + $page_count; echo "\">Next</a> &raquo;";
            }
        elseif($cur_page >= $total_pages){
            echo " Next &raquo;";
            }
        else{
            echo " <a href=\"$PHP_SELF?start="; echo $start + $page_count; echo "\">Next</a> &raquo;";
            }

            echo "</td></tr></table><br><div align=\"center\" 
style=\"font-family: Verdana; font-size: 11px; color: #666666;\">Page $cur_page
 of $total_pages</div>";
        }
    }


#Create the table that contains the total amount of records found, in other words, the total amount of owned DVD's.
            echo "<center><table width='950' border=0><tr><td>&nbsp;</tr></td>";
            echo "<tr><td><b>Total amount of DVD's: " . $num . "</b></td></tr></table></center><br>";

#Make the links to the pages appear.
            pagination($page_count,$num,$start,$PHP_SELF,$cut_off);

#Some empty lines to make the page look better.
            echo "<br><br>";

#Make links to the other collection pages.
            echo "<center><a href='index.php' title='Go back to the 
index.'>Index</a> | <a href='dvd.php' title='View my DVD Collection.'>DVD</a> | 
<a href='gamecube.php' title='View my Gamecube Games 
Collection.'>Gamecube</a> | <a href='gameboy.php' title='View my Gameboy 
Games Collection.'>Gameboy</a> | <a href='pc_games.php' title='View my PC 
Games Collectie.'>PC Games</a> | <a href='audio.php' title='View my Audio CD 
Collection.'>Audio CD's</a></center><br>";

#Make a link to go directly to the homepage.
            echo "<center><a href='javascript:history.back(1)'><< Go 
back</a></center>";

#End the webpage.
            echo "</body></html>";

#Close the connection to our database.
    mysql_close($db);
?>


Ook heb ik de code in een txt file staan die hier te lezen is (misschien overzichtelijker).

[ Voor 110% gewijzigd door Godjira op 12-08-2004 20:08 ]

Profile


Verwijderd

Het is een beetje veel code, maar als ik je probleem snap dan zou ik eens naar de LIMIT syntax zoeken voor Sql queries.

[ Voor 4% gewijzigd door Verwijderd op 12-08-2004 20:15 ]


  • Megamind
  • Registratie: Augustus 2002
  • Laatst online: 14-05 03:35
Verwijderd schreef op 12 augustus 2004 @ 20:14:
Het is een beetje veel code, maar als ik je probleem snap dan zou ik eens naar de LIMIT syntax zoeken voor Sql queries.
Idd je begint zelf al goed:

http://www.godjira.net/collection/test/index.php?start=20

Dus dan maak je zo'n query:

code:
1
SELECT * FROM dvds WHERE bla LIMIT $start,$start+20

  • Godjira
  • Registratie: Februari 2003
  • Laatst online: 16-05 15:20

Godjira

To infinity and beyond!

Topicstarter
Megamind schreef op 12 augustus 2004 @ 20:16:
[...]

Idd je begint zelf al goed:

http://www.godjira.net/collection/test/index.php?start=20

Dus dan maak je zo'n query:

code:
1
SELECT * FROM dvds WHERE bla LIMIT $start,$start+20
Oke, ik heb nu al query dit:

code:
1
mysql_query("SELECT * FROM `" . $type . "` WHERE ID LIMIT $start,$page_count");


Maar hij werkt alleen als ik bij WHERE de ID zet, maar eigenlijk wil ik ordenen op titel. Dus ik zou Title moeten hebben, alleen dan krijg ik dus een lege tabel. Ook laat hij met deze query waar ik wel ID gebruik 20 rijen zien, maar niet het totaal aantal pagina's en de links naar de volgende pagina's.

Hier een voorbeeld.

Profile


  • Creepy
  • Registratie: Juni 2001
  • Nu online

Creepy

Tactical Espionage Splatterer

Godjira schreef op 12 augustus 2004 @ 20:31:
[...]

Oke, ik heb nu al query dit:

code:
1
mysql_query("SELECT * FROM `" . $type . "` WHERE ID LIMIT $start,$page_count");


Maar hij werkt alleen als ik bij WHERE de ID zet, maar eigenlijk wil ik ordenen op titel. Dus ik zou Title moeten hebben, alleen dan krijg ik dus een lege tabel. Ook laat hij met deze query waar ik wel ID gebruik 20 rijen zien, maar niet het totaal aantal pagina's en de links naar de volgende pagina's.

Hier een voorbeeld.
Je *ZOU* er ook eens een SQL tutorial bij kunnen pakken en er dan achter komen dat:
- WHERE gebruikt wordt voor selectie criteria
- ORDER BY gebruikt wordt om te sorteren

"I had a problem, I solved it with regular expressions. Now I have two problems". That's shows a lack of appreciation for regular expressions: "I know have _star_ problems" --Kevlin Henney


  • Wacky
  • Registratie: Januari 2000
  • Laatst online: 09:20

Wacky

Dr. Lektroluv \o/

Laat dat "WHERE ID" eruit, en gebruik ORDER BY i.c.m. LIMIT :)

Zoiets dus:
code:
1
mysql_query("SELECT * FROM tabel ORDER BY Titel LIMIT $bla,$blabla")

[ Voor 61% gewijzigd door Wacky op 12-08-2004 20:37 ]

Nu ook met Flickr account


  • gorgi_19
  • Registratie: Mei 2002
  • Laatst online: 14:36

gorgi_19

Kruimeltjes zijn weer op :9

Volgens mij moet hier wel uit te komen zijn door het LIMIT gedeelte te bestuderen in een handleiding van MySQL :) Bovendien moet je er met de eerder gegeven tips wel uit kunnen komen. :)

Digitaal onderwijsmateriaal, leermateriaal voor hbo

Pagina: 1

Dit topic is gesloten.