Jump to content

[Résolu] variable définie par AVG (sql) et affichage smarty qui ne marche pas


Recommended Posts

Bonjour,
 
J'ai la fonction suivante qui me permet de gérer l'affichage de mes données
 

public function displayTestimonials()
	{
		$output = array(); // create an array named $output to store our testimonials. We will read the from the DB
		$db = Db::getInstance(); // create and object to represent the database

$average = $db->ExecuteS('SELECT AVG(testimonial_rate)
					FROM `'._DB_PREFIX_.'testimonials`
					WHERE testimonial_status = "Enabled";');
		
					
		$nbtestimonials = $db->ExecuteS('SELECT COUNT(ALL testimonial_id)
					FROM `'._DB_PREFIX_.'testimonials`
					WHERE testimonial_status = "Enabled";');

 et la déclaration smarty pour terminer
 

				  $this->smarty->assign(array(
				  'http_host' => $_SERVER['HTTP_HOST'],
				  'this_path' => $this->_path,
			          'base_dir'=> __PS_BASE_URI__,
                                  'testimonials' => $results,
                                  'currentpage' => $currentpage,
                                  'prevpage' => $prevpage,
                                  'nextpage' => $nextpage,
                                  'totalpages' => $totalpages
								  
								  
				  ));
				  
				  		$this->context->smarty->assign('average', $average);
				$this->context->smarty->assign('nbtestimonials', $nbtestimonials);
				  
		  return $this->display(__FILE__, 'displaytestimonials.tpl');

Et voilà simplement le code pour l'affichage sur mon fichier tpl
 

      <p>{$average[0]|@print_r}/5</p>
      <p> (Basé sur {$nbtestimonials[0]|@print_r} avis) </p>

 
Résultat, ça m'affiche Array ( [AVG(testimonial_rate)] => 3.7500 ) 1/5[/size]
(Basé sur Array ( [COUNT(ALL testimonial_id)] => 4 ) 1 avis)

Une petite idée du problème ?

Edited by YakaLire (see edit history)
Link to comment
Share on other sites

Bonjour,

 

Un var_dump($average) devrait montrer la structure du tableau.

En virant le print_r et en essayant  $average[0][0] ou $average[0]['testimonial_rate]

 

Une solution serait aussi d'utiliser ->getRow plutot que ->executeS

 

Cordialement

  • Like 1
Link to comment
Share on other sites

 
Hello math_php !

Merci j'ai pu, grâce à toi, afficher les bons résultats.

Pour info voilà les codes valides :

Côté PHP
 

		// average of all testimonials rate when the status is Enable
		
		$average = $db->ExecuteS('SELECT ROUND(AVG(testimonial_rate),1) AS average
					FROM `'._DB_PREFIX_.'testimonials`
					WHERE testimonial_status = "Enabled";');
					
		// Nomber of all testimonials when the status is Enable
					
		$nbtestimonials = $db->ExecuteS('SELECT COUNT(ALL testimonial_id) AS nbtestimonials
					FROM `'._DB_PREFIX_.'testimonials`
					WHERE testimonial_status = "Enabled";'); 

Ainsi que l'affichage

     {$average[0]['average']}
     {$nbtestimonials[0]['nbtestimonials']}
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...