Bonjour a tous.

Je suis nouveau dans la programmation PHP.

Dans mon site je dois montrer un tableau qui prends des infos depuis plusieurs tables de ma base de donnés.

Je ne sais pas si c'est mieux mettre tout ce dont j'ai besoin dans la requête initiale avec des JOIN ou bien prendre a chaque fois l'info nécessaire en consultant la table originale par la clé.

Dans mon code actuel:
a. Je lance une première requête qui joint plusieurs tables.
b. Je lis le résultat de chaque ligne dans une boucle et avec une clé j'appelle la fonction getLecturas () pour lire une autre table qui garde des infos relatives a la ligne actuelle dans (a)
c. getLecturas() appelle á sa suite une autre fonction getLecturasLink () pour obtenir des donnés relatives aux possibles lignes qui trouverait (b)

Ce qui me gene c'est de lancer de requêtes (b) et (c) à chaque fois à l'intérieur du boucle, je pense que ce n'est pas tres performant.

J'ai conçu la chose de maniere auto-dicté, en consultant dans les forums... le code marche, mais j'ai interet de la rendre le plus performant possible.

Merci d'avance pour vous idées.

Ici mon code:

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
 
<?php
$categories = get_the_category();
$category_id = $categories[0]->cat_ID;
//105 Cuaresma 122 SS 104:TriP  23:TP TO: 120
	$arrId = array(1 => '118', 2 => '119', 3 => '105', 4 => '23', 5 => '120');
echo 'Este módulo está en proceso de programación. Si no aparecen los datos, puedes escribir la fiesta que buscas y pulsar en "Buscar"';
?> <form role="search" method="get" id="searchform" action="<?php echo home_url( '/' ); ?>">
    <div><label class="screen-reader-text" for="s">Palabras que quieres buscar:</label>
        <input type="text" value="" name="s" id="s" />
        <input type="submit" id="searchsubmit" value="Buscar" />
    </div>
</form>
<?php echo $category_id.'  -  ';
$id=array_search($category_id, $arrId);
if ($id){
	require('wp-config.php' );
	require('SafePDO.php' );
	$dsn = 'mysql:host='.DB_HOST.';dbname='.DB_NAME.';charset=utf8';
	$opt = array(
    PDO::ATTR_ERRMODE            => PDO::ERRMODE_EXCEPTION,
    PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
    PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8'
	);
	$pdo = new SafePDO($dsn,DB_USER,DB_PASSWORD);
	try {
	$query = 'SELECT TCel.id_celebracion, TCel.id_tiempo, TCel.id_tipo, TCel.dia, TCel.estatus, TCel.semana,
	 		TTiempos.id_tiempo, TTiempos.tiempo FROM  tblLCelebraciones  AS TCel
			INNER JOIN tblLTiempos as TTiempos ON TCel.id_tiempo = TTiempos.id_tiempo
			WHERE TCel.id_tiempo=? ORDER BY TCel.id_tipo, TCel.id_tiempo,semana, id_tipo,dia'; 
	$stmt = $pdo->prepare($query);
	$stmt->bindParam(1, $id);
	$stmt->execute();
	$result = $stmt->fetchAll();
	$x=0;
	$claseAlt1='blueBackground';
	if ($id==1){
		$claseAlt2='moradoBackground'; $class='fondoAdviento';
	} else {
		$claseAlt2= 'graybackground';
		$class='tablaUtil';	
	}
	echo '<div class="util"><table class="'.$class.'"><th>SEMANA-DÍA</th><th>HOMILÍAS</th><th>LECTURAS</th>';
	foreach($result as $row){
		$classalterna = ($x%2 == 0)? $claseAlt1 : $claseAlt2;//'blueBackground': 'moradoBackground';
		$data = explode("|", doEnlace($row['estatus'], $row['id_tipo'],$row['id_tiempo'],$row['semana'],$row['dia']));
//		print $data;
		echo "<tr class='$classalterna'>";
		echo '<td><b>'.romanic_number($row['semana']).' '.ucfirst($data[0]).'</b></td><td>'
			.$data[1].'</td><td><span class="small">'.getLecturas ($pdo, $row['id_celebracion']).'</span></td></tr>';
 
		$x++; 
 
	}
		echo '</table></div>';
	} 
	catch(PDOException $e) {
    	echo 'ERROR: <br> Por favor, informe de este error al administrador del sitio web. Gracias';
	}
	$pdo = null;
	} 
	else {
		if(is_category() || is_single()){
		global $post;
		foreach(get_the_category() as $category) {
			$current = $category->cat_ID;
			$current_name = $category->cat_name;
 		}
		}
		echo '<p></p>';
		echo $current_name . " has id ".$current;
		$myposts = get_posts('numberposts=50&orderby=post_name&order=ASC&category='.$current);
		foreach($myposts as $post) : ?>
			<li>
			<span style="line-height:1.5;"><a href="<?php the_permalink(); ?>">
			<?php the_title(); ?></a></span>
			</li>
		<?php endforeach; 
    } 		
 
function getLecturas ($pdo, $id_celebracion) {
	try {
		$query = 'SELECT TLibros.abreviatura, TPericopas.id_pericopa, TPericopas.capitulo, TPericopas.v_inicial, TPericopas.v_final, TPericopas.tema_liturgia, TPericopas.estatus_link, TPericopas.estatus_ref, TLecturas.id_tipo  
		FROM  tblLPericopas AS TPericopas			
		INNER JOIN tblLLecturas as TLecturas ON TLecturas.id_pericopa = TPericopas.id_pericopa
		JOIN tblLibros AS TLibros ON TPericopas.id_libro = TLibros.id_libro
		WHERE TLecturas.id_celebracion=? ORDER BY TLecturas.id_tipo'; 
 
		$stmt = $pdo->prepare($query);
		$stmt->bindParam(1, $id_celebracion);
		$stmt->execute();
		$result = $stmt->fetchAll();
		$lecturas='';
 
	foreach($result as $row) {
		$abreviatura=$row['abreviatura'];
		$capitulo=$row['capitulo'];
		$v_inicial=$row['v_inicial'];
		$v_final=$row['v_final'];
 
		if ($row['estatus_ref']==1){ 
			$ref='<b>'.$row['abreviatura'].'</b> '.$capitulo.', '.getLecturaLink($row['id_pericopa'], $pdo);
			} else {
			$ref='<b>'.$row['abreviatura'].'</b> '.$row['capitulo'].
			', '.$row['v_inicial'].'-'.$row['v_final'];
		}
		if ($row['estatus_link']==1){
			$ref='<a href="http://www.deiverbum.org/'.strtolower($abreviatura).'-'.
			rellenarCeros('2', $capitulo).'_'.rellenarCeros('2', $v_inicial).'-'.
			rellenarCeros('2', $v_final).'"'.$ref.'</a>';
		}	
		$lecturas=$lecturas.$ref.'<br>';	
	}
	} catch(PDOException $e) {
    	echo 'ERROR: <br> Por favor, informe de este error al administrador del sitio web. Gracias';
	}	
	return $lecturas;
}
 
function getLecturaLink ($id_pericopa, $pdo){
	try {
		$query='SELECT ref FROM tblLPericopasAux WHERE id_pericopa=?';
		$stmt = $pdo->prepare($query);
		$stmt->bindParam(1, $id_pericopa);
		$stmt->execute();
		$result = $stmt->fetchAll();
		$ref='';
		foreach($result as $row){
			$ref=$row['ref'];
		}
		} catch(PDOException $e) {
    		echo 'ERROR: <br> Por favor, informe de este error al administrador del sitio web. Gracias';
		}	
	return $ref;	
}
 
function doEnlace  ($estatus, $id_ciclo, $id_tiempo, $id_semana, $id_dia) {
$enlace='Por hacer';
	$arrCiclos = array(1 => 'a', 2 => 'b', 3 => 'c');
	$arrTiempos = array(1 => 'adviento', 2 => 'navidad', 3 => 'cuaresma', 5=> 'ordinario');
	$arrDias = array(1 => 'Domingo', 2 => 'Lunes', 3 => 'Martes', 6=> 'Miercoles');
	$strDia=$arrDias[$id_dia];
	if ($id_dia==1) {
		$strCiclo=$arrCiclos[$id_ciclo];
		$id_ciclo='ciclo-'.$strCiclo;
		$strDia=$strDia.' (Ciclo '.ucwords($strCiclo).')';
		}
	else {
		$id_ciclo='';
	}	
 
	if($estatus!=='0'){
		$enlace = '<a HREF="http://deiverbum.org/homilias-'.$id_ciclo.'_semana-'
			.rellenarCeros('2', $id_semana).'_tiempo-'
			.$arrTiempos[$id_tiempo].'_dia-'.rellenarCeros('2', $id_dia).'-'
			.$arrDias[$id_dia].'">Homilías</a>';
	}
	return $strDia.'|'.strtolower ($enlace);
}
 
function romanic_number($integer, $upcase = true) { 
    $table = array('M'=>1000, 'CM'=>900, 'D'=>500, 'CD'=>400, 'C'=>100, 'XC'=>90, 'L'=>50, 'XL'=>40, 'X'=>10, 'IX'=>9, 'V'=>5, 'IV'=>4, 'I'=>1); 
    $return = ''; 
    while($integer > 0) { 
        foreach($table as $rom=>$arb) { 
            if($integer >= $arb){ 
                $integer -= $arb; 
                $return .= $rom; 
                break; 
            } 
        } 
    } 
    return $return; 
} 
function rellenarCeros ($iLength, $mStretch ) {
    $sPrintfString = '%0' . (int)$iLength . 'd';
    return sprintf($sPrintfString, $mStretch);
}		
?>