Différences
Ci-dessous, les différences entre deux révisions de la page.
clients:php_soletcivilisation [2011/09/26 17:31] – créée vincent | clients:php_soletcivilisation [2011/09/29 09:51] (Version actuelle) – effacée vincent | ||
---|---|---|---|
Ligne 1: | Ligne 1: | ||
- | ====== Exemple du script PHP du site Sol et Civilisation ====== | ||
- | |||
- | <code php> | ||
- | <?php | ||
- | header(" | ||
- | |||
- | /* Initialisation */ | ||
- | $url = " | ||
- | |||
- | /* Récupération de la chaine de recherche */ | ||
- | $q = $_REQUEST[" | ||
- | |||
- | /* Définition de langue (dans notre cas uniquement le français */ | ||
- | $lang=' | ||
- | |||
- | /* Récupération des information de localisation dans un tableau $locArray | ||
- | Dans notre cas, il y a un seul fichier */ | ||
- | $locArray = parse_ini_file(" | ||
- | |||
- | /* Point important : la construction de la requête vers le serveur Scrutari | ||
- | avec les paramètres des champs*/ | ||
- | $query = " | ||
- | |||
- | /* | ||
- | * Récupération de l' | ||
- | */ | ||
- | $obj = Sct_decodeJsonArray($url, | ||
- | |||
- | /* | ||
- | * $ficheSearchResult est l' | ||
- | */ | ||
- | $ficheSearchResult = $obj[' | ||
- | |||
- | /* | ||
- | * Création d'une instance de la classe Sct_FicheSearchResult qui va conserver les résultats | ||
- | */ | ||
- | $sctFicheSearchResult = new Sct_FicheSearchResult($ficheSearchResult, | ||
- | |||
- | /* | ||
- | * Html résultant à afficher | ||
- | */ | ||
- | $resultString = ""; | ||
- | |||
- | /* | ||
- | * Nombre de fiches dans le résultat | ||
- | */ | ||
- | $ficheCount = $ficheSearchResult[' | ||
- | |||
- | /* | ||
- | * Construction du résultat | ||
- | */ | ||
- | $resultString = ""; | ||
- | if ($ficheCount == 0) { | ||
- | $resultString .= '< | ||
- | } else if ($ficheCount == 1) { | ||
- | $resultString .= '< | ||
- | } else { | ||
- | $resultString .= '< | ||
- | } | ||
- | if ($ficheCount > 0) { | ||
- | $ficheGroupArray = $ficheSearchResult[' | ||
- | $groupCount = count($ficheGroupArray); | ||
- | $resultString .= '< | ||
- | for($i = 0; $i < $groupCount; | ||
- | $ficheGroup = $ficheGroupArray[$i]; | ||
- | $ficheArray = $ficheGroup[' | ||
- | $count = count($ficheArray); | ||
- | for($j = 0; $j < $count; $j++) { | ||
- | $fiche = $ficheArray[$j]; | ||
- | $codecorpus = $fiche[' | ||
- | $resultString .= '< | ||
- | $resultString .= $sctFicheSearchResult-> | ||
- | $resultString .= '</ | ||
- | if (array_key_exists(' | ||
- | $resultString .= '<p class=" | ||
- | $resultString .= $sctFicheSearchResult-> | ||
- | $resultString .= '</ | ||
- | } | ||
- | $infoTech = ""; | ||
- | if (array_key_exists(' | ||
- | $mcomplementArray = $fiche[' | ||
- | $compCount = count($mcomplementArray); | ||
- | for($k = 0; $k < $compCount; $k++) { | ||
- | $mcomplement = $mcomplementArray[$k]; | ||
- | $num = $mcomplement[' | ||
- | if ($k > 0) { | ||
- | $infoTech .= ', '; | ||
- | } | ||
- | $infoTech .= $sctFicheSearchResult-> | ||
- | } | ||
- | } | ||
- | if (array_key_exists(' | ||
- | if (strlen($infoTech) > 0) { | ||
- | $infoTech .= ' | '; | ||
- | } | ||
- | $infoTech .= $fiche[' | ||
- | } | ||
- | if (strlen($infoTech) > 0) { | ||
- | $resultString .= '<p class=" | ||
- | $resultString .= $infoTech; | ||
- | $resultString .= '</ | ||
- | } | ||
- | | ||
- | if (array_key_exists(' | ||
- | $motcleCount = count($codemotcleArray); | ||
- | $codemotcleArray = $fiche[' | ||
- | $resultString .= '<p class=" | ||
- | $resultString .= '< | ||
- | if ($motcleCount == 1) { | ||
- | $resultString .= $locArray[' | ||
- | } else { | ||
- | $resultString .= $locArray[' | ||
- | } | ||
- | $resultString .= " "; | ||
- | $resultString .= '</ | ||
- | for($k = 0; $k < $motcleCount; | ||
- | if ($k > 0) { | ||
- | $resultString .= ', '; | ||
- | } | ||
- | $codemotcle = $codemotcleArray[$k]; | ||
- | $resultString .= $sctFicheSearchResult-> | ||
- | } | ||
- | $resultString .= '</ | ||
- | } | ||
- | $resultString .= '</ | ||
- | } | ||
- | } | ||
- | $resultString .= "</ | ||
- | } | ||
- | |||
- | ?> | ||
- | |||
- | <!-- Partie HTML proprement dite --> | ||
- | < | ||
- | < | ||
- | < | ||
- | <meta http-equiv=" | ||
- | <link href=" | ||
- | </ | ||
- | < | ||
- | <div id=" | ||
- | <div id=" | ||
- | <?php echo $resultString; | ||
- | </ | ||
- | </ | ||
- | </ | ||
- | </ | ||
- | <!-- Find de la partie HTML --> | ||
- | |||
- | <?php | ||
- | |||
- | / | ||
- | * Clasess et fonctions | ||
- | ******************************************************************/ | ||
- | |||
- | /** | ||
- | * Effectue la requête JSon et décode JSon pour obtenir un tableau | ||
- | */ | ||
- | function Sct_decodeJsonArray($url, | ||
- | $jsonString = file_get_contents($url.'?' | ||
- | return json_decode($jsonString, | ||
- | } | ||
- | |||
- | |||
- | |||
- | /** | ||
- | * Objet encapsulant le résultat d'une recherche et proposant | ||
- | * des fonctions utilitaires. | ||
- | */ | ||
- | class Sct_FicheSearchResult { | ||
- | |||
- | var $corpusIntituleArray; | ||
- | var $spanClass; //contenu de l' | ||
- | var $motcleStringMap; | ||
- | |||
- | function __construct($ficheSearchResult, | ||
- | $this-> | ||
- | $this-> | ||
- | $this-> | ||
- | } | ||
- | | ||
- | /** | ||
- | * Retourne l' | ||
- | * le corpus de code $codecorpus | ||
- | */ | ||
- | function getComplementIntitule($codecorpus, | ||
- | $intituleCount = count($this-> | ||
- | for($i = 0; $i < $intituleCount; | ||
- | $corpusIntitule = $this-> | ||
- | if ($corpusIntitule[' | ||
- | return $corpusIntitule[' | ||
- | } | ||
- | } | ||
- | return "?"; | ||
- | } | ||
- | | ||
- | /** | ||
- | * $markedStringArray est un tableau qui contient soit des chaines soit des tableaux associatifs | ||
- | * comprenant une clé ' | ||
- | * span dont la classe est $spanClass | ||
- | */ | ||
- | function concatWithSpan($markedStringArray) { | ||
- | $result = ""; | ||
- | $count = count($markedStringArray); | ||
- | for($i = 0; $i < $count; $i++) { | ||
- | $obj = $markedStringArray[$i]; | ||
- | if (is_array($obj)) { | ||
- | $result .= '< | ||
- | $result .= $obj[' | ||
- | $result .= '</ | ||
- | } else { | ||
- | $result .= $obj; | ||
- | } | ||
- | } | ||
- | return $result; | ||
- | } | ||
- | | ||
- | /** | ||
- | * Retourne le libellé du mot-clé de code $codemotcle | ||
- | */ | ||
- | function getMotcleString($codemotcle) { | ||
- | return $this-> | ||
- | } | ||
- | | ||
- | /** | ||
- | * Initialisation du tableau associatif des codes de mots-clés et de lerus libellés | ||
- | */ | ||
- | private function initMotcleStringMap($ficheSearchResult) { | ||
- | $motcleStringMap = array(); | ||
- | if (!array_key_exists(' | ||
- | return $motcleStringMap; | ||
- | } | ||
- | $motcleArray = $ficheSearchResult[' | ||
- | $count = count($motcleArray); | ||
- | for($i = 0; $i < $count; $i++) { | ||
- | $motcle = $motcleArray[$i]; | ||
- | $codemotcle = $motcle[' | ||
- | $mlibelleArray = $motcle[' | ||
- | $lib = ""; | ||
- | $libCount = count($mlibelleArray); | ||
- | for($j = 0; $j < $libCount; $j++) { | ||
- | if ($j > 0) { | ||
- | $lib .= "/"; | ||
- | } | ||
- | $mlib =$mlibelleArray[$j]; | ||
- | $lib .= $this-> | ||
- | } | ||
- | $motcleStringMap[$codemotcle] = $lib; | ||
- | } | ||
- | return $motcleStringMap; | ||
- | } | ||
- | |||
- | } | ||
- | |||
- | ?> | ||
- | |||
- | </ | ||
- | |||
- | |||
- | Et pour information, | ||
- | |||
- | < | ||
- | sct_motscles_un = " | ||
- | sct_deuxpoints = " | ||
- | sct_resultat_zero = "Aucun résultat pour la recherche « " | ||
- | sct_resultat_plusieurs = " | ||
- | sct_resultat_un = "Un résultat pour la recherche « " | ||
- | sct_finguillemet = " | ||
- | </ |