Bonjour à tous,

Voilà, j'ai écris une page de code (qui vient en appuie d'un forum phpbb) et le script mets trop longtemps à s'exécuter et le serveur l'envoie des fois dans sa sandbox...

En gros, le script va lire la BDD en plusieurs requêtes pour créer un tableau en HTML. Quand il y a 500 références, ça va, mais dès qu'on arrive aux alentours de 1000 réfs, les 20 secondes du timeout claquent souvent...

Voici mon code (surement très crade, mais j'ai fait suivant ma vieille logique du langage C, enfin, de ce qui m'en reste ) :

Code : Sélectionner tout - Visualiser dans une fenêtre à part
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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
<?php
 
//$UserId = $_GET["UserId"];
//echo ($UserId);
 
define('IN_PHPBB', true);
 
$phpbb_root_path = (defined('PHPBB_ROOT_PATH')) ? PHPBB_ROOT_PATH : './';
 
$phpEx = substr(strrchr(__FILE__, '.'), 1);
 
$_GET = array();
 
include($phpbb_root_path . 'common.' . $phpEx);
 
 
 
// Start session management
$user->session_begin();
$auth->acl($user->data);
$user->setup();
 
$Pseudo = $user->data['username'];
$UserIDlook = $user->data['user_id'];
if ($user->data['user_id'] == ANONYMOUS)
{
    login_box('', $user->lang['LOGIN']);
} 
 
page_header('Les fiches perso de la Bible');
 
 
$template->set_filenames(array(
    'body' => 'JeLai.html',
 
));
 
//début affichage
 
 
//récupération des ID des forums enfants
mysql_query("SET NAMES UTF8"); 
 
$countTotal=0;
 
 
if($UserId<>"")
{
// Récupération des Thanks avec l'ID du user en argument
$requete = "SELECT thanks_from, post_id FROM  `phpbb3_thanks` WHERE  `thanks_from` =$UserId";
 
$Resultat = mysql_query($requete);
 
$count = 0;
 
$table2[] = array('ForumType' => "Forum/Type", 'Modele' => "Modele", 'Lien' => "Lien");
 
while ($row = mysql_fetch_array($Resultat))
{
	$count++;
 
// Sélection des infos de la fiche du thanks
$requeteF = "SELECT Bible_Ref, Bible_Fab, Bible_Ech, Bible_Photo, post_subject
FROM `phpbb3_posts` 
WHERE `post_id` =" . $row[post_id];
$ResultatF = mysql_query($requeteF);
$rowF = mysql_fetch_array($ResultatF);
 
// on remplie le tableau
$table2[$count] = array($count,$rowF[Bible_Ech],$rowF[post_subject],"http://forum.site.fr/viewtopic.php?p=" . $row[post_id] . "#p" . $row[post_id],$rowF[Bible_Fab],$rowF[Bible_Ref],$rowF[Bible_Photo]);
}
 
// suivant le tri en argument, on construit l'entete du tableau HTML
switch ($Sort)
{
	case "Echelle":
		$dataBible=$dataBible."<div class=\"ClassBibleEntete\">
  		<table width=\"100%\" border=\"0\">
   		 <tr>
      <td width=\"40\">#</td><td width=\"63\">
	  <font color=\"#f86900\">Echelle</font><a></td><td width=\"28%\"><a href=\"http://forum.site.fr/JeLai.php?UserId=" . $UserId . "&Sort=Modele\">Mod&egrave;le</a>
	  </a></td><td><a href=\"http://forum.site.fr/JeLai.php?UserId=" . $UserId . "&Sort=Fabricant\">Fabricant</a></td><td width=\"38%\">R&eacute;f&eacute;rence</td><td width=\"50\">Photo</td></tr></table></div>";
 
		break;
	case "Modele":
		$dataBible=$dataBible."<div class=\"ClassBibleEntete\">
  		<table width=\"100%\" border=\"0\">
   		 <tr>
      <td width=\"40\">#</td><td width=\"63\"><a href=\"http://forum.site.fr/JeLai.php?UserId=" . $UserId . "&Sort=Echelle\">Echelle</a><a></td><td width=\"28%\">
	  <font color=\"#f86900\">Mod&egrave;le</font></a></td><td><a href=\"http://forum.site.fr/JeLai.php?UserId=" . $UserId . "&Sort=Fabricant\">Fabricant</a></td><td width=\"38%\">R&eacute;f&eacute;rence</td><td width=\"50\">Photo</td></tr></table></div>";
		break;
 
		case "Fabricant":
		$dataBible=$dataBible."<div class=\"ClassBibleEntete\">
  		<table width=\"100%\" border=\"0\">
   		 <tr>
      <td width=\"40\">#</td><td width=\"63\"><a href=\"http://forum.site.fr/JeLai.php?UserId=" . $UserId . "&Sort=Echelle\">Echelle</a><a></td><td width=\"28%\"><a href=\"http://forum.site.fr/JeLai.php?UserId=" . $UserId . "&Sort=Modele\">Mod&egrave;le</a></a></td><td><font color=\"#f86900\">Fabricant</font></td><td width=\"38%\">R&eacute;f&eacute;rence</td><td width=\"50\">Photo</td></tr></table></div>";
		break;
 
 
		default:
		$dataBible=$dataBible."<div class=\"ClassBibleEntete\">
  		<table width=\"100%\" border=\"0\">
   		 <tr>
      <td width=\"40\">#</td><td width=\"63\"><a href=\"http://forum.site.fr/JeLai.php?UserId=" . $UserId . "&Sort=Echelle\">Echelle</a><a></td><td width=\"28%\"><font color=\"#f86900\">Mod&egrave;le</font></a></td><td><a href=\"http://forum.site.fr/JeLai.php?UserId=" . $UserId . "&Sort=Fabricant\">Fabricant</a></td><td width=\"38%\">R&eacute;f&eacute;rence</td><td width=\"50\">Photo</td></tr></table></div>";
 
}
 
// Obtient une liste de colonnes que l'on va trier
 
switch ($Sort)
{
	case "Echelle":
		foreach ($table2 as $key => $row)
		{
    		$rang[$key]  = $row[1];
 
		}
		break;
	case "Modele":
		foreach ($table2 as $key => $row)
		{
    		$rang[$key]  = $row[2];
 
		}
		break;
	case "Fabricant":
		foreach ($table2 as $key => $row)
		{
    		$rang[$key]  = $row[4];
 
		}
		break;
 
 
		default:
		foreach ($table2 as $key => $row)
		{
    		$rang[$key]  = $row[2];
		}
}
 
 
// Trie les données par rang croissant
// Ajoute $array en tant que dernier paramètre, pour trier par la clé commune
array_multisort($rang, SORT_ASC, $table2);
 
// on poursuit le tableau en alternant les class 1 et 2 
for ($counter=1; $counter<$count+1; $counter++)
{
	//echo $count . "<br>"; echo $TableauFinal[$count];
	if($counter&1)
	{
		$dataBible=$dataBible."<div class=\"ClassBible1\" onmouseover=\"this.style.background='#F8B600'; this.style.color='white'; \" onmouseout=\"this.style.background='#EEEEEE'; this.style.color='black';\" onClick='window.open(\"" . $table2[$counter][3]. "\")'>
  <table width=\"100%\" border=\"0\">
    <tr>
      <td width=\"40\">" . $counter . "</td><td width=\"63\">" . $table2[$counter][1] . "</td><td width=\"28%\">" . $table2[$counter][2] . "</td><td>" . $table2[$counter][4] . "</td><td width=\"38%\">" . $table2[$counter][5] . "</td><td width=\"50\"><img src=\"" . $table2[$counter][6] . "\">
	  </td></tr></table></div>";
	}
	else
	{
		$dataBible=$dataBible."<div class=\"ClassBible2\" onmouseover=\"this.style.background='#F8B600'; this.style.color='white'; \" onmouseout=\"this.style.background='#FFFFFF'; this.style.color='black';\" onClick='window.open(\"" . $table2[$counter][3]. "\")'>
  <table width=\"100%\" border=\"0\">
    <tr>
      <td width=\"40\">" . $counter . "</td><td width=\"63\">" . $table2[$counter][1] . "</td><td width=\"28%\">" . $table2[$counter][2] . "</td><td>" . $table2[$counter][4] . "</td><td width=\"38%\">" . $table2[$counter][5] . "</td><td width=\"50\"><img src=\"" . $table2[$counter][6] . "\">
	  </td></tr></table></div>";
	}
 
}
 
$dataBible=$dataBible."</table>";
 
//On liste les users pour ré utiliser les infos
$requeteName = "SELECT username, username_clean FROM `phpbb3_users` WHERE `user_id` = $UserId";
$ResultatName = mysql_query($requeteName);
if($ResultatName<>0)
{
	$row4 = mysql_fetch_array($ResultatName);
}
 
 
	if($count>0)
	{
		//$dataBible=$dataBible."<br><br><strong>" . $count . "</strong> fiches ont été trouvées";
	}
	else
	{
		$dataBible=$dataBible."<strong><br><br>Aucune fiche de coch&eacute;e :-(</strong>";
	}
}
 
$countUsers = 0;
 
//On construit le menu déroulant des users
$requeteUsers = "SELECT username, user_id, username_clean FROM `phpbb3_users` WHERE user_thanks != 0";
$ResultatUsers = mysql_query($requeteUsers);
 
while ($rowUsers = mysql_fetch_array($ResultatUsers))
{
 
	$tableUsers[$countUsers] = array($rowUsers[user_id], $rowUsers[username], $rowUsers[username_clean]);
	$countUsers++;
}
 
// on tri les users
unset($rang);
foreach ($tableUsers as $keyBis => $rowBis)
		{
    		$rang[$keyBis]  = $rowBis[2];
		}
//array_multisort($rang, SORT_ASC, $tableUsers2);
array_multisort($rang, SORT_ASC, $tableUsers);
//print_r($tableUsers);
/*echo"<br><br>";
print_r($tableUsers2);*/
 
// on construit le menu en HTML
$dataFormUsers = $dataFormUsers . "<form action=\"http://forum.site.fr/JeLai.php\" method=\"get\" name=\"formusers\" target=\"_self\" id=\"formusers\">
  <label>
  <select name=\"UserId\" size=\"1\">";
 
for ($counter=1; $counter<$countUsers+1; $counter++)
{ 
    $dataFormUsers = $dataFormUsers . "<option value=\"" . $tableUsers[$counter][0] . "\"";
 
	if($tableUsers[$counter][0] == $UserId )
	{
		$dataFormUsers = $dataFormUsers . " selected=\"selected\"";
	}
	$dataFormUsers = $dataFormUsers . ">" . $tableUsers[$counter][1] . "</option>";
}
$dataFormUsers = $dataFormUsers . "  </select>
  </label>
  <input type=\"submit\" value=\"Ok\">
</form>";
 
 
$LienProfil = "<a href=\"http://forum.site.fr/memberlist.php?mode=viewprofile&u=" . $UserId . "\"><u>" . $row4[username] . "</u></a>";
 
if($UserId != "")
{
	$titre = "Les <strong><u>" . $count . "</u></strong> fiches coch&eacute;es par ";
}
else
{
	$titre = "Merci de s&eacute;lectionner un utilisateur -->";
}
 
// On envoie les infos au template pour affichage
$template->assign_vars(array(
    'USERID'     => $UserId,
	'PSEUDO'     => $LienProfil,
	'DATA'		 => $dataBible,
	'COUNT'		 => $titre,
	'FORMUSERS'  => $dataFormUsers,
));
 
 
 
//make_jumpbox(append_sid("{$phpbb_root_path}viewforum.$phpEx"));
page_footer();
?>