Smarty Forum Index Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon.

[résolu]Citations aléatoires d'une base de donnée

 
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> Language: French
View previous topic :: View next topic  
Author Message
arif85
Smarty n00b


Joined: 20 Aug 2011
Posts: 4

PostPosted: Sat Aug 20, 2011 9:10 pm    Post subject: [résolu]Citations aléatoires d'une base de donnée Reply with quote

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
View user's profile Send private message
arif85
Smarty n00b


Joined: 20 Aug 2011
Posts: 4

PostPosted: Sun Aug 21, 2011 1:48 pm    Post subject: Reply with quote

Ok , j'ai trouvé une solution à mon problème.

Visiblement la comunauté FR de Smarty semble très active. Laughing Rolling Eyes
Back to top
View user's profile Send private message
cobex4
Smarty Regular


Joined: 02 Sep 2008
Posts: 74

PostPosted: Wed Aug 24, 2011 2:46 pm    Post subject: Reply with quote

Y à pas pas QUE en FR Razz Razz
Back to top
View user's profile Send private message
arif85
Smarty n00b


Joined: 20 Aug 2011
Posts: 4

PostPosted: Sat Aug 27, 2011 3:29 pm    Post subject: Reply with quote

cobex4 wrote:
Y à pas pas QUE en FR Razz Razz


Confused Crying or Very sad 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
View user's profile Send private message
Gowser
Smarty Pro


Joined: 19 Feb 2008
Posts: 104
Location: Nantes (France)

PostPosted: Mon Nov 14, 2011 1:37 pm    Post subject: Reply with quote

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 Smile
Back to top
View user's profile Send private message Send e-mail
adn
Smarty Regular


Joined: 31 May 2011
Posts: 91

PostPosted: Tue Nov 22, 2011 9:49 am    Post subject: Reply with quote

Hello,

Perso je ferai encore plus simple sans charger toute la base en mémoire, ce qui est inutile Very Happy

Code:
$query = $connexion->prepare("SELECT * FROM citation ORDER BY RAND() LIMIT 1");

if ($row = $query->mysql_fetch_row($result)) {.....
Back to top
View user's profile Send private message Visit poster's website
arif85
Smarty n00b


Joined: 20 Aug 2011
Posts: 4

PostPosted: Sun May 06, 2012 3:43 am    Post subject: Reply with quote

merci beacoup à Gowser et a adn pour votre aide, j avais abondonné ce projet mais je vais m'y remettre de suite Razz
Back to top
View user's profile Send private message
Display posts from previous:   
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> Language: French All times are GMT
Page 1 of 1

 
Jump to:  
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
Protected by Anti-Spam ACP