We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 30585
    • 833 Posts
    Guys I have a custom PHP/MYSQL bible application developed and Opensourced by the good fellow David Houstin. It's available for download here: http://houstin.info/chretien/bible-php_354.html and props to David for the great work.

    The MYSQL dabase is very simple in structure and only has 2 tables: ref_bible and versets

    The ref_bible table is essentially a table of content and has 3 fields (columns): id, livre (book) and chap (Number of chapters)

    The versets (verses) table is where the money lives. It stores each individual verse, the chapter, it's corresponding ref_bible id and the translation version (KJB, DARBY, SEGOND, etc.)

    There is a total of 1189 chapters. In other words, 1189 pages when accessed from the frontend. That's a lot of pages. My question is how to integrate this into MODX smartly given that it already comes with a default php file that handles the frontend browsing part. (Please see below). A live example of the application can be seen here. lgctv[dot]org/bible/

    I'm running MODX 2.2 PL2

    Ideally, as recommended here http://rtfm.modx.com/display/revolution20/Functional+Changes+from+Evolution, I'd like to turn the supplied php file into a snippet that would easily be integrated with the rest of the resources. I need some guidance on how include the file below in a new snippet and ultimately pull its values. This is new territory for me and I'm eager to learn.

    Another concern would be how to handle friendly URLS when so many pages are being pulled from a custom database?

    Here's the main php file: (French comments, but I can help translate if need be)

    <?
    /**************************************************************
    *					LA BIBLE EN LIGNE
    *								David Houstin - houstin.info
    **************************************************************/
    
    	// variables technique
    	$racine = "./";
    	
    	// includes
    	include ("config.inc.php");
    	include ("includes/tradfrancais.inc.php");
    	include ("includes/barre_navig.inc.php");
    	include ("includes/gen_listechap.inc.php");
    	include ("includes/gen_listeverses.inc.php");
    	include ("includes/gen_listelivres.inc.php");
    
    /* I. LES VARIABLES ******************************************/
    	
    	$nb_paagesaff 	= 3; 	// nombre de liens vers les pages suivantes affichées
    	$nb_versets 	= 30; 	// nombre de versets par page
    	$nb_resultpp 	= 20;	// nombre de résultats par page
    	
    	$file_accueil 	= "livres.php";  // page d'accueil
    
    	$LSG 		= $_REQUEST["LSG"];
    	$darby 		= $_REQUEST["darby"];
    	$martin 	= $_REQUEST["martin"];
    	$ostervald 	= $_REQUEST["ostervald"];
    	$change_vst = $_REQUEST["change_vst"];
    	$cookie_ok 	= $_REQUEST["cookie_ok"];
    	$change_vst = $_REQUEST["change_vst"];
    	$promesse 	= $_REQUEST["promesse"];
    	$livre 		= $_REQUEST["livre"];
    	$chapitre 	= $_REQUEST["chapitre"];
    	$verset 	= $_REQUEST["verset"];
    	$q 			= $_REQUEST["q"];
    	$start		= $_REQUEST["start"];
    	
    	// attribution de valeurs par défaut
    	if (empty($action) || ($action == "recherche" && $q < 2)) $action = $_REQUEST["action"];
    	if (empty($action)) $action = "accueil";
    	if (empty($promesse) && $change_vst != 1) $promesse = $_REQUEST["promesse"]; 
    	if (empty($LSG) && empty($darby) && empty($martin) && empty($ostervald)) $LSG = 1;
    
    	// construction de la requete
    	if (!empty($promesse)) 	$larequete = "&promesse=" . $promesse;
    	if (!empty($livre)) 	$larequete .= "&livre=" . $livre;
    	if (!empty($chapitre)) 	$larequete .= "&chapitre=" . $chapitre;
    	if (!empty($q)) 		$larequete .= "&q=" . urlencode($q);
    	
    	$larequete2 = $larequete . "&action=" . $action; ;
    	
    	if (!empty($LSG))		$larequete .= "&LSG=" . $LSG;
    	if (!empty($darby))		$larequete .= "&darby=" . $darby;
    	if (!empty($martin))	$larequete .= "&martin=" . $martin;
    	if (!empty($ostervald))	$larequete .= "&ostervald=" . $ostervald;
    	
    	// titre de la version
    	if ($LSG == 1)  		 $title = " Louis Segond";
    	elseif ($darby == 1) 	 $title = " Darby";
    	elseif ($martin == 1) 	 $title = " Martin";
    	elseif ($ostervald == 1) $title = " Ostervald";
    	
    	// nom du livre
    	$sql_titrelivre = mysql_db_query ($dbname2, "SELECT livre FROM ref_bible WHERE id='" . $livre . "'", $connex_bible);
    	$titre_livre = mysql_fetch_object ($sql_titrelivre);
    	$nom_livre = $titre_livre->livre;
    
    	// mise en norme des variables
    	if (empty($chapitre) && $action == "texte") $chapitre = 1;
    	
    	$q = str_replace('+', ' ', trim(stripslashes($q)));
    	$q = str_replace(',', ' ', $q);
    	$q = str_replace(':', ' ', $q);
    	$q = str_replace('-', ' ', $q);
    	$q = str_replace('\'', '`', $q);
    
    	$tab_mc = explode(' ' , $q);
    
    	
    /* III. AFFICHAGE DE L'ACCUEIL *******************************/
    
    	if ($action == "accueil" || empty($action)) include ($file_accueil);
    
    /* IV. RECHERCHE PAR MOT-CLE *********************************/
    
    	elseif ($action == "recherche" && strlen($q) > 2 ) {
    	
    		if ($LSG == 1) 			 $traduction = "LSG";
    		elseif ($darby == 1) 	 $traduction = "darby";
    		elseif ($martin == 1) 	 $traduction = "martin";
    		elseif ($ostervald == 1) $traduction = "ostervald";
    			
    		// requete principale de recherche
    		$req_search = "SELECT id, ch, vst, $traduction FROM versets WHERE ";
    		
    		// recherche dans tous les mots clés
    		$nb_mc = count ($tab_mc);
    		
    		if  ($nb_mc != 0) {
    		
    			$req_search .= " (";
    			
    			for ($i = 0; $i != $nb_mc; $i++) {
    			
    				if (strlen($tab_mc[$i]) > 2) {
    				
    					if ($promesse == 1) $LIAISON = " OR ";
    					else $LIAISON = " AND ";
    					
    					$req_search .= " (" . $traduction . " LIKE '%" . trim(addslashes($tab_mc[$i])) . "%' OR
    								   " . $traduction . " LIKE '%" . trim(addslashes(francais3($tab_mc[$i]))) . "%')";
    					$req_search .= $LIAISON;
    				}
    			}
    			$req_search = substr ($req_search, 0, -4);
    			$req_search .= ") ";
    		}
    		
    		// si on cherche dans un livre
    		if (!empty($livre) && $livre != "O" && $livre != "N") $req_search .= " AND id='" . $livre . "'";
    		
    		// si on cherche dans un chapitre
    		if (!empty($chapitre) && !empty($livre)) $req_search .= " AND ch='" . $chapitre . "'";
    		
    		// si on cherche dans un verset
    		if (!empty($verset) && !empty($livre) && !empty($chapitre)) $req_search .= " AND vst='" . $verset . "'";
    		
    		// si on cherche dans le Nouveau Testament
    		if ($livre == "O") $req_search .= " AND id like '%O'";
    
    		// si on cherche dans l'Ancien Testament
    		if ($livre == "N") $req_search .= " AND id like '%N'";
    		
    		// si on recherche uniquement dans les promesse
    		if ($promesse == 1) $req_search .= " AND is_promesse='1'";
    		
    		$sql_search2 = mysql_db_query ($dbname2, str_replace ("id, ch, vst, $traduction", "id", $req_search), $connex_bible);
    		$nb_results = mysql_num_rows ($sql_search2);
    
    		$lapage = $start * $nb_resultpp;
    		
    		$req_search .= " LIMIT ". $lapage . ", " . $nb_resultpp;
    		$sql_search = mysql_db_query ($dbname2, $req_search, $connex_bible);
    		
    		
    		/* Résumé de la recherche **************************************/
    		
    		echo "<h2>Recherche dans la bible $title</h2>\n";
    		echo "<div class=\"menu_versets\"><strong>$nb_results</strong> résultats trouvés";
    		
    		if (!empty($q)) echo " pour <h4>$q</h4> ";
    		if ($livre == "N") echo " dans le <h4>Nouveau Testament</h4>";
    		elseif ($livre == "O") echo "dans l'<h4>Ancien Testament</h4>";
    		elseif (!empty($livre)) {
    			
    			echo " dans <h4>$nom_livre";
    			if (!empty($chapitre)) echo " $chapitre";
    			echo "</h4>\n";
    		}
    		else echo " dans <h4>Toute la Bible</h4>
    \n";
    			
    		echo "</div>";
    		
    		if ($nb_results != 0 ) {
    		
    			// barre de navigation
    			$barre_nav = barrenavig();
    			$leverset .= $barre_nav;			
    
    			while ($line = mysql_fetch_object ($sql_search)) {
    			
    				// selection du titre du livre
    				$sql_titrelivre = mysql_db_query ($dbname2, "SELECT livre FROM ref_bible WHERE id='" . $line->id . "'", $connex_bible);
    				$titre_livre = mysql_fetch_object ($sql_titrelivre);
    				$nom_livre = $titre_livre->livre;
    				
    				// lignes de résultat
    				echo  "<div id=\"verset\"><div class=\"reference\"><a href=\"?action=texte&livre=" . $line->id . "&chapitre=" . $line->ch . "#" . $line->vst . "\"
    					   class=\"reference\">" . $nom_livre . " " . $line->ch . " : " . $line->vst . "</a></div>\n";
    				
    				if ($LSG == 1) 			 $verset_enforme = $line->LSG;
    				elseif ($darby == 1) 	 $verset_enforme = $line->darby;
    				elseif ($martin == 1) 	 $verset_enforme = $line->martin;
    				elseif ($ostervald == 1) $verset_enforme = $line->ostervald;
    				
    				foreach ($tab_mc as $mot) {
    								
    					if (strlen($mot) > 2) {
    					
    						$verset_enforme = eregi_replace ($mot, "<span class=\"mc\">$mot</span>", $verset_enforme);
    						$verset_enforme = eregi_replace (francais2($mot), "<span class=\"mc\">" . francais2($mot) . "</span>", $verset_enforme);
    					}
    				}
    				$verset_enforme = eregi_replace ($q, "<span class=\"mc\">$q</span>", $verset_enforme);
    				echo str_replace('`', '\'', $verset_enforme) . "</div>\n";
    			}
    			echo $barre_nav ;
    			
    		} // END IF
    		
    		else { // si pas de résultat à la recherche
    			
    			echo "<div id=\"message\">Aucun verset ne correspond à votre requète : </div>";
    			if (!empty($livre)) echo "<a href=\"?action=recherche&livre=&mc=" . urlencode($q) . "\" >Effectuer une recherche dans toute la Bible</a>";
    		}
    		
    	} // END ELSEIF Recherche
    	elseif ($action == "recherche" && strlen($q) < 2 && empty($livre)) echo "<div id=\"message\">Erreur : Le mots clé doit comporter au moins 2 caractères</div>";
    	else $action = "texte";
    
    /* V. RECHERCHE PAR VERSET ********************************/
    
    	if ($action == "texte" && !empty($livre) && $livre != "N" && $livre != "O") {
    	
    		if (empty($chapitre) || $chapitre < 1) $chapitre = 1;
    	
    		if ($LSG == 1) 			 $col = "LSG";
    		elseif ($darby == 1)   	 $col = "darby";
    		elseif ($martin == 1) 	 $col = "martin";
    		elseif ($ostervald == 1) $col = "ostervald";
    		
    		// requete générale
    		$req_afficher = "SELECT $col, ch, vst FROM versets WHERE id='" . $livre . "' AND ch='" . $chapitre . "'";
    		
    		$sql_afficher2 = mysql_db_query ($dbname2, $req_afficher, $connex_bible);
    		$nb_versets = mysql_num_rows ($sql_afficher2);
    		
    		if (!empty($verset)) $req_afficher .= " AND vst='" . $verset . "'";
    		
    		$sql_afficher = mysql_db_query ($dbname2, $req_afficher, $connex_bible);
    		
    		// lecteur flash
    		if (substr($livre, -1) == "N") $dossier = "NT";
    		elseif (substr($livre, -1) == "O") $dossier = "AT";
    
    		if ($chapitre < 10) $lechapitre = "0" . $chapitre;
    		else $lechapitre = $chapitre;
    		
    		$mp3 = "http://www.enseignemoi.net/audio/bible/$dossier/" . $livre . "_" . $lechapitre . ".mp3";
    		
    		$bouton_ecouter = '<p><table border="0" cellpadding="2" cellspacing="0">
    			  <tr>
    				<td><strong>ECOUTER (LSG)</strong></td>
    				<td>
    					<object type="application/x-shockwave-flash" data="' .$racine . 'dewplayer.swf?son=' . $mp3 .'" width="200" height="20"> 
    						<param name="movie" value="' .$racine . 'dewplayer.swf?son=' . $mp3 .'" />  
    					</object>
    				</td>
    				<td><a href="'. $mp3 .'" rel=enclosure title="Flux du fichier"><img src="' .$racine . 'images/podcast.png" border="0"/></a></td>
    			  </tr>
    		</table></p>';			
    		
    		// fin bouton
    		
    		echo "<h2>" . $nom_livre . " " . $chapitre . " (" . trim($title) . ") </h2>\n";
    		
    /* Barre de navigation **************************************/
    
    		$barre_nav = "<div class=\"menu_versets\">\n";
    		$barre_nav .= "<strong>versets</strong> :\n";
    		
    		for ($i = 1; $i != $nb_versets + 1; $i++) {
    		
    			if ($action != "recherche") $barre_nav .= " <a class=\"black\" href=\"#$i\">$i</a>";
    			else $barre_nav .= " <a class=\"black\" href=\"?action=texte&livre=$livre&chapitre=5#$i\">$i</a>";
    		}
    		echo $barre_nav .= "</div>\n";
    		
    /* Fin barre *************************************************/
    		
    		// ecouter
    		echo "<p>" . $bouton_ecouter . "</p>";
    		
    		while ($line = mysql_fetch_object ($sql_afficher)) {
    		
    			echo "<div id=\"verset\">\n";
    			echo "<a name=\"" . $line->vst . "\"></a>\n\n<div class=\"reference\">" . $line->ch . " : " . $line->vst . "</div>";
    			
    			if ($LSG == 1) {
    				
    				$verset_enforme = $line->LSG;
    				if (!empty($q)) $verset_enforme = ereg_replace ($q, "<div class=\"mc\">$q</div>", $verset_enforme);
    			}
    			elseif ($darby == 1) {
    				
    				$verset_enforme = $line->darby;
    				if (!empty($q)) $verset_enforme = ereg_replace ($q, "<div class=\"mc\">$q</div>", $verset_enforme);
    			}
    			elseif ($martin == 1) {
    				
    				$verset_enforme = $line->martin;
    				if (!empty($q)) $verset_enforme = ereg_replace ($q, "<div class=\"mc\">$q</div>", $verset_enforme);
    			}
    			elseif ($ostervald == 1) {
    				
    				$verset_enforme = $line->ostervald;
    				if (!empty($q)) $verset_enforme = ereg_replace ($q, "<div class=\"mc\">$q</div>", $verset_enforme);
    			}
    			echo str_replace('`', '\'', $verset_enforme);
    			echo "</div>";
    		}
    		if (empty($verset) && $i >= 10) echo $barre_nav;
    	} 
    	
    	mysql_close ($connex_bible);
    ?>

    [ed. note: treigh last edited this post 12 years ago.]
      A MODx Fanatic
      • 37286
      • 160 Posts
      Are you planning to make this an addon for modx, or is this for your own use?

      This will help get you going on Revo development, I still go back to this page occasionally to review: http://rtfm.modx.com/display/revolution20/Developing+an+Extra+in+MODX+Revolution.
        • 30585
        • 833 Posts
        It's for a project, but of course it will get released to the community so everyone can benefit form it and even improve it.

        Thx for the link. I'm currently working on it. Hopefully I'll release it for everyone
          A MODx Fanatic
        • Mark Hamstra Reply #4, 12 years ago
          You can also use $modx->query('YOUR SQL HERE') which returns an array of the results, which may be easier to incorporate into your code while using the existing connection to MySQL.

          When developing snippets it's typically better to use a single return instead of echo-ing data to the screen. You do have a lot of echo-ing going on, so it may be easier to leave it as-is for now, unless you're going all-in for refactoring smiley If you are, also consider using chunks for the html parts with placeholders as to split markup and logic.
            Mark Hamstra • Developer spending his days working on Premium Extras and a MODX Site Dashboard with the ability to remotely upgrade MODX and extras to make the MODX world a little better.

            Tweet me @mark_hamstra, check my infrequent blog at markhamstra.com, my slightly more frequent ramblings at MODX.today or see code at Github.
            • 30585
            • 833 Posts
            Thx for the follow-up Mark.
              A MODx Fanatic