|
Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon. |
|
View previous topic :: View next topic |
Author |
Message |
arif85 Smarty n00b
Joined: 20 Aug 2011 Posts: 4
|
Posted: Sat Aug 20, 2011 9:10 pm Post subject: [résolu]Citations aléatoires d'une base de donnée |
|
|
Je suis tout nouveau sur smarty et je bloque pour réaliser l'action suivante:
J'ai une base de données MySQL avec une table de 3 rows :
-id
-citation
-auteur
Je souhaite afficher aléatoirement mes citations.
Pour ce faire dans mon index.php j'ai notamment fait un
Code: |
<?php
$connexion = new PDO(
'mysql:host=localhost;dbname=lalala',
'tralala',
'mypassword'
);
$query = $connexion->prepare("SELECT * FROM citation ORDER BY id ASC");
$query->execute();
$cite_lacitation = array();
$i = 0;
while ($data = $query->fetch()){
$cite_lacitation[$i] ['id'] = $data ["id"];
$cite_lacitation[$i] ['cite'] = $data ["cite"];
$cite_lacitation[$i] ['author'] = $data ["author"];
$i ++;
}
shuffle($cite_lacitation);
require("lib/smarty/smarty.class.php");
$tpl = new Smarty();
$tpl->assign('cite_lacitation', $cite_lacitation);
$tpl->display("index.html");
?>
|
Mon problème est que je ne sais pas comment m'y prendre dans mon index.html afin de ne prendre qu'une citation à la fois et la faire changer à chaque refresh...
Pouvez-vous m'aider ?
Merci
Last edited by arif85 on Sun May 06, 2012 3:43 am; edited 1 time in total |
|
Back to top |
|
arif85 Smarty n00b
Joined: 20 Aug 2011 Posts: 4
|
Posted: Sun Aug 21, 2011 1:48 pm Post subject: |
|
|
Ok , j'ai trouvé une solution à mon problème.
Visiblement la comunauté FR de Smarty semble très active. |
|
Back to top |
|
cobex4 Smarty Regular
Joined: 02 Sep 2008 Posts: 74
|
Posted: Wed Aug 24, 2011 2:46 pm Post subject: |
|
|
Y à pas pas QUE en FR |
|
Back to top |
|
arif85 Smarty n00b
Joined: 20 Aug 2011 Posts: 4
|
Posted: Sat Aug 27, 2011 3:29 pm Post subject: |
|
|
cobex4 wrote: | Y à pas pas QUE en FR |
domage car le template smarty me parrait bien. or pour trouver de l aide il faut souvent aller sur google et l on tombre sur divers sites externes... |
|
Back to top |
|
Gowser Smarty Pro
Joined: 19 Feb 2008 Posts: 104 Location: Nantes (France)
|
Posted: Mon Nov 14, 2011 1:37 pm Post subject: |
|
|
Juste pour information :
Il y a plus facile comme solution que de faire un shuffle sur ton tableau (surtout si ton tableau ne retourne rien, le suffle pete) :
Code: |
$query = $connexion->prepare("SELECT * FROM citation ORDER BY id ASC");
par :
$query = $connexion->prepare("SELECT * FROM citation ORDER BY RAND()");
|
et tu n'as pas besoin d'initialiser l'index du tableau "$cite_lacitation" :
Code: |
while ($data = $query->fetch()){
$cite_lacitation[] ['id'] = $data ["id"];
$cite_lacitation[] ['cite'] = $data ["cite"];
$cite_lacitation[] ['author'] = $data ["author"];
}
|
voir même (je prefere)
Code: |
while ($data = $query->fetch()){
$cite_lacitation[] = array(
'id' => $data ["id"],
'cite' => $data ["cite"],
'author' => $data ["author"]
);
}
|
suffit à initialiser ton tableau
Cordialement _________________ N'oubliez pas de mettre le tag "[Résolu]" sur vos post |
|
Back to top |
|
adn Smarty Regular
Joined: 31 May 2011 Posts: 91
|
Posted: Tue Nov 22, 2011 9:49 am Post subject: |
|
|
Hello,
Perso je ferai encore plus simple sans charger toute la base en mémoire, ce qui est inutile
Code: | $query = $connexion->prepare("SELECT * FROM citation ORDER BY RAND() LIMIT 1");
if ($row = $query->mysql_fetch_row($result)) {..... |
|
|
Back to top |
|
arif85 Smarty n00b
Joined: 20 Aug 2011 Posts: 4
|
Posted: Sun May 06, 2012 3:43 am Post subject: |
|
|
merci beacoup à Gowser et a adn pour votre aide, j avais abondonné ce projet mais je vais m'y remettre de suite |
|
Back to top |
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|