Thread Probleme mit 2fachem Template-Loop (23 answers)
Opened by Bob at 2009-12-29 22:29

Gast Bob
 2009-12-31 17:40
#129884 #129884
Hallo,
der komplette Teil der index.cgi
werde noch dinge ausbessern, wie zb. fetchrow_hashref benutzen etc...

Code (perl): (dl )
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
#!/usr/bin/perl 

use strict;
use warnings;
use CGI;
use CGI qw(:standart);
use CGI::Carp qw(fatalsToBrowser);
use HTML::Template;
use DBI;
use POSIX;
require('config.cgi');

our %config;

my $cgi = new CGI;
print $cgi->header();




# ---------- Ip auf Blackliste überprüfen ---------- #  
my $dbh = DBI->connect("dbi:mysql:$config{'mysql_db'}:$config{'mysql_host'}","$config{'mysql_user'}","$config{'mysql_pass'}") || die("$DBI::errstr\n");
my $sth = $dbh->prepare("SELECT * FROM blacklist WHERE ip=?") || die("$DBI::errstr\n");
                  $sth->execute($ENV{'REMOTE_ADDR'}) || die("$DBI::errstr\n");
my $request= $sth->fetchrow_array();
$sth->finish();
$dbh->disconnect;

# ---------- Bei auftretender Ip beenden ---------- #  
exit if ($request ne "");


# ---------- Settings lesen ---------- #  
my $dbh = DBI->connect("dbi:mysql:$config{'mysql_db'}:$config{'mysql_host'}","$config{'mysql_user'}","$config{'mysql_pass'}") || die("$DBI::errstr\n");
my $sth = $dbh->prepare("SELECT blogname,messages_per_page FROM settings") || die("$DBI::errstr\n");
                  $sth->execute();
my @settings = $sth->fetchrow_array();
$sth->finish();

my $blogname = $settings[0];
my $proseite = $settings[1];













 ###########################################################################################################################









# ---------- Navigation lesen ---------- #  


my $sth_box_name = $dbh->prepare("SELECT box_name FROM navibox") or die $dbh->errstr;
my $sth_links = $dbh->prepare("SELECT id,link_titel,link,categorie,box_name,option,content FROM navigation WHERE box_name=?") or die $dbh->errstr;

$sth_box_name->execute or die $dbh->errstr;

my @boxes;

while( my $box_name = $sth_box_name->fetchrow_array ) {

    $sth_links->execute($box_name);

    my @links;
    while( my $hashref = $sth_links->fetchrow_hashref ) {
        push @links, { link => $hashref->{link} };
    }

    push @boxes, { box_name => $box_name, box_links => \@links };

}







$sth_links->finish();  
$sth_box_name->finish();





 ###########################################################################################################################














my $page = CGI::param('page');
        if ($page !~ /^\d*$/) { $page = 0; }
        
my $perm = CGI::param('perm'); 
        if ($perm eq "") { 
                &index(); 
        } 
        else {
                 if ($perm !~ /^\d*$/) { $perm = 0; } 
                 &perm();
                 }



# ---------- Hauptseite ---------- #
sub index() {
        
my $template = HTML::Template->new(filename => "$config{'path'}/index.tmpl");



my $start = $page * $proseite; 


# ---------- Anzahl der Blogeinträge zählen ---------- #  
$sth = $dbh->prepare("SELECT COUNT(*) as anzahl FROM posts") || die("$DBI::errstr\n");
                  $sth->execute() || die("$DBI::errstr\n");
my $anzahl = $sth->fetchrow_array();
$sth->finish();



# ---------- Seiten berechnen ---------- #  
my $seiten = ceil($anzahl / $proseite);
if ($page > $seiten - 1){$page = 0; $start = $page * $proseite;}



# ---------- Einträge für jeweilige Seite auslesen ---------- #  
$sth = $dbh->prepare("SELECT * FROM posts order by date DESC Limit $start,$proseite") || die("$DBI::errstr\n");
                  $sth->execute() || die("$DBI::errstr\n");
                  
my @posts;
my $comment_str;
while(my @post_infos = $sth->fetchrow_array())
{
        # ---------- Anzahl der Kommentare zählen ---------- # 
        $anzahl = "";
        my $sth2 = $dbh->prepare("SELECT COUNT(*) as anzahl FROM comments WHERE post_perm=? AND unlocked=?") || die("$DBI::errstr\n");
           $sth2->execute($post_infos[0],1) || die("$DBI::errstr\n");
           $anzahl = $sth2->fetchrow_array();
           $sth2->finish();
           
                if($anzahl == 0) {$comment_str = "Keine Kommentare";}
                elsif($anzahl == 1) {$comment_str = "1 Kommentar";}
                else {$comment_str = "$anzahl Kommentare";}
                
        my %posts = (
                                perm => $post_infos[0],
                                head => $post_infos[2],
                                date => $post_infos[3],
                                user => $post_infos[4],
           comment_count => $comment_str,
                                post => $post_infos[5]
                                );
        push(@posts,\%posts);
}
$sth->finish();           
$dbh->disconnect;



# ---------- Seitennavigation vorbereiten ---------- #  
my $newer = $page - 1;
my $older = $page + 1;

my $html_newer = "<a style=\"float:left;\" href=\"index.cgi?page=$newer\"> &laquo zur&uuml;ck   </a>";
my $html_older = "<a style=\"float:right;\" href=\"index.cgi?page=$older\"> n&auml;chste &raquo; </a>";

if ($newer < 0) { $html_newer = ""; }
if ( ($older > $seiten - 1) || ($seiten == 1) ) { $html_older = ""; }   
        




 ###########################################################################################################################


# ----- Template Parameter ------ #
$template->param(
                                path => $config{'htmlpath'},
                        blogname => $blogname,
                           posts => \@posts,
                        zurueck => $html_newer,
                        naechste => $html_older,
                           box_list => \@boxes

                                );
                                
print $template->output();

}



Das Template:
Code: (dl )
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">



<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="de">





<head>



<title><TMPL_VAR NAME="blogname"></title>

<link rel="shortcut icon" type="image/x-icon" href="<TMPL_VAR NAME="path">/images/favicon.ico" />

<meta http-equiv="content-type" content="text/html;charset=iso-8859-1" />

<link rel="stylesheet" type="text/css" href="<TMPL_VAR NAME="path">/css/style.css" />



</head>



<body>



<div id="top"></div>



<!-- Header -->

<div id="header">



<!-- Blogname -->

<div id="blogname_box">

<p class="blogname"><TMPL_VAR NAME="blogname"></p>

</div>



</div>



<!-- Menubar -->

<div id="menu">



<a href="Test">TestPage</a>

<a href="Test2">TestPage2</a>



</div>



<!-- Main -->

<div id="main">



<!-- Content -->

<div id="content">



<TMPL_LOOP NAME="posts">

<div class="blog_box">

<div class="blog_box_head"><TMPL_VAR NAME="head"></div>

<div class="blog_box_info"><span class="blog_date"><TMPL_VAR NAME="date"></span><span class="blog_user"><TMPL_VAR NAME="user"></span> <span class="blog_comments"><a href="index.cgi?perm=<TMPL_VAR NAME="perm">"><TMPL_VAR NAME="comment_count"></a></span></div>

<div class="blog_post">



<TMPL_VAR NAME="post">



</div>



<div class="hr"></div>



</div>

</TMPL_LOOP>



<div class="pages"><TMPL_VAR NAME="zurueck"><TMPL_VAR NAME="naechste"></div>

</div>



<!-- Main2 -->

<div id="main2">

<!-- Search -->

<div id="search">



<form style="display:inline" action="" method="post">

<div><input style="width:100%;" type="text" name="search" value="Search ..." maxlength="40" onfocus="this.value=''" onblur="this.value='Search ...'" /></div>



</form>



</div>



<!-- Navi -->

<TMPL_LOOP NAME="box_list">

<div class="navi_box">





<fieldset>



<legend><TMPL_VAR NAME="box_name"></legend>



<TMPL_LOOP NAME="box_links">

<TMPL_VAR NAME="link">

</TMPL_LOOP>



</fieldset>





</div>

</TMPL_LOOP>





</div>

<div class="clear"></div>



</div>







<!-- Footer -->

<div id="footer">



</div>





</body>



</html>



Auszug der Datenbank:
Code: (dl )
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
--
-- Tabellenstruktur für Tabelle `navibox`
--

CREATE TABLE IF NOT EXISTS `navibox` (
`box_id` int(11) NOT NULL AUTO_INCREMENT,
`box_typ` varchar(30) NOT NULL,
PRIMARY KEY (`box_id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=3 ;

--
-- Daten für Tabelle `navibox`
--

INSERT INTO `navibox` (`box_id`, `box_typ`) VALUES
(1, 'Kategorien'),
(2, 'Counter');

-- --------------------------------------------------------

--
-- Tabellenstruktur für Tabelle `navigation`
--

CREATE TABLE IF NOT EXISTS `navigation` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`link_titel` varchar(200) NOT NULL,
`link` text NOT NULL,
`categorie` varchar(20) NOT NULL,
`box_typ` varchar(20) NOT NULL,
`option` varchar(20) NOT NULL,
`content` text NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=3 ;

--
-- Daten für Tabelle `navigation`
--

INSERT INTO `navigation` (`id`, `link_titel`, `link`, `categorie`, `box_typ`, `option`, `content`) VALUES
(1, 'Google', 'http://www.google.de', '', 'Kategorien', 'link', ''),
(2, 'Youtube', 'http://www.youtube.de', '', 'Kategorien', 'link', '');

-- --------------------------------------------------------



hoffe das reciht euch :)

lg Bob

View full thread Probleme mit 2fachem Template-Loop